mirror of https://github.com/docker/compose.git
Add envFile arguments to run command
Signed-off-by: Ulysses Souza <ulyssessouza@gmail.com>
This commit is contained in:
parent
4f48318332
commit
2e899202ec
|
@ -52,6 +52,7 @@ func Command(contextType string) *cobra.Command {
|
||||||
cmd.Flags().Float64Var(&opts.Cpus, "cpus", 1., "Number of CPUs")
|
cmd.Flags().Float64Var(&opts.Cpus, "cpus", 1., "Number of CPUs")
|
||||||
cmd.Flags().VarP(&opts.Memory, "memory", "m", "Memory limit")
|
cmd.Flags().VarP(&opts.Memory, "memory", "m", "Memory limit")
|
||||||
cmd.Flags().StringArrayVarP(&opts.Environment, "env", "e", []string{}, "Set environment variables")
|
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")
|
cmd.Flags().StringVarP(&opts.RestartPolicyCondition, "restart", "", containers.RestartPolicyNone, "Restart policy to apply when a container exits")
|
||||||
|
|
||||||
if contextType == store.AciContextType {
|
if contextType == store.AciContextType {
|
||||||
|
|
|
@ -23,6 +23,8 @@ import (
|
||||||
|
|
||||||
"gotest.tools/v3/assert"
|
"gotest.tools/v3/assert"
|
||||||
"gotest.tools/v3/golden"
|
"gotest.tools/v3/golden"
|
||||||
|
|
||||||
|
"github.com/docker/compose-cli/cli/options/run"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestHelp(t *testing.T) {
|
func TestHelp(t *testing.T) {
|
||||||
|
@ -40,3 +42,24 @@ func TestHelpNoDomainFlag(t *testing.T) {
|
||||||
_ = c.Help()
|
_ = c.Help()
|
||||||
assert.Assert(t, !strings.Contains(b.String(), "domainname"))
|
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",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ Flags:
|
||||||
-d, --detach Run container in background and print container ID
|
-d, --detach Run container in background and print container ID
|
||||||
--domainname string Container NIS domain name
|
--domainname string Container NIS domain name
|
||||||
-e, --env stringArray Set environment variables
|
-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
|
-l, --label stringArray Set meta data on a container
|
||||||
-m, --memory bytes Memory limit
|
-m, --memory bytes Memory limit
|
||||||
--name string Assign a name to the container
|
--name string Assign a name to the container
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
FIRST_VAR="firstValue"
|
||||||
|
SECOND_VAR=secondValue
|
|
@ -0,0 +1,2 @@
|
||||||
|
THIRD_VAR=2
|
||||||
|
FOURTH_VAR=fourthValue
|
|
@ -21,6 +21,8 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/docker/cli/opts"
|
||||||
|
|
||||||
"github.com/docker/compose-cli/utils"
|
"github.com/docker/compose-cli/utils"
|
||||||
|
|
||||||
"github.com/docker/docker/pkg/namesgenerator"
|
"github.com/docker/docker/pkg/namesgenerator"
|
||||||
|
@ -40,6 +42,7 @@ type Opts struct {
|
||||||
Memory formatter.MemBytes
|
Memory formatter.MemBytes
|
||||||
Detach bool
|
Detach bool
|
||||||
Environment []string
|
Environment []string
|
||||||
|
EnvironmentFiles []string
|
||||||
RestartPolicyCondition string
|
RestartPolicyCondition string
|
||||||
DomainName string
|
DomainName string
|
||||||
}
|
}
|
||||||
|
@ -65,6 +68,15 @@ func (r *Opts) ToContainerConfig(image string) (containers.ContainerConfig, erro
|
||||||
return containers.ContainerConfig{}, err
|
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{
|
return containers.ContainerConfig{
|
||||||
ID: r.Name,
|
ID: r.Name,
|
||||||
Image: image,
|
Image: image,
|
||||||
|
@ -73,7 +85,7 @@ func (r *Opts) ToContainerConfig(image string) (containers.ContainerConfig, erro
|
||||||
Volumes: r.Volumes,
|
Volumes: r.Volumes,
|
||||||
MemLimit: r.Memory,
|
MemLimit: r.Memory,
|
||||||
CPULimit: r.Cpus,
|
CPULimit: r.Cpus,
|
||||||
Environment: r.Environment,
|
Environment: envVars,
|
||||||
RestartPolicyCondition: restartPolicy,
|
RestartPolicyCondition: restartPolicy,
|
||||||
DomainName: r.DomainName,
|
DomainName: r.DomainName,
|
||||||
}, nil
|
}, nil
|
||||||
|
|
Loading…
Reference in New Issue