Skip to content

Commit

Permalink
Merge pull request #9761 from ulyssessouza/refactor-osenv-precedence
Browse files Browse the repository at this point in the history
Give environment variables precedence back to OS over .env
  • Loading branch information
ulyssessouza committed Aug 18, 2022
2 parents a64a5a6 + 3f4f4e5 commit 10cfd55
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
6 changes: 4 additions & 2 deletions cmd/compose/compose.go
Expand Up @@ -383,8 +383,10 @@ func setEnvWithDotEnv(prjOpts *projectOptions) error {
return err
}
for k, v := range envFromFile {
if err := os.Setenv(k, v); err != nil { // overwrite the process env with merged OS + env file results
return err
if _, ok := os.LookupEnv(k); !ok { // Precedence to OS Env
if err := os.Setenv(k, v); err != nil {
return err
}
}
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -6,7 +6,7 @@ require (
github.com/AlecAivazis/survey/v2 v2.3.5
github.com/buger/goterm v1.0.4
github.com/cnabio/cnab-to-oci v0.3.6
github.com/compose-spec/compose-go v1.4.0
github.com/compose-spec/compose-go v1.5.0
github.com/containerd/console v1.0.3
github.com/containerd/containerd v1.6.8
github.com/distribution/distribution/v3 v3.0.0-20220729163034-26163d82560f
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -286,8 +286,8 @@ github.com/cockroachdb/errors v1.2.4/go.mod h1:rQD95gz6FARkaKkQXUksEje/d9a6wBJoC
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f/go.mod h1:i/u985jwjWRlyHXQbwatDASoW0RMlZ/3i9yJHE2xLkI=
github.com/codahale/hdrhistogram v0.0.0-20160425231609-f8ad88b59a58/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
github.com/compose-spec/compose-go v1.2.1/go.mod h1:pAy7Mikpeft4pxkFU565/DRHEbDfR84G6AQuiL+Hdg8=
github.com/compose-spec/compose-go v1.4.0 h1:zaYVAZ6lIByr7Jffi20AabfeUwcTrdXfH3X1R5HEm+g=
github.com/compose-spec/compose-go v1.4.0/go.mod h1:l7RUULbFFLzlQHuxtJr7SVLyWdqEpbJEGTWCgcu6Eqw=
github.com/compose-spec/compose-go v1.5.0 h1:yOmYpIm13pYt2o+oKVe/JAD6o2Tv+eUyOcRhf0qF4fA=
github.com/compose-spec/compose-go v1.5.0/go.mod h1:l7RUULbFFLzlQHuxtJr7SVLyWdqEpbJEGTWCgcu6Eqw=
github.com/containerd/aufs v0.0.0-20200908144142-dab0cbea06f4/go.mod h1:nukgQABAEopAHvB6j7cnP5zJ+/3aVcE7hCYqvIwAHyE=
github.com/containerd/aufs v0.0.0-20201003224125-76a6863f2989/go.mod h1:AkGGQs9NM2vtYHaUen+NljV0/baGCAPELGm2q9ZXpWU=
github.com/containerd/aufs v0.0.0-20210316121734-20793ff83c97/go.mod h1:kL5kd6KM5TzQjR79jljyi4olc1Vrx6XBlcyj3gNv2PU=
Expand Down
8 changes: 4 additions & 4 deletions pkg/e2e/compose_environment_test.go
Expand Up @@ -34,7 +34,7 @@ func TestEnvPriority(t *testing.T) {
})

// Full options activated
// 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected (From environment patched by --env-file)
// 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected (From OS Environment)
// 2. Compose File (service::environment section)
// 3. Compose File (service::env_file section file)
// 4. Container Image ENV directive
Expand All @@ -45,7 +45,7 @@ func TestEnvPriority(t *testing.T) {
"run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
cmd.Env = append(cmd.Env, "WHEREAMI=shell")
res := icmd.RunCmd(cmd)
assert.Equal(t, strings.TrimSpace(res.Stdout()), "override")
assert.Equal(t, strings.TrimSpace(res.Stdout()), "shell")
})

// Full options activated
Expand All @@ -63,7 +63,7 @@ func TestEnvPriority(t *testing.T) {
})

// No Compose file, all other options
// 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected (From environment patched by --env-file)
// 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected (From OS Environment)
// 2. Compose File (service::environment section)
// 3. Compose File (service::env_file section file)
// 4. Container Image ENV directive
Expand All @@ -74,7 +74,7 @@ func TestEnvPriority(t *testing.T) {
"run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
cmd.Env = append(cmd.Env, "WHEREAMI=shell")
res := icmd.RunCmd(cmd)
assert.Equal(t, strings.TrimSpace(res.Stdout()), "override")
assert.Equal(t, strings.TrimSpace(res.Stdout()), "shell")
})

// No Compose file, all other options with env variable from OS environment
Expand Down

0 comments on commit 10cfd55

Please sign in to comment.