Skip to content

Commit

Permalink
Add service-env-file option to pass service env file
Browse files Browse the repository at this point in the history
Signed-off-by: Vedant Koditkar <[email protected]>
  • Loading branch information
KoditkarVedant committed Jan 24, 2023
1 parent d6e806a commit bfb8c20
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 28 deletions.
48 changes: 24 additions & 24 deletions cmd/compose/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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).")
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/compose/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
}
58 changes: 58 additions & 0 deletions pkg/compose/run_test.go
Original file line number Diff line number Diff line change
@@ -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")
})
}

0 comments on commit bfb8c20

Please sign in to comment.