From f159bc49015dfc75386a0b613e6882e0aef0963c Mon Sep 17 00:00:00 2001 From: Ulysses Souza Date: Mon, 3 May 2021 21:32:51 -0300 Subject: [PATCH] Support bypass envvars Signed-off-by: Ulysses Souza --- api/compose/api.go | 16 ---------------- api/compose/api_test.go | 3 ++- local/compose/run.go | 11 ++++++++--- 3 files changed, 10 insertions(+), 20 deletions(-) diff --git a/api/compose/api.go b/api/compose/api.go index eae71aa61..5b210adf3 100644 --- a/api/compose/api.go +++ b/api/compose/api.go @@ -245,22 +245,6 @@ func (e Event) String() string { } -// EnvironmentMap return RunOptions.Environment as a MappingWithEquals -func (opts *RunOptions) EnvironmentMap() types.MappingWithEquals { - environment := types.MappingWithEquals{} - for _, s := range opts.Environment { - parts := strings.SplitN(s, "=", 2) - key := parts[0] - switch { - case len(parts) == 1: - environment[key] = nil - default: - environment[key] = &parts[1] - } - } - return environment -} - // ListOptions group options of the ls API type ListOptions struct { All bool diff --git a/api/compose/api_test.go b/api/compose/api_test.go index ddbe25d6a..fca9d0fcc 100644 --- a/api/compose/api_test.go +++ b/api/compose/api_test.go @@ -19,6 +19,7 @@ package compose import ( "testing" + "github.com/compose-spec/compose-go/types" "gotest.tools/v3/assert" ) @@ -30,7 +31,7 @@ func TestRunOptionsEnvironmentMap(t *testing.T) { "QIX", }, } - env := opts.EnvironmentMap() + env := types.NewMappingWithEquals(opts.Environment) assert.Equal(t, *env["FOO"], "BAR") assert.Equal(t, *env["ZOT"], "") assert.Check(t, env["QIX"] == nil) diff --git a/local/compose/run.go b/local/compose/run.go index aad04d7b4..bc02d9fda 100644 --- a/local/compose/run.go +++ b/local/compose/run.go @@ -42,7 +42,7 @@ func (s *composeService) RunOneOffContainer(ctx context.Context, project *types. return 0, err } - applyRunOptions(&service, opts) + applyRunOptions(project, &service, opts) slug := moby.GenerateRandomID() if service.ContainerName == "" { @@ -107,7 +107,7 @@ func (s *composeService) RunOneOffContainer(ctx context.Context, project *types. } -func applyRunOptions(service *types.ServiceConfig, opts compose.RunOptions) { +func applyRunOptions(project *types.Project, service *types.ServiceConfig, opts compose.RunOptions) { service.Tty = opts.Tty service.ContainerName = opts.Name @@ -124,7 +124,12 @@ func applyRunOptions(service *types.ServiceConfig, opts compose.RunOptions) { service.Entrypoint = opts.Entrypoint } if len(opts.Environment) > 0 { - service.Environment.OverrideBy(opts.EnvironmentMap()) + env := types.NewMappingWithEquals(opts.Environment) + projectEnv := env.Resolve(func(s string) (string, bool) { + v, ok := project.Environment[s] + return v, ok + }).RemoveEmpty() + service.Environment.OverrideBy(projectEnv) } for k, v := range opts.Labels { service.Labels.Add(k, v)