From c6b3720925acf4df84a52ea3f216d66add7ffa03 Mon Sep 17 00:00:00 2001 From: Shola Adeshina Hassan Date: Wed, 1 Jul 2020 16:57:46 +0100 Subject: [PATCH 1/3] Add option for loading multiple env files from frolder(s) --- .gitignore | 3 + .idea/.gitignore | 2 + .idea/codeStyles/Project.xml | 28 +++ .idea/dbnavigator.xml | 453 +++++++++++++++++++++++++++++++++++ .idea/godotenv.iml | 9 + .idea/misc.xml | 6 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + go.mod | 3 + godotenv.go | 67 +++++- godotenv_test.go | 19 ++ try/main.go | 44 ++++ try/trytry/db.env | 4 + try/trytry/deploy.env | 4 + try/trytry/dev.env | 4 + try/trytry/prod.env | 4 + try/trytry/test.go | 1 + try/trytry/try2/db1.env | 1 + 18 files changed, 660 insertions(+), 6 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/codeStyles/Project.xml create mode 100644 .idea/dbnavigator.xml create mode 100644 .idea/godotenv.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 go.mod create mode 100644 try/main.go create mode 100644 try/trytry/db.env create mode 100644 try/trytry/deploy.env create mode 100644 try/trytry/dev.env create mode 100644 try/trytry/prod.env create mode 100644 try/trytry/test.go create mode 100644 try/trytry/try2/db1.env diff --git a/.gitignore b/.gitignore index e43b0f9..136bbe3 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ .DS_Store + +try +go.mod diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..e7e9d11 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..3cdc6ae --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/dbnavigator.xml b/.idea/dbnavigator.xml new file mode 100644 index 0000000..7a611ac --- /dev/null +++ b/.idea/dbnavigator.xml @@ -0,0 +1,453 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/godotenv.iml b/.idea/godotenv.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/godotenv.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..28a804d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..492819d --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b17d88f --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/open-source/godotenv + +go 1.13 diff --git a/godotenv.go b/godotenv.go index 69e816c..9aef909 100644 --- a/godotenv.go +++ b/godotenv.go @@ -20,6 +20,7 @@ import ( "io" "os" "os/exec" + "path/filepath" "regexp" "sort" "strings" @@ -37,16 +38,32 @@ const doubleQuoteSpecialChars = "\\\n\r\"!$`" // // godotenv.Load("fileone", "filetwo") // +// Also, you can tell it a folder name, and it would crawl the folder tree and load all the env files +// +// godotenv.Load("fixtures") +// // It's important to note that it WILL NOT OVERRIDE an env variable that already exists - consider the .env file to set dev vars or sensible defaults func Load(filenames ...string) (err error) { filenames = filenamesOrDefault(filenames) + paths := make([]string, 0) for _, filename := range filenames { - err = loadFile(filename, false) - if err != nil { - return // return early on a spazout + if _, err = os.Stat(filename); !os.IsNotExist(err) { + paths, err = folders(filenames) + for _, p := range paths { + err = loadFile(p, false) + if err != nil { + return + } + } + }else{ + err = loadFile(filename, false) + if err != nil { + return // return early on a spazout + } } } + return } @@ -60,14 +77,30 @@ func Load(filenames ...string) (err error) { // // godotenv.Overload("fileone", "filetwo") // +// Also, you can tell it a folder name, and it would crawl the folder tree and load all the env files +// +// godotenv.Overload("fixtures") +// +// // It's important to note this WILL OVERRIDE an env variable that already exists - consider the .env file to forcefilly set all vars. func Overload(filenames ...string) (err error) { filenames = filenamesOrDefault(filenames) + paths := make([]string, 0) for _, filename := range filenames { - err = loadFile(filename, true) - if err != nil { - return // return early on a spazout + if _, err = os.Stat(filename); !os.IsNotExist(err) { + paths, err = folders(filenames) + for _, p := range paths { + err = loadFile(p, true) + if err != nil { + return + } + } + }else{ + err = loadFile(filename, true) + if err != nil { + return // return early on a spazout + } } } return @@ -182,6 +215,28 @@ func filenamesOrDefault(filenames []string) []string { return filenames } +func folders(folders []string) ([]string, error) { + paths := make([]string, 0) + + for _, folder := range folders { + err := filepath.Walk(folder, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if ext := filepath.Ext(path); ext == ".env" { + paths = append(paths, path) + } + return nil + }) + if err != nil { + return nil, err + } + } + + return paths, nil +} + func loadFile(filename string, overload bool) error { envMap, err := readFile(filename) if err != nil { diff --git a/godotenv_test.go b/godotenv_test.go index d1f73cb..62e517c 100644 --- a/godotenv_test.go +++ b/godotenv_test.go @@ -40,6 +40,18 @@ func loadEnvAndCompareValues(t *testing.T, loader func(files ...string) error, e } } +func compareTotal(t *testing.T, loader func(files []string) ([]string, error), envFolderName []string, expectedNum int) { + values, err := loader(envFolderName) + if err != nil { + t.Fatalf("Error loading %v", envFolderName) + } + + if len(values) != expectedNum { + t.Errorf("wrong number of files fetched: expected, %v got %v", len(values), expectedNum) + } + +} + func TestLoadWithNoArgsLoadsDotEnv(t *testing.T) { err := Load() pathError := err.(*os.PathError) @@ -416,6 +428,13 @@ func TestErrorReadDirectory(t *testing.T) { } } +func TestLoadMultipleEnvFiles(t *testing.T) { + folderName := []string{"fixtures"} + expectedFilesNum := 6 + + compareTotal(t, folders, folderName, expectedFilesNum) +} + func TestErrorParsing(t *testing.T) { envFileName := "fixtures/invalid1.env" envMap, err := Read(envFileName) diff --git a/try/main.go b/try/main.go new file mode 100644 index 0000000..2a084ff --- /dev/null +++ b/try/main.go @@ -0,0 +1,44 @@ +package main + +import ( + "fmt" + "github.com/open-source/godotenv" + "log" + "os" +) + +func main() { + err := godotenv.Load("trytry") + if err != nil { + log.Fatal("FATAL ERROR: ", err) + } + + //err = godotenv.Load("deploy.env", "dev.env") + //if err != nil { + // log.Fatal("FATAL ERROR: ", err) + //} + + dbA := os.Getenv("A") + dbB := os.Getenv("B") + dbC := os.Getenv("C") + dbD := os.Getenv("D") + dbName := os.Getenv("NAME") + fmt.Println(dbA, "...", dbB, "...", dbC, "...", dbD) + fmt.Println(dbName) + + dbAA := os.Getenv("AA") + dbBB:= os.Getenv("BB") + dbCC := os.Getenv("CC") + dbDD := os.Getenv("DD") + fmt.Println(dbAA, "...", dbBB, "...", dbCC, "...", dbDD) + + dbAAA := os.Getenv("AAA") + dbBBB := os.Getenv("BBB") + dbCCC := os.Getenv("CCC") + dbDDD := os.Getenv("DDD") + fmt.Println(dbAAA, "...", dbBBB, "...", dbCCC, "...", dbDDD) + + + + // now do something with s3 or whatever +} diff --git a/try/trytry/db.env b/try/trytry/db.env new file mode 100644 index 0000000..f9fb955 --- /dev/null +++ b/try/trytry/db.env @@ -0,0 +1,4 @@ +A=A +B=B +C=C +D=D diff --git a/try/trytry/deploy.env b/try/trytry/deploy.env new file mode 100644 index 0000000..ecdea93 --- /dev/null +++ b/try/trytry/deploy.env @@ -0,0 +1,4 @@ +AA=AA +BB=BB +CC=CC +DD=DD diff --git a/try/trytry/dev.env b/try/trytry/dev.env new file mode 100644 index 0000000..8b6f6c7 --- /dev/null +++ b/try/trytry/dev.env @@ -0,0 +1,4 @@ +AAA=AAA +BBB=BBB +CCC=CCC +DDD=DDD diff --git a/try/trytry/prod.env b/try/trytry/prod.env new file mode 100644 index 0000000..b167f59 --- /dev/null +++ b/try/trytry/prod.env @@ -0,0 +1,4 @@ +AAAA=AAAA +BBBB=BBBB +CCCC=CCCC +DDDD=DDDD diff --git a/try/trytry/test.go b/try/trytry/test.go new file mode 100644 index 0000000..71921bb --- /dev/null +++ b/try/trytry/test.go @@ -0,0 +1 @@ +package trytry diff --git a/try/trytry/try2/db1.env b/try/trytry/try2/db1.env new file mode 100644 index 0000000..b4f6c4f --- /dev/null +++ b/try/trytry/try2/db1.env @@ -0,0 +1 @@ +NAME=ADESHINA \ No newline at end of file From 4d797ed211da6e47b09989294e4892e2a836ff01 Mon Sep 17 00:00:00 2001 From: Shola Adeshina Hassan Date: Wed, 1 Jul 2020 17:22:29 +0100 Subject: [PATCH 2/3] Untrack files in .gitignore --- go.mod | 3 --- try/main.go | 44 ----------------------------------------- try/trytry/db.env | 4 ---- try/trytry/deploy.env | 4 ---- try/trytry/dev.env | 4 ---- try/trytry/prod.env | 4 ---- try/trytry/test.go | 1 - try/trytry/try2/db1.env | 1 - 8 files changed, 65 deletions(-) delete mode 100644 go.mod delete mode 100644 try/main.go delete mode 100644 try/trytry/db.env delete mode 100644 try/trytry/deploy.env delete mode 100644 try/trytry/dev.env delete mode 100644 try/trytry/prod.env delete mode 100644 try/trytry/test.go delete mode 100644 try/trytry/try2/db1.env diff --git a/go.mod b/go.mod deleted file mode 100644 index b17d88f..0000000 --- a/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module github.com/open-source/godotenv - -go 1.13 diff --git a/try/main.go b/try/main.go deleted file mode 100644 index 2a084ff..0000000 --- a/try/main.go +++ /dev/null @@ -1,44 +0,0 @@ -package main - -import ( - "fmt" - "github.com/open-source/godotenv" - "log" - "os" -) - -func main() { - err := godotenv.Load("trytry") - if err != nil { - log.Fatal("FATAL ERROR: ", err) - } - - //err = godotenv.Load("deploy.env", "dev.env") - //if err != nil { - // log.Fatal("FATAL ERROR: ", err) - //} - - dbA := os.Getenv("A") - dbB := os.Getenv("B") - dbC := os.Getenv("C") - dbD := os.Getenv("D") - dbName := os.Getenv("NAME") - fmt.Println(dbA, "...", dbB, "...", dbC, "...", dbD) - fmt.Println(dbName) - - dbAA := os.Getenv("AA") - dbBB:= os.Getenv("BB") - dbCC := os.Getenv("CC") - dbDD := os.Getenv("DD") - fmt.Println(dbAA, "...", dbBB, "...", dbCC, "...", dbDD) - - dbAAA := os.Getenv("AAA") - dbBBB := os.Getenv("BBB") - dbCCC := os.Getenv("CCC") - dbDDD := os.Getenv("DDD") - fmt.Println(dbAAA, "...", dbBBB, "...", dbCCC, "...", dbDDD) - - - - // now do something with s3 or whatever -} diff --git a/try/trytry/db.env b/try/trytry/db.env deleted file mode 100644 index f9fb955..0000000 --- a/try/trytry/db.env +++ /dev/null @@ -1,4 +0,0 @@ -A=A -B=B -C=C -D=D diff --git a/try/trytry/deploy.env b/try/trytry/deploy.env deleted file mode 100644 index ecdea93..0000000 --- a/try/trytry/deploy.env +++ /dev/null @@ -1,4 +0,0 @@ -AA=AA -BB=BB -CC=CC -DD=DD diff --git a/try/trytry/dev.env b/try/trytry/dev.env deleted file mode 100644 index 8b6f6c7..0000000 --- a/try/trytry/dev.env +++ /dev/null @@ -1,4 +0,0 @@ -AAA=AAA -BBB=BBB -CCC=CCC -DDD=DDD diff --git a/try/trytry/prod.env b/try/trytry/prod.env deleted file mode 100644 index b167f59..0000000 --- a/try/trytry/prod.env +++ /dev/null @@ -1,4 +0,0 @@ -AAAA=AAAA -BBBB=BBBB -CCCC=CCCC -DDDD=DDDD diff --git a/try/trytry/test.go b/try/trytry/test.go deleted file mode 100644 index 71921bb..0000000 --- a/try/trytry/test.go +++ /dev/null @@ -1 +0,0 @@ -package trytry diff --git a/try/trytry/try2/db1.env b/try/trytry/try2/db1.env deleted file mode 100644 index b4f6c4f..0000000 --- a/try/trytry/try2/db1.env +++ /dev/null @@ -1 +0,0 @@ -NAME=ADESHINA \ No newline at end of file From ed6cbe4f0b978f27c7e90c0cddbad5eaaa977a50 Mon Sep 17 00:00:00 2001 From: Shola Adeshina Hassan Date: Wed, 1 Jul 2020 17:24:28 +0100 Subject: [PATCH 3/3] Untrack files in .gitignore --- .gitignore | 1 + .idea/.gitignore | 2 - .idea/codeStyles/Project.xml | 28 --- .idea/dbnavigator.xml | 453 ----------------------------------- .idea/godotenv.iml | 9 - .idea/misc.xml | 6 - .idea/modules.xml | 8 - .idea/vcs.xml | 6 - 8 files changed, 1 insertion(+), 512 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/codeStyles/Project.xml delete mode 100644 .idea/dbnavigator.xml delete mode 100644 .idea/godotenv.iml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml diff --git a/.gitignore b/.gitignore index 136bbe3..f5511b5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .DS_Store +.idea try go.mod diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index e7e9d11..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -# Default ignored files -/workspace.xml diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml deleted file mode 100644 index 3cdc6ae..0000000 --- a/.idea/codeStyles/Project.xml +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/dbnavigator.xml b/.idea/dbnavigator.xml deleted file mode 100644 index 7a611ac..0000000 --- a/.idea/dbnavigator.xml +++ /dev/null @@ -1,453 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/godotenv.iml b/.idea/godotenv.iml deleted file mode 100644 index 5e764c4..0000000 --- a/.idea/godotenv.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 28a804d..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 492819d..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file