github.com/Goboolean/common@v0.0.0-20231130153141-cb54596b217d/internal/util/env/env.go (about) 1 package env 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 8 "github.com/joho/godotenv" 9 ) 10 11 // Just import this package to get all the env variables at the root of the project 12 // Import this package anonymously as shown below: 13 // import _ "github.com/Goboolean/common/pkg/env" 14 15 const ( 16 rootBase = "common" 17 containerRootBase = "app" 18 ) 19 20 func init() { 21 path, err := filepath.Abs(".") 22 if err != nil { 23 panic(err) 24 } 25 26 for base := filepath.Base(path); base != rootBase && base != containerRootBase; { 27 path = filepath.Dir(path) 28 base = filepath.Base(path) 29 30 if base == "." || base == "/" { 31 panic(errRootNotFound) 32 } 33 } 34 35 if err := os.Chdir(path); err != nil { 36 panic(err) 37 } 38 39 if err := godotenv.Load(); err != nil { 40 panic(err) 41 } 42 } 43 44 var errRootNotFound = fmt.Errorf("could not find root directory, be sure to set root of the project as %s", rootBase)