diff --git a/e2e/commands_test.go b/e2e/commands_test.go index 0154f1e40..69bc8763b 100644 --- a/e2e/commands_test.go +++ b/e2e/commands_test.go @@ -115,8 +115,8 @@ func checkFileWarning(t *testing.T, goldenFile, composeData string) { cmd.Dir = tmpDir.Path() cmd.Command = dockerCli.Command("app", "init", "app-test", "--compose-file", tmpDir.Join(internal.ComposeFileName)) - stdOut := icmd.RunCmd(cmd).Assert(t, icmd.Success).Combined() - golden.Assert(t, stdOut, goldenFile) + stdErr := icmd.RunCmd(cmd).Assert(t, icmd.Success).Stderr() + golden.Assert(t, stdErr, goldenFile) } func TestInitWarningEnvFiles(t *testing.T) { diff --git a/e2e/testdata/init-output-warning-multiple-envfiles.golden b/e2e/testdata/init-output-warning-multiple-envfiles.golden index d17b11d27..f3deff231 100644 --- a/e2e/testdata/init-output-warning-multiple-envfiles.golden +++ b/e2e/testdata/init-output-warning-multiple-envfiles.golden @@ -1,3 +1,2 @@ -"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" +nginx.env_file "myenv1.env" will not be copied into app-test.dockerapp. Please copy it manually and update the path accordingly in the compose file. +nginx.env_file "myenv2.env" will not be copied into app-test.dockerapp. Please copy it manually and update the path accordingly in the compose file. diff --git a/e2e/testdata/init-output-warning-single-envfile.golden b/e2e/testdata/init-output-warning-single-envfile.golden index ff1db6b8f..f34368714 100644 --- a/e2e/testdata/init-output-warning-single-envfile.golden +++ b/e2e/testdata/init-output-warning-single-envfile.golden @@ -1,2 +1 @@ -"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. -Created "app-test.dockerapp" +nginx.env_file "myenv1.env" will not be copied into app-test.dockerapp. Please copy it manually and update the path accordingly in the compose file. \ No newline at end of file diff --git a/internal/commands/init.go b/internal/commands/init.go index bcfa976eb..9a02f3ea3 100644 --- a/internal/commands/init.go +++ b/internal/commands/init.go @@ -21,7 +21,7 @@ func initCmd(dockerCli command.Cli) *cobra.Command { $ docker app init myapp --compose-file docker-compose.yml`, Args: cli.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - created, err := packager.Init(args[0], initComposeFile) + created, err := packager.Init(dockerCli.Err(), args[0], initComposeFile) if err != nil { return err } diff --git a/internal/packager/init.go b/internal/packager/init.go index 9de8823ae..1f3f02bce 100644 --- a/internal/packager/init.go +++ b/internal/packager/init.go @@ -3,6 +3,7 @@ package packager import ( "bytes" "fmt" + "io" "io/ioutil" "os" "os/user" @@ -27,7 +28,7 @@ import ( // Init is the entrypoint initialization function. // It generates a new application definition based on the provided parameters // and returns the path to the created application definition. -func Init(name string, composeFile string) (string, error) { +func Init(errWriter io.Writer, name string, composeFile string) (string, error) { if err := internal.ValidateAppName(name); err != nil { return "", err } @@ -48,7 +49,7 @@ func Init(name string, composeFile string) (string, error) { if composeFile == "" { err = initFromScratch(name) } else { - err = initFromComposeFile(name, composeFile) + err = initFromComposeFile(errWriter, name, composeFile) } if err != nil { return "", err @@ -95,16 +96,16 @@ func getEnvFiles(svcName string, envFileEntry interface{}) ([]string, error) { return envFiles, nil } -func checkEnvFiles(cfgMap map[string]interface{}) error { +func checkEnvFiles(errWriter io.Writer, appName string, cfgMap map[string]interface{}) error { services := cfgMap["services"] servicesMap, ok := services.(map[string]interface{}) if !ok { - return fmt.Errorf("could not process the yaml file") + return fmt.Errorf("invalid Compose file") } for svcName, svc := range servicesMap { svcContent, ok := svc.(map[string]interface{}) if !ok { - return fmt.Errorf("could not process the yaml file") + return fmt.Errorf("invalid service %q", svcName) } envFileEntry, ok := svcContent["env_file"] if !ok { @@ -112,12 +113,13 @@ func checkEnvFiles(cfgMap map[string]interface{}) error { } envFiles, err := getEnvFiles(svcName, envFileEntry) if err != nil { - return errors.Wrap(err, "could not process the yaml file") + return errors.Wrap(err, "invalid Compose file") } for _, envFilePath := range envFiles { - 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) + fmt.Fprintf(errWriter, + "%s.env_file %q will not be copied into %s.dockerapp. "+ + "Please copy it manually and update the path accordingly in the compose file.\n", + svcName, envFilePath, appName) } } return nil @@ -152,14 +154,14 @@ func getParamsFromDefaultEnvFile(composeFile string, composeRaw []byte) (map[str return params, needsFilling, nil } -func initFromComposeFile(name string, composeFile string) error { +func initFromComposeFile(errWriter io.Writer, name string, composeFile string) error { logrus.Debugf("Initializing from compose file %s", composeFile) dirName := internal.DirNameFromAppName(name) composeRaw, err := ioutil.ReadFile(composeFile) if err != nil { - return errors.Wrap(err, "failed to read compose file") + return errors.Wrapf(err, "failed to read compose file %q", composeFile) } cfgMap, err := composeloader.ParseYAML(composeRaw) if err != nil { @@ -168,7 +170,7 @@ func initFromComposeFile(name string, composeFile string) error { if err := checkComposeFileVersion(cfgMap); err != nil { return err } - if err := checkEnvFiles(cfgMap); err != nil { + if err := checkEnvFiles(errWriter, name, cfgMap); err != nil { return err } params, needsFilling, err := getParamsFromDefaultEnvFile(composeFile, composeRaw) diff --git a/internal/packager/init_test.go b/internal/packager/init_test.go index 767670bb0..b809b3fff 100644 --- a/internal/packager/init_test.go +++ b/internal/packager/init_test.go @@ -32,7 +32,7 @@ services: ) defer dir.Remove() - err := initFromComposeFile(dir.Join(appName), inputDir.Join(internal.ComposeFileName)) + err := initFromComposeFile(nil, dir.Join(appName), inputDir.Join(internal.ComposeFileName)) assert.NilError(t, err) manifest := fs.Expected( @@ -73,7 +73,7 @@ services: ) defer dir.Remove() - err := initFromComposeFile(dir.Join(appName), inputDir.Join(internal.ComposeFileName)) + err := initFromComposeFile(nil, dir.Join(appName), inputDir.Join(internal.ComposeFileName)) assert.NilError(t, err) const expectedParameters = `ports: @@ -108,7 +108,7 @@ services: } func TestInitFromInvalidComposeFile(t *testing.T) { - err := initFromComposeFile("my.dockerapp", "doesnotexist") + err := initFromComposeFile(nil, "my.dockerapp", "doesnotexist") assert.ErrorContains(t, err, "failed to read") } @@ -131,7 +131,7 @@ services: ) defer dir.Remove() - err := initFromComposeFile(dir.Join(appName), inputDir.Join(internal.ComposeFileName)) + err := initFromComposeFile(nil, dir.Join(appName), inputDir.Join(internal.ComposeFileName)) assert.ErrorContains(t, err, "unsupported Compose file version") } @@ -151,7 +151,7 @@ nginx: ) defer dir.Remove() - err := initFromComposeFile(dir.Join(appName), inputDir.Join(internal.ComposeFileName)) + err := initFromComposeFile(nil, dir.Join(appName), inputDir.Join(internal.ComposeFileName)) assert.ErrorContains(t, err, "unsupported Compose file version") }