MCPcopy Create free account
hub / github.com/compose-spec/compose-go / GetEnvFromFile

Function GetEnvFromFile

dotenv/env.go:26–73  ·  view source on GitHub ↗
(currentEnv map[string]string, filenames []string)

Source from the content-addressed store, hash-verified

24)
25
26func GetEnvFromFile(currentEnv map[string]string, filenames []string) (map[string]string, error) {
27 envMap := make(map[string]string)
28
29 for _, dotEnvFile := range filenames {
30 abs, err := filepath.Abs(dotEnvFile)
31 if err != nil {
32 return envMap, err
33 }
34 dotEnvFile = abs
35
36 s, err := os.Stat(dotEnvFile)
37 if os.IsNotExist(err) {
38 return envMap, fmt.Errorf("couldn't find env file: %s", dotEnvFile)
39 }
40 if err != nil {
41 return envMap, err
42 }
43
44 if s.IsDir() {
45 if len(filenames) == 0 {
46 return envMap, nil
47 }
48 return envMap, fmt.Errorf("%s is a directory", dotEnvFile)
49 }
50
51 b, err := os.ReadFile(dotEnvFile)
52 if os.IsNotExist(err) {
53 return nil, fmt.Errorf("couldn't read env file: %s", dotEnvFile)
54 }
55 if err != nil {
56 return envMap, err
57 }
58
59 err = parseWithLookup(bytes.NewReader(b), envMap, func(k string) (string, bool) {
60 v, ok := currentEnv[k]
61 if ok {
62 return v, true
63 }
64 v, ok = envMap[k]
65 return v, ok
66 })
67 if err != nil {
68 return envMap, fmt.Errorf("failed to read %s: %w", dotEnvFile, err)
69 }
70 }
71
72 return envMap, nil
73}

Callers 4

WithDotEnvFunction · 0.92
ApplyIncludeFunction · 0.92
TestGetEnvFromFileFunction · 0.85
TestMultipleFilesFunction · 0.85

Calls 1

parseWithLookupFunction · 0.85

Tested by 2

TestGetEnvFromFileFunction · 0.68
TestMultipleFilesFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…