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 var writer pkgio.WriterIface
func intEnvConfig(isProd bool) { func intEnvConfig(envFilePath string) {
if !isProd { if envFilePath != "" {
err := godotenv.Load(util.RootDir() + "/.env") err := godotenv.Load(envFilePath)
if err != nil { if err != nil {
log.Fatalf("environment variable ENV is empty and an error occurred while loading the .env file: %s\n", err) 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() { func init() {
intEnvConfig(false) envFile := ""
if os.Getenv("ENV") != "prod" {
envFile = "./.env"
}
intEnvConfig(envFile)
cobra.OnInitialize(initConfig) cobra.OnInitialize(initConfig)
} }

View File

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