Repository: docker/compose-cli
Pull request #704 (2020-10-01 04:21:14)

Add envFile arguments to run command
**What I did**
Add `envFile` arguments to run command

**Related issue**
Resolves https://github.com/docker/compose-cli/issues/365

**(not mandatory) A picture of a cute animal, if possible in relation with what you did**
![environment](https://user-images.githubusercontent.com/373485/94767562-faaf9000-03ac-11eb-8370-e55862f7f148.jpg)




Patch:
diff --git a/cli/cmd/run/run.go b/cli/cmd/run/run.go
index 0c1559b3..4bbf9ad0 100644
--- a/cli/cmd/run/run.go
+++ b/cli/cmd/run/run.go
@@ -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 {
diff --git a/cli/options/run/opts.go b/cli/options/run/opts.go
index 9cdd2ebe..cf862a74 100644
--- a/cli/options/run/opts.go
+++ b/cli/options/run/opts.go
@@ -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


Test patch:
diff --git a/cli/cmd/run/run_test.go b/cli/cmd/run/run_test.go
index 78b23c54..789fe400 100644
--- a/cli/cmd/run/run_test.go
+++ b/cli/cmd/run/run_test.go
@@ -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",
+	})
+}
diff --git a/cli/cmd/run/testdata/run-help.golden b/cli/cmd/run/testdata/run-help.golden
index 178c63b0..71692ccd 100644
--- a/cli/cmd/run/testdata/run-help.golden
+++ b/cli/cmd/run/testdata/run-help.golden
@@ -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
diff --git a/cli/cmd/run/testdata/runtest1.env b/cli/cmd/run/testdata/runtest1.env
new file mode 100644
index 00000000..495661e3
--- /dev/null
+++ b/cli/cmd/run/testdata/runtest1.env
@@ -0,0 +1,2 @@
+FIRST_VAR="firstValue"
+SECOND_VAR=secondValue
diff --git a/cli/cmd/run/testdata/runtest2.env b/cli/cmd/run/testdata/runtest2.env
new file mode 100644
index 00000000..d6182fbf
--- /dev/null
+++ b/cli/cmd/run/testdata/runtest2.env
@@ -0,0 +1,2 @@
+THIRD_VAR=2
+FOURTH_VAR=fourthValue