Add envFile arguments to run command

Signed-off-by: Ulysses Souza <ulyssessouza@gmail.com>
This commit is contained in:
Ulysses Souza 2020-10-01 06:08:25 +02:00
parent 4f48318332
commit 2e899202ec
6 changed files with 42 additions and 1 deletions

View File

@ -52,6 +52,7 @@ func Command(contextType string) *cobra.Command {
cmd.Flags().Float64Var(&opts.Cpus, "cpus", 1., "Number of CPUs")
cmd.Flags().VarP(&opts.Memory, "memory", "m", "Memory limit")
cmd.Flags().StringArrayVarP(&opts.Environment, "env", "e", []string{}, "Set environment variables")
cmd.Flags().StringArrayVar(&opts.EnvironmentFiles, "envFile", []string{}, "Path to environment files to be translated as environment variables")
cmd.Flags().StringVarP(&opts.RestartPolicyCondition, "restart", "", containers.RestartPolicyNone, "Restart policy to apply when a container exits")
if contextType == store.AciContextType {

View File

@ -23,6 +23,8 @@ import (
"gotest.tools/v3/assert"
"gotest.tools/v3/golden"
"github.com/docker/compose-cli/cli/options/run"
)
func TestHelp(t *testing.T) {
@ -40,3 +42,24 @@ func TestHelpNoDomainFlag(t *testing.T) {
_ = c.Help()
assert.Assert(t, !strings.Contains(b.String(), "domainname"))
}
func TestRunEnvironmentFiles(t *testing.T) {
runOpts := run.Opts{
Environment: []string{
"VAR=1",
},
EnvironmentFiles: []string{
"testdata/runtest1.env",
"testdata/runtest2.env",
},
}
containerConfig, err := runOpts.ToContainerConfig("test")
assert.NilError(t, err)
assert.DeepEqual(t, containerConfig.Environment, []string{
"VAR=1",
"FIRST_VAR=\"firstValue\"",
"SECOND_VAR=secondValue",
"THIRD_VAR=2",
"FOURTH_VAR=fourthValue",
})
}

View File

@ -8,6 +8,7 @@ Flags:
-d, --detach Run container in background and print container ID
--domainname string Container NIS domain name
-e, --env stringArray Set environment variables
--envFile stringArray Path to environment files to be translated as environment variables
-l, --label stringArray Set meta data on a container
-m, --memory bytes Memory limit
--name string Assign a name to the container

2
cli/cmd/run/testdata/runtest1.env vendored Normal file
View File

@ -0,0 +1,2 @@
FIRST_VAR="firstValue"
SECOND_VAR=secondValue

2
cli/cmd/run/testdata/runtest2.env vendored Normal file
View File

@ -0,0 +1,2 @@
THIRD_VAR=2
FOURTH_VAR=fourthValue

View File

@ -21,6 +21,8 @@ import (
"strconv"
"strings"
"github.com/docker/cli/opts"
"github.com/docker/compose-cli/utils"
"github.com/docker/docker/pkg/namesgenerator"
@ -40,6 +42,7 @@ type Opts struct {
Memory formatter.MemBytes
Detach bool
Environment []string
EnvironmentFiles []string
RestartPolicyCondition string
DomainName string
}
@ -65,6 +68,15 @@ func (r *Opts) ToContainerConfig(image string) (containers.ContainerConfig, erro
return containers.ContainerConfig{}, err
}
envVars := r.Environment
for _, f := range r.EnvironmentFiles {
vars, err := opts.ParseEnvFile(f)
if err != nil {
return containers.ContainerConfig{}, err
}
envVars = append(envVars, vars...)
}
return containers.ContainerConfig{
ID: r.Name,
Image: image,
@ -73,7 +85,7 @@ func (r *Opts) ToContainerConfig(image string) (containers.ContainerConfig, erro
Volumes: r.Volumes,
MemLimit: r.Memory,
CPULimit: r.Cpus,
Environment: r.Environment,
Environment: envVars,
RestartPolicyCondition: restartPolicy,
DomainName: r.DomainName,
}, nil