Skip to content

Commit 1be5aca

Browse files
authored
Merge pull request #449 from ndeloof/env_override_include
2 parents cda4df5 + 15210d9 commit 1be5aca

File tree

6 files changed

+46
-12
lines changed

6 files changed

+46
-12
lines changed

cli/options.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ type ProjectOptions struct {
6666
// NOTE: For security, the loader does not automatically expose any
6767
// process environment variables. For convenience, WithOsEnv can be
6868
// used if appropriate.
69-
Environment map[string]string
69+
Environment types.Mapping
7070

7171
// EnvFiles are file paths to ".env" files with additional environment
7272
// variable data.
@@ -256,11 +256,7 @@ func WithDotEnv(o *ProjectOptions) error {
256256
if err != nil {
257257
return err
258258
}
259-
for k, v := range envMap {
260-
if _, set := o.Environment[k]; !set {
261-
o.Environment[k] = v
262-
}
263-
}
259+
o.Environment.Merge(envMap)
264260
return nil
265261
}
266262

cli/options_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"path/filepath"
2323
"testing"
2424

25+
"github.com/compose-spec/compose-go/types"
2526
"gotest.tools/v3/assert"
2627

2728
"github.com/compose-spec/compose-go/consts"
@@ -314,13 +315,13 @@ func TestEnvVariablePrecedence(t *testing.T) {
314315
name string
315316
dotEnv string
316317
osEnv []string
317-
expected map[string]string
318+
expected types.Mapping
318319
}{
319320
{
320321
"no value set in environment",
321322
"FOO=foo\nBAR=${FOO}",
322323
nil,
323-
map[string]string{
324+
types.Mapping{
324325
"FOO": "foo",
325326
"BAR": "foo",
326327
},
@@ -329,7 +330,7 @@ func TestEnvVariablePrecedence(t *testing.T) {
329330
"conflict with value set in environment",
330331
"FOO=foo\nBAR=${FOO}",
331332
[]string{"FOO=zot"},
332-
map[string]string{
333+
types.Mapping{
333334
"FOO": "zot",
334335
"BAR": "zot",
335336
},

loader/include.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,15 @@ func loadInclude(ctx context.Context, filename string, configDetails types.Confi
7373
loadOptions.SkipNormalization = true
7474
loadOptions.SkipConsistencyCheck = true
7575

76-
env, err := dotenv.GetEnvFromFile(configDetails.Environment, r.ProjectDirectory, r.EnvFile)
76+
envFromFile, err := dotenv.GetEnvFromFile(configDetails.Environment, r.ProjectDirectory, r.EnvFile)
7777
if err != nil {
7878
return nil, nil, err
7979
}
8080

8181
config := types.ConfigDetails{
8282
WorkingDir: r.ProjectDirectory,
8383
ConfigFiles: types.ToConfigFiles(r.Path),
84-
Environment: env,
84+
Environment: configDetails.Environment.Clone().Merge(envFromFile),
8585
}
8686
loadOptions.Interpolate = &interp.Options{
8787
Substitute: options.Interpolate.Substitute,

loader/loader_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2555,6 +2555,25 @@ services:
25552555
},
25562556
})
25572557
assert.NilError(t, err)
2558+
2559+
p, err = Load(buildConfigDetails(`
2560+
name: 'test-include'
2561+
2562+
include:
2563+
- path: ./testdata/subdir/compose-test-extends-imported.yaml
2564+
env_file: ./testdata/subdir/extra.env
2565+
2566+
services:
2567+
foo:
2568+
image: busybox
2569+
depends_on:
2570+
- imported
2571+
`, map[string]string{"SOURCE": "override"}), func(options *Options) {
2572+
options.SkipNormalization = true
2573+
options.ResolvePaths = true
2574+
})
2575+
assert.NilError(t, err)
2576+
assert.Equal(t, p.Services[1].ContainerName, "override")
25582577
}
25592578

25602579
func TestLoadWithIncludeCycle(t *testing.T) {

types/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type ConfigDetails struct {
3434
Version string
3535
WorkingDir string
3636
ConfigFiles []ConfigFile
37-
Environment map[string]string
37+
Environment Mapping
3838
}
3939

4040
// LookupEnv provides a lookup function for environment variables

types/types.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,24 @@ func (m Mapping) Resolve(s string) (string, bool) {
516516
return v, ok
517517
}
518518

519+
func (m Mapping) Clone() Mapping {
520+
clone := Mapping{}
521+
for k, v := range m {
522+
clone[k] = v
523+
}
524+
return clone
525+
}
526+
527+
// Merge adds all values from second mapping which are not already defined
528+
func (m Mapping) Merge(o Mapping) Mapping {
529+
for k, v := range o {
530+
if _, set := m[k]; !set {
531+
m[k] = v
532+
}
533+
}
534+
return m
535+
}
536+
519537
// Labels is a mapping type for labels
520538
type Labels map[string]string
521539

0 commit comments

Comments
 (0)