feat: load env from file

This commit is contained in:
Urko 2023-03-09 13:29:05 +01:00
parent d815b12719
commit e77a67a3de
2 changed files with 11 additions and 6 deletions

View File

@ -27,9 +27,9 @@ var envConfig struct {
var writer pkgio.WriterIface
func intEnvConfig(isProd bool) {
if !isProd {
err := godotenv.Load(util.RootDir() + "/.env")
func intEnvConfig(envFilePath string) {
if envFilePath != "" {
err := godotenv.Load(envFilePath)
if err != nil {
log.Fatalf("environment variable ENV is empty and an error occurred while loading the .env file: %s\n", err)
}
@ -134,7 +134,12 @@ func exportPem(filename string, data []byte) (string, error) {
}
func init() {
intEnvConfig(false)
envFile := ""
if os.Getenv("ENV") != "prod" {
envFile = "./.env"
}
intEnvConfig(envFile)
cobra.OnInitialize(initConfig)
}

View File

@ -44,8 +44,8 @@ func init() {
func Test_exportPem(t *testing.T) {
defer func() {
os.Remove(testFile)
// err := os.Remove(testFile)
// require.NoError(t, err)
err := os.Remove(testFile)
require.NoError(t, err)
}()
data := []byte("test data")