Skip to content

Commit

Permalink
Merge pull request #2060 from ulyssessouza/redundant_envfile
Browse files Browse the repository at this point in the history
Add option to remove `env_file` entry once it's merged in the `environment` section
  • Loading branch information
thaJeztah authored Sep 23, 2019
2 parents f429e4b + 821f5ec commit 83751b9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
13 changes: 13 additions & 0 deletions cli/compose/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ type Options struct {
SkipInterpolation bool
// Interpolation options
Interpolate *interp.Options
// Discard 'env_file' entries after resolving to 'environment' section
discardEnvFiles bool
}

// WithDiscardEnvFiles sets the Options to discard the `env_file` section after resolving to
// the `environment` section
func WithDiscardEnvFiles(opts *Options) {
opts.discardEnvFiles = true
}

// ParseYAML reads the bytes from a file, parses the bytes into a mapping
Expand Down Expand Up @@ -105,6 +113,11 @@ func Load(configDetails types.ConfigDetails, options ...func(*Options)) (*types.
return nil, err
}
cfg.Filename = file.Filename
if opts.discardEnvFiles {
for i := range cfg.Services {
cfg.Services[i].EnvFile = nil
}
}

configs = append(configs, cfg)
}
Expand Down
32 changes: 32 additions & 0 deletions cli/compose/loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,38 @@ services:
assert.Check(t, is.DeepEqual([]string{"build", "links", "pid"}, unsupported))
}

func TestDiscardEnvFileOption(t *testing.T) {
dict, err := ParseYAML([]byte(`version: "3"
services:
web:
image: nginx
env_file:
- example1.env
- example2.env
`))
expectedEnvironmentMap := types.MappingWithEquals{
"FOO": strPtr("foo_from_env_file"),
"BAZ": strPtr("baz_from_env_file"),
"BAR": strPtr("bar_from_env_file_2"), // Original value is overwritten by example2.env
"QUX": strPtr("quz_from_env_file_2"),
}
assert.NilError(t, err)
configDetails := buildConfigDetails(dict, nil)

// Default behavior keeps the `env_file` entries
configWithEnvFiles, err := Load(configDetails)
assert.NilError(t, err)
assert.DeepEqual(t, configWithEnvFiles.Services[0].EnvFile, types.StringList{"example1.env",
"example2.env"})
assert.DeepEqual(t, configWithEnvFiles.Services[0].Environment, expectedEnvironmentMap)

// Custom behavior removes the `env_file` entries
configWithoutEnvFiles, err := Load(configDetails, WithDiscardEnvFiles)
assert.NilError(t, err)
assert.DeepEqual(t, configWithoutEnvFiles.Services[0].EnvFile, types.StringList(nil))
assert.DeepEqual(t, configWithoutEnvFiles.Services[0].Environment, expectedEnvironmentMap)
}

func TestBuildProperties(t *testing.T) {
dict, err := ParseYAML([]byte(`
version: "3"
Expand Down

0 comments on commit 83751b9

Please sign in to comment.