Skip to content
This repository has been archived by the owner on Jun 13, 2021. It is now read-only.

Commit

Permalink
Add warnings for env_file entries not copied
Browse files Browse the repository at this point in the history
As 'env_file' entries are not taken in account,
we warn the user to copy that and fix it's
compose file.

Signed-off-by: Ulysses Souza <[email protected]>
  • Loading branch information
ulyssessouza committed Sep 10, 2019
1 parent daee953 commit 8b13bfc
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
57 changes: 57 additions & 0 deletions e2e/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,63 @@ func TestRenderFormatters(t *testing.T) {
golden.Assert(t, result.Stdout(), "expected-yaml-render.golden")
}

func TestInitWarningEnvFile(t *testing.T) {
cmd, cleanup := dockerCli.createTestCmd()
defer cleanup()

composeData := `version: "3.2"
services:
nginx:
image: nginx:latest
command: nginx $NGINX_ARGS ${NGINX_DRY_RUN}
env_file:
- myenv1.env
- myenv2.env`
meta := `# Version of the application
version: 0.1.0
# Name of the application
name: app-test
# A short description of the application
description: my cool app
# List of application maintainers with name and email for each
maintainers:
- name: dev1
email:
- name: dev2
email: [email protected]
`
envData := "# some comment\nNGINX_DRY_RUN=-t"
tmpDir := fs.NewDir(t, "app_input",
fs.WithFile(internal.ComposeFileName, composeData),
fs.WithFile(".env", envData),
fs.WithFile("myenv1.env", ""),
fs.WithFile("myenv2.env", ""),
)
defer tmpDir.Remove()

testAppName := "app-test"
dirName := internal.DirNameFromAppName(testAppName)

cmd.Dir = tmpDir.Path()
cmd.Command = dockerCli.Command("app",
"init", testAppName,
"--compose-file", tmpDir.Join(internal.ComposeFileName),
"--description", "my cool app",
"--maintainer", "dev1",
"--maintainer", "dev2:[email protected]")
stdOut := icmd.RunCmd(cmd).Assert(t, icmd.Success).Combined()
golden.Assert(t, stdOut, "init-output-warning-envfile.golden")

manifest := fs.Expected(
t,
fs.WithMode(0755),
fs.WithFile(internal.MetadataFileName, meta, fs.WithMode(0644)),
fs.WithFile(internal.ComposeFileName, composeData, fs.WithMode(0644)),
fs.WithFile(internal.ParametersFileName, "NGINX_ARGS: FILL ME\nNGINX_DRY_RUN: -t\n", fs.WithMode(0644)),
)
assert.Assert(t, fs.Equal(tmpDir.Join(dirName), manifest))
}

func TestInit(t *testing.T) {
cmd, cleanup := dockerCli.createTestCmd()
defer cleanup()
Expand Down
4 changes: 4 additions & 0 deletions e2e/testdata/init-output-warning-envfile.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"env_file: myenv1.env" entry of service "nginx" will NOT be copied automatically! Please, copy the file inside the application folder and update your compose file referring to it.
"env_file: myenv2.env" entry of service "nginx" will NOT be copied automatically! Please, copy the file inside the application folder and update your compose file referring to it.
Created "app-test.dockerapp"
You will need to edit parameters.yml to fill in default values.
40 changes: 40 additions & 0 deletions internal/packager/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,45 @@ func checkComposeFileVersion(compose map[string]interface{}) error {
return schema.Validate(compose, fmt.Sprintf("%v", version))
}

func getEnvFiles(svcName string, envFileEntry interface{}) []string {
var envFiles []string
switch envFileEntry.(type) {
case string:
envFiles = append(envFiles, envFileEntry.(string))
case []string:
envFiles = append(envFiles, envFileEntry.([]string)...)
case []interface{}:
for _, env := range envFileEntry.([]interface{}) {
envFiles = append(envFiles, env.(string))
}
default:
logrus.Warnf("unknown entries in 'env_file' for service %s -> %v\n",
svcName, envFileEntry)
return envFiles
}
return envFiles
}

func checkEnvFiles(cfgMap map[string]interface{}) {
services, ok := cfgMap["services"]
if !ok {
return
}
servicesMap := services.(map[string]interface{})
for svcName, svc := range servicesMap {
svcContent := svc.(map[string]interface{})
envFileEntry, ok := svcContent["env_file"]
if !ok {
return
}
for _, envFilePath := range getEnvFiles(svcName, envFileEntry) {
fmt.Printf("\"env_file: %s\" entry of service %q will NOT be copied automatically! "+
"Please, copy the file inside the application folder and update your compose file"+
" referring to it.\n", envFilePath, svcName)
}
}
}

func initFromComposeFile(name string, composeFile string) error {
logrus.Debugf("Initializing from compose file %s", composeFile)

Expand All @@ -138,6 +177,7 @@ func initFromComposeFile(name string, composeFile string) error {
if err := checkComposeFileVersion(cfgMap); err != nil {
return err
}
checkEnvFiles(cfgMap)
params := make(map[string]string)
envs, err := opts.ParseEnvFile(filepath.Join(filepath.Dir(composeFile), ".env"))
if err == nil {
Expand Down

0 comments on commit 8b13bfc

Please sign in to comment.