Add option remove `env_file` entry after merged

This avoids having a redundant `env_file` entry
output when rendering the compose file

Signed-off-by: Ulysses Souza <ulysses.souza@docker.com>
This commit is contained in:
Ulysses Souza 2019-08-22 17:36:12 +02:00
parent f429e4bfd9
commit 821f5ecf53
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"