package dotenv import ( "bytes" "io" "os" "path/filepath" "gitserver.in/patialtech/rano/util/logger" ) // // ported from https://github.com/joho/godotenv/blob/main/godotenv.go // // Read all env (with same file loading semantics as Load) but return values as // a map rather than automatically writing values into env func Read(dir string, filenames ...string) (envMap map[string]string, err error) { filenames = filenamesOrDefault(filenames) envMap = make(map[string]string) for _, filename := range filenames { logger.Info("read env file %s", filepath.Join(dir, filename)) individualEnvMap, individualErr := readFile(filepath.Join(dir, filename)) if individualErr != nil { err = individualErr return // return early on a spazout } for key, value := range individualEnvMap { envMap[key] = value } } return } func filenamesOrDefault(filenames []string) []string { if len(filenames) == 0 { return []string{".env"} } return filenames } func readFile(filename string) (envMap map[string]string, err error) { file, err := os.Open(filename) if err != nil { return } defer file.Close() return parse(file) } // parse reads an env file from io.Reader, returning a map of keys and values. func parse(r io.Reader) (map[string]string, error) { var buf bytes.Buffer _, err := io.Copy(&buf, r) if err != nil { return nil, err } return unmarshal(buf.Bytes()) } // unmarshal parses env file from byte slice of chars, returning a map of keys and values. func unmarshal(src []byte) (map[string]string, error) { out := make(map[string]string) err := parseBytes(src, out) return out, err }