From 8b13bfcf62a540f7437c3b363dbf7aa7afd7afe5 Mon Sep 17 00:00:00 2001 From: Ulysses Souza Date: Tue, 10 Sep 2019 18:13:44 +0200 Subject: [PATCH] Add warnings for env_file entries not copied 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 --- e2e/commands_test.go | 57 +++++++++++++++++++ .../init-output-warning-envfile.golden | 4 ++ internal/packager/init.go | 40 +++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 e2e/testdata/init-output-warning-envfile.golden diff --git a/e2e/commands_test.go b/e2e/commands_test.go index fdf283967..d152e5a40 100644 --- a/e2e/commands_test.go +++ b/e2e/commands_test.go @@ -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: dev2@example.com +` + 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:dev2@example.com") + 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() diff --git a/e2e/testdata/init-output-warning-envfile.golden b/e2e/testdata/init-output-warning-envfile.golden new file mode 100644 index 000000000..b0be9acdd --- /dev/null +++ b/e2e/testdata/init-output-warning-envfile.golden @@ -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. diff --git a/internal/packager/init.go b/internal/packager/init.go index 221847e48..d218c2fff 100644 --- a/internal/packager/init.go +++ b/internal/packager/init.go @@ -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) @@ -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 {