Skip to content

Commit c796fcc

Browse files
authored
Merge pull request #284 from compose-spec/fix-numeric-named-vars
Ignore variable names starting with numbers
2 parents d2f6dcf + bc2974c commit c796fcc

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

dotenv/fixtures/plain.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ OPTION_B=2
33
OPTION_C= 3
44
OPTION_D =4
55
OPTION_E = 5
6+
456 = ABC
67
OPTION_F =
78
OPTION_G=
89
OPTION_H = my string # Inline comment

dotenv/godotenv.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,26 +93,30 @@ func load(overload bool, filenames ...string) (err error) {
9393
return
9494
}
9595

96+
var startsWithDigitRegex = regexp.MustCompile(`^\s*\d.*`) // Keys starting with numbers are ignored
97+
9698
// ReadWithLookup gets all env vars from the files and/or lookup function and return values as
9799
// a map rather than automatically writing values into env
98-
func ReadWithLookup(lookupFn LookupFn, filenames ...string) (envMap map[string]string, err error) {
100+
func ReadWithLookup(lookupFn LookupFn, filenames ...string) (map[string]string, error) {
99101
filenames = filenamesOrDefault(filenames)
100-
envMap = make(map[string]string)
102+
envMap := make(map[string]string)
101103

102104
for _, filename := range filenames {
103105
individualEnvMap, individualErr := readFile(filename, lookupFn)
104106

105107
if individualErr != nil {
106-
err = individualErr
107-
return // return early on a spazout
108+
return envMap, individualErr
108109
}
109110

110111
for key, value := range individualEnvMap {
112+
if startsWithDigitRegex.MatchString(key) {
113+
continue
114+
}
111115
envMap[key] = value
112116
}
113117
}
114118

115-
return
119+
return envMap, nil
116120
}
117121

118122
// Read all env (with same file loading semantics as Load) but return values as
@@ -183,7 +187,7 @@ func Marshal(envMap map[string]string) (string, error) {
183187
if d, err := strconv.Atoi(v); err == nil {
184188
lines = append(lines, fmt.Sprintf(`%s=%d`, k, d))
185189
} else {
186-
lines = append(lines, fmt.Sprintf(`%s="%s"`, k, doubleQuoteEscape(v))) //nolint // Cannot use %q here
190+
lines = append(lines, fmt.Sprintf(`%s="%s"`, k, doubleQuoteEscape(v))) // nolint // Cannot use %q here
187191
}
188192
}
189193
sort.Strings(lines)

0 commit comments

Comments
 (0)