From bfb8c205c696e12c7e07bd72c91da7aa859f7b48 Mon Sep 17 00:00:00 2001 From: Vedant Koditkar Date: Tue, 24 Jan 2023 23:08:41 +0530 Subject: [PATCH] Add service-env-file option to pass service env file Signed-off-by: Vedant Koditkar --- cmd/compose/run.go | 48 +++++++++++++++++----------------- pkg/api/api.go | 4 +-- pkg/compose/run.go | 4 +-- pkg/compose/run_test.go | 58 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 28 deletions(-) create mode 100644 pkg/compose/run_test.go diff --git a/cmd/compose/run.go b/cmd/compose/run.go index fd18c94fb02..ff2657f79b5 100644 --- a/cmd/compose/run.go +++ b/cmd/compose/run.go @@ -36,28 +36,28 @@ import ( type runOptions struct { *composeOptions - Service string - Command []string - environment []string - Detach bool - Remove bool - noTty bool - tty bool - interactive bool - user string - workdir string - entrypoint string - entrypointCmd []string - labels []string - volumes []string - publish []string - useAliases bool - servicePorts bool - name string - noDeps bool - ignoreOrphans bool - quietPull bool - EnvFiles []string + Service string + Command []string + environment []string + Detach bool + Remove bool + noTty bool + tty bool + interactive bool + user string + workdir string + entrypoint string + entrypointCmd []string + labels []string + volumes []string + publish []string + useAliases bool + servicePorts bool + name string + noDeps bool + ignoreOrphans bool + quietPull bool + ServiceEnvFiles []string } func (opts runOptions) apply(project *types.Project) error { @@ -157,7 +157,7 @@ func runCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *co flags := cmd.Flags() flags.BoolVarP(&opts.Detach, "detach", "d", false, "Run container in background and print container ID") flags.StringArrayVarP(&opts.environment, "env", "e", []string{}, "Set environment variables") - flags.StringArrayVar(&opts.EnvFiles, "env-file", []string{}, "Read in a file of environment variables") + flags.StringArrayVar(&opts.ServiceEnvFiles, "service-env-file", []string{}, "Read in a file of environment variables") flags.StringArrayVarP(&opts.labels, "label", "l", []string{}, "Add or override a label") flags.BoolVar(&opts.Remove, "rm", false, "Automatically remove the container when it exits") flags.BoolVarP(&opts.noTty, "no-TTY", "T", !streams.Out().IsTerminal(), "Disable pseudo-TTY allocation (default: auto-detected).") @@ -235,7 +235,7 @@ func runRun(ctx context.Context, backend api.Service, project *types.Project, op NoDeps: opts.noDeps, Index: 0, QuietPull: opts.quietPull, - EnvFiles: opts.EnvFiles, + ServiceEnvFiles: opts.ServiceEnvFiles, } for i, service := range project.Services { diff --git a/pkg/api/api.go b/pkg/api/api.go index fcc36aab424..5820b8ca756 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -259,8 +259,8 @@ type RunOptions struct { // QuietPull makes the pulling process quiet QuietPull bool // used by exec - Index int - EnvFiles []string + Index int + ServiceEnvFiles []string } // EventsOptions group options of the Events API diff --git a/pkg/compose/run.go b/pkg/compose/run.go index 7bb96e269f9..28ecb5490ab 100644 --- a/pkg/compose/run.go +++ b/pkg/compose/run.go @@ -128,7 +128,7 @@ func applyRunOptions(project *types.Project, service *types.ServiceConfig, opts service.Labels = service.Labels.Add(k, v) } - if opts.EnvFiles != nil { - service.EnvFile = opts.EnvFiles + if opts.ServiceEnvFiles != nil { + service.EnvFile = append(service.EnvFile, opts.ServiceEnvFiles...) } } diff --git a/pkg/compose/run_test.go b/pkg/compose/run_test.go new file mode 100644 index 00000000000..4cb6f785ee7 --- /dev/null +++ b/pkg/compose/run_test.go @@ -0,0 +1,58 @@ +package compose + +import ( + "testing" + + "github.com/compose-spec/compose-go/types" + "github.com/docker/compose/v2/pkg/api" + "gotest.tools/v3/assert" +) + +func TestServiceEnvFiles(t *testing.T) { + + t.Run("Verify service.EnvFile shouldn't modify", func(t *testing.T) { + fooService := types.ServiceConfig{ + Name: "foo", + EnvFile: []string{}, + } + + project := types.Project{ + Name: "test-project", + Services: types.Services{ + fooService, + }, + } + + opts := api.RunOptions{ + ServiceEnvFiles: nil, + } + + applyRunOptions(&project, &fooService, opts) + + assert.Assert(t, len(fooService.EnvFile) == 0) + }) + + t.Run("Verify appends ServiceEnvFiles", func(t *testing.T) { + fooService := &types.ServiceConfig{ + Name: "foo", + EnvFile: []string{"./existing.env"}, + } + + project := types.Project{ + Name: "test-project", + Services: types.Services{ + *fooService, + }, + } + + opts := api.RunOptions{ + ServiceEnvFiles: []string{"./file.env"}, + } + + applyRunOptions(&project, fooService, opts) + + assert.Assert(t, len(fooService.EnvFile) == 2) + assert.Assert(t, fooService.EnvFile[0] == "./existing.env") + assert.Assert(t, fooService.EnvFile[1] == "./file.env") + }) +}