Merge pull request #2060 from ulyssessouza/redundant_envfile

Add option to remove `env_file` entry once it's merged in the `environment` section
This commit is contained in:
Sebastiaan van Stijn 2019-09-23 18:00:41 +02:00 committed by GitHub
commit 83751b9781
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 0 deletions

View File

@ -32,6 +32,14 @@ type Options struct {
SkipInterpolation bool SkipInterpolation bool
// Interpolation options // Interpolation options
Interpolate *interp.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 // 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.
return nil, err return nil, err
} }
cfg.Filename = file.Filename cfg.Filename = file.Filename
if opts.discardEnvFiles {
for i := range cfg.Services {
cfg.Services[i].EnvFile = nil
}
}
configs = append(configs, cfg) configs = append(configs, cfg)
} }

View File

@ -759,6 +759,38 @@ services:
assert.Check(t, is.DeepEqual([]string{"build", "links", "pid"}, unsupported)) 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) { func TestBuildProperties(t *testing.T) {
dict, err := ParseYAML([]byte(` dict, err := ParseYAML([]byte(`
version: "3" version: "3"