Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.

Commit 8a39b06

Browse files
authored
Merge pull request #2124 from chris-crone/backport-19.03-env-merge
[19.03 backport] Add option to remove `env_file` entry once it's merged in the `environment` section Upstream-commit: 8a9e86c728ad591a199149de73624be4dd2bc371 Component: cli
2 parents cce8b2f + c81f631 commit 8a39b06

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

components/cli/cli/compose/loader/loader.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ type Options struct {
3232
SkipInterpolation bool
3333
// Interpolation options
3434
Interpolate *interp.Options
35+
// Discard 'env_file' entries after resolving to 'environment' section
36+
discardEnvFiles bool
37+
}
38+
39+
// WithDiscardEnvFiles sets the Options to discard the `env_file` section after resolving to
40+
// the `environment` section
41+
func WithDiscardEnvFiles(opts *Options) {
42+
opts.discardEnvFiles = true
3543
}
3644

3745
// ParseYAML reads the bytes from a file, parses the bytes into a mapping
@@ -105,6 +113,11 @@ func Load(configDetails types.ConfigDetails, options ...func(*Options)) (*types.
105113
return nil, err
106114
}
107115
cfg.Filename = file.Filename
116+
if opts.discardEnvFiles {
117+
for i := range cfg.Services {
118+
cfg.Services[i].EnvFile = nil
119+
}
120+
}
108121

109122
configs = append(configs, cfg)
110123
}

components/cli/cli/compose/loader/loader_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,38 @@ services:
759759
assert.Check(t, is.DeepEqual([]string{"build", "links", "pid"}, unsupported))
760760
}
761761

762+
func TestDiscardEnvFileOption(t *testing.T) {
763+
dict, err := ParseYAML([]byte(`version: "3"
764+
services:
765+
web:
766+
image: nginx
767+
env_file:
768+
- example1.env
769+
- example2.env
770+
`))
771+
expectedEnvironmentMap := types.MappingWithEquals{
772+
"FOO": strPtr("foo_from_env_file"),
773+
"BAZ": strPtr("baz_from_env_file"),
774+
"BAR": strPtr("bar_from_env_file_2"), // Original value is overwritten by example2.env
775+
"QUX": strPtr("quz_from_env_file_2"),
776+
}
777+
assert.NilError(t, err)
778+
configDetails := buildConfigDetails(dict, nil)
779+
780+
// Default behavior keeps the `env_file` entries
781+
configWithEnvFiles, err := Load(configDetails)
782+
assert.NilError(t, err)
783+
assert.DeepEqual(t, configWithEnvFiles.Services[0].EnvFile, types.StringList{"example1.env",
784+
"example2.env"})
785+
assert.DeepEqual(t, configWithEnvFiles.Services[0].Environment, expectedEnvironmentMap)
786+
787+
// Custom behavior removes the `env_file` entries
788+
configWithoutEnvFiles, err := Load(configDetails, WithDiscardEnvFiles)
789+
assert.NilError(t, err)
790+
assert.DeepEqual(t, configWithoutEnvFiles.Services[0].EnvFile, types.StringList(nil))
791+
assert.DeepEqual(t, configWithoutEnvFiles.Services[0].Environment, expectedEnvironmentMap)
792+
}
793+
762794
func TestBuildProperties(t *testing.T) {
763795
dict, err := ParseYAML([]byte(`
764796
version: "3"

0 commit comments

Comments
 (0)