Skip to content

Commit a7f6c4c

Browse files
EnvekStanislau Arsoba
and
Stanislau Arsoba
authored
Re-add global env variable substitution (#227)
Co-authored-by: Stanislau Arsoba <[email protected]>
1 parent 32e64fa commit a7f6c4c

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

fixtures/substitutions.env

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ OPTION_B=${OPTION_A}
33
OPTION_C=$OPTION_B
44
OPTION_D=${OPTION_A}${OPTION_B}
55
OPTION_E=${OPTION_NOT_DEFINED}
6+
OPTION_F=${GLOBAL_OPTION}

godotenv_test.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,21 @@ func TestLoadQuotedEnv(t *testing.T) {
207207

208208
func TestSubstitutions(t *testing.T) {
209209
envFileName := "fixtures/substitutions.env"
210+
211+
presets := map[string]string{
212+
"GLOBAL_OPTION": "global",
213+
}
214+
210215
expectedValues := map[string]string{
211216
"OPTION_A": "1",
212217
"OPTION_B": "1",
213218
"OPTION_C": "1",
214219
"OPTION_D": "11",
215220
"OPTION_E": "",
221+
"OPTION_F": "global",
216222
}
217223

218-
loadEnvAndCompareValues(t, Load, envFileName, expectedValues, noopPresets)
224+
loadEnvAndCompareValues(t, Load, envFileName, expectedValues, presets)
219225
}
220226

221227
func TestExpanding(t *testing.T) {

parser.go

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"errors"
66
"fmt"
7+
"os"
78
"regexp"
89
"strings"
910
"unicode"
@@ -264,6 +265,12 @@ func expandVariables(v string, m map[string]string) string {
264265
if submatch[1] == "\\" || submatch[2] == "(" {
265266
return submatch[0][1:]
266267
} else if submatch[4] != "" {
268+
if val, ok := m[submatch[4]]; ok {
269+
return val
270+
}
271+
if val, ok := os.LookupEnv(submatch[4]); ok {
272+
return val
273+
}
267274
return m[submatch[4]]
268275
}
269276
return s

0 commit comments

Comments
 (0)