From 2cb1a7c785210c4318d914f03242d8d8d2c538fa Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Wed, 21 Dec 2022 09:59:34 +0100 Subject: [PATCH] resolve environment incrementaly, including values loaded from previous files in the list Signed-off-by: Nicolas De Loof --- loader/loader.go | 8 +++++++- types/types.go | 4 ++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/loader/loader.go b/loader/loader.go index 4449ba022..64b691ba5 100644 --- a/loader/loader.go +++ b/loader/loader.go @@ -629,6 +629,12 @@ func convertVolumePath(volume types.ServiceVolumeConfig) types.ServiceVolumeConf func resolveEnvironment(serviceConfig *types.ServiceConfig, workingDir string, lookupEnv template.Mapping) error { environment := types.MappingWithEquals{} + var resolve dotenv.LookupFn = func(s string) (string, bool) { + if v, ok := environment[s]; ok && v != nil { + return *v, true + } + return lookupEnv(s) + } if len(serviceConfig.EnvFile) > 0 { if serviceConfig.Environment == nil { @@ -649,7 +655,7 @@ func resolveEnvironment(serviceConfig *types.ServiceConfig, workingDir string, l // Do not defer to avoid it inside a loop file.Close() //nolint:errcheck - fileVars, err := dotenv.ParseWithLookup(bytes.NewBuffer(b), dotenv.LookupFn(lookupEnv)) + fileVars, err := dotenv.ParseWithLookup(bytes.NewBuffer(b), resolve) if err != nil { return err } diff --git a/types/types.go b/types/types.go index 156cacd0e..e5a5fcd7b 100644 --- a/types/types.go +++ b/types/types.go @@ -356,12 +356,12 @@ type ThrottleDevice struct { // ShellCommand is a string or list of string args. // // When marshaled to YAML, nil command fields will be omitted if `omitempty` -// is specified as a struct tag. Explicitly empty commands (i.e. `[]` or `''`) +// is specified as a struct tag. Explicitly empty commands (i.e. `[]` or `”`) // will serialize to an empty array (`[]`). // // When marshaled to JSON, the `omitempty` struct must NOT be specified. // If the command field is nil, it will be serialized as `null`. -// Explicitly empty commands (i.e. `[]` or `''`) will serialize to an empty +// Explicitly empty commands (i.e. `[]` or `”`) will serialize to an empty // array (`[]`). // // The distinction between nil and explicitly empty is important to distinguish