Skip to content

Commit 704c890

Browse files
authored
Merge pull request #266 from glours/env-var-priority
2 parents 5bab6eb + 8256ba8 commit 704c890

File tree

5 files changed

+46
-14
lines changed

5 files changed

+46
-14
lines changed

cli/options.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,22 +226,18 @@ func WithDotEnv(o *ProjectOptions) error {
226226
}
227227
defer file.Close()
228228

229-
notInEnvSet := make(map[string]interface{})
230229
env, err := dotenv.ParseWithLookup(file, func(k string) (string, bool) {
231230
v, ok := o.Environment[k]
232231
if !ok {
233-
notInEnvSet[k] = nil
234-
return "", true
232+
233+
return "", false
235234
}
236235
return v, true
237236
})
238237
if err != nil {
239238
return err
240239
}
241240
for k, v := range env {
242-
if _, ok := notInEnvSet[k]; ok {
243-
continue
244-
}
245241
if _, set := o.Environment[k]; set {
246242
continue
247243
}

dotenv/godotenv_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,3 +612,34 @@ func TestExpendingEnvironmentWithLookup(t *testing.T) {
612612
t.Errorf("Expected '%v' to parse as '%v' => '%v', got '%v' => '%v' instead", rawEnvLine, key, expectedValue, key, value)
613613
}
614614
}
615+
616+
func TestSubstitutionsWithShellEnvPrecedence(t *testing.T) {
617+
os.Clearenv()
618+
const envKey = "OPTION_A"
619+
const envVal = "5"
620+
os.Setenv(envKey, envVal)
621+
defer os.Unsetenv(envKey)
622+
623+
envFileName := "fixtures/substitutions.env"
624+
expectedValues := map[string]string{
625+
"OPTION_A": "5",
626+
"OPTION_B": "5",
627+
"OPTION_C": "5",
628+
"OPTION_D": "55",
629+
"OPTION_E": "",
630+
}
631+
632+
envMap, err := ReadWithLookup(os.LookupEnv, envFileName)
633+
if err != nil {
634+
t.Error("Error reading file")
635+
}
636+
if len(envMap) != len(expectedValues) {
637+
t.Error("Didn't get the right size map back")
638+
}
639+
640+
for key, value := range expectedValues {
641+
if envMap[key] != value {
642+
t.Errorf("Read got one of the keys wrong, [%q]->%q", key, envMap[key])
643+
}
644+
}
645+
}

dotenv/parser.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ const (
1818

1919
func parseBytes(src []byte, out map[string]string, lookupFn LookupFn) error {
2020
cutset := src
21+
if lookupFn == nil {
22+
lookupFn = noLookupFn
23+
}
2124
for {
2225
cutset = getStatementStart(cutset)
2326
if cutset == nil {
@@ -34,9 +37,6 @@ func parseBytes(src []byte, out map[string]string, lookupFn LookupFn) error {
3437
}
3538

3639
if inherited {
37-
if lookupFn == nil {
38-
lookupFn = noLookupFn
39-
}
4040

4141
value, ok := lookupFn(key)
4242
if ok {
@@ -50,6 +50,9 @@ func parseBytes(src []byte, out map[string]string, lookupFn LookupFn) error {
5050
if err != nil {
5151
return err
5252
}
53+
if lookUpValue, ok := lookupFn(key); ok {
54+
value = lookUpValue
55+
}
5356

5457
out[key] = value
5558
cutset = left

loader/full-struct_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func services(workingDir, homeDir string) []types.ServiceConfig {
165165
Entrypoint: []string{"/code/entrypoint.sh", "-p", "3000"},
166166
Environment: map[string]*string{
167167
"FOO": strPtr("foo_from_env_file"),
168-
"BAR": strPtr("bar_from_env_file_2"),
168+
"BAR": strPtr("this is a secret"),
169169
"BAZ": strPtr("baz_from_service_def"),
170170
"QUX": strPtr("qux_from_environment"),
171171
},
@@ -689,7 +689,7 @@ services:
689689
- -p
690690
- "3000"
691691
environment:
692-
BAR: bar_from_env_file_2
692+
BAR: this is a secret
693693
BAZ: baz_from_service_def
694694
FOO: foo_from_env_file
695695
QUX: qux_from_environment
@@ -1259,7 +1259,7 @@ func fullExampleJSON(workingDir, homeDir string) string {
12591259
"3000"
12601260
],
12611261
"environment": {
1262-
"BAR": "bar_from_env_file_2",
1262+
"BAR": "this is a secret",
12631263
"BAZ": "baz_from_service_def",
12641264
"FOO": "foo_from_env_file",
12651265
"QUX": "qux_from_environment"

loader/loader_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,8 +1833,10 @@ func TestLoadServiceWithEnvFile(t *testing.T) {
18331833
"env_file": file.Name(),
18341834
}
18351835
s, err := LoadService("Test Name", m, ".", func(s string) (string, bool) {
1836-
assert.Equal(t, "TEST", s)
1837-
return "YES", true
1836+
if s == "TEST" {
1837+
return "YES", true
1838+
}
1839+
return "", false
18381840
}, true, false)
18391841
assert.NilError(t, err)
18401842
assert.Equal(t, "YES", *s.Environment["HALLO"])

0 commit comments

Comments
 (0)