diff --git a/cmd/score-compose/main.go b/cmd/score-compose/main.go index a07048a..99408d1 100644 --- a/cmd/score-compose/main.go +++ b/cmd/score-compose/main.go @@ -16,7 +16,7 @@ import ( func main() { if err := command.Execute(); err != nil { - fmt.Fprintln(os.Stderr, err) + _, _ = fmt.Fprintln(os.Stderr, "Error: "+err.Error()) os.Exit(1) } } diff --git a/e2e-tests/resources/outputs/run --verbose-output.txt b/e2e-tests/resources/outputs/run --verbose-output.txt index ed7891e..45c6c4e 100644 --- a/e2e-tests/resources/outputs/run --verbose-output.txt +++ b/e2e-tests/resources/outputs/run --verbose-output.txt @@ -1,17 +1 @@ -'./score.yaml'... -Error: open ./score.yaml: no such file or directory -Usage: - score-compose run [flags] - -Flags: - --build string Replaces 'image' name with compose 'build' instruction - --env-file string Location to store sample .env file - -f, --file string Source SCORE file (default "./score.yaml") - -h, --help help for run - -o, --output string Output file - --overrides string Overrides SCORE file (default "./overrides.score.yaml") - -p, --property stringArray Overrides selected property value - --skip-validation DEPRECATED: Disables Score file schema validation - --verbose Enable diagnostic messages (written to STDERR) - -open ./score.yaml: no such file or directory \ No newline at end of file +Error: open ./score.yaml: no such file or directory \ No newline at end of file diff --git a/e2e-tests/resources/outputs/unknown-output.txt b/e2e-tests/resources/outputs/unknown-output.txt index 04a2e28..006fe2c 100644 --- a/e2e-tests/resources/outputs/unknown-output.txt +++ b/e2e-tests/resources/outputs/unknown-output.txt @@ -1,3 +1 @@ -Error: unknown command "unknown" for "score-compose" -Run 'score-compose --help' for usage. -unknown command "unknown" for "score-compose" \ No newline at end of file +Error: unknown command "unknown" for "score-compose" \ No newline at end of file diff --git a/internal/command/root.go b/internal/command/root.go index 21e9059..0f85e82 100644 --- a/internal/command/root.go +++ b/internal/command/root.go @@ -22,7 +22,8 @@ var ( Long: `SCORE is a specification for defining environment agnostic configuration for cloud based workloads. This tool produces a docker-compose configuration file from the SCORE specification. Complete documentation is available at https://score.dev`, - Version: fmt.Sprintf("%s (build: %s; sha: %s)", version.Version, version.BuildTime, version.GitSHA), + Version: fmt.Sprintf("%s (build: %s; sha: %s)", version.Version, version.BuildTime, version.GitSHA), + SilenceErrors: true, } ) diff --git a/internal/command/run.go b/internal/command/run.go index 356454b..a9f0d7e 100644 --- a/internal/command/run.go +++ b/internal/command/run.go @@ -63,12 +63,10 @@ func init() { } var runCmd = &cobra.Command{ - Use: "run [--file=score.yaml] [--output=compose.yaml]", - Short: "Translate the SCORE file to docker-compose configuration", - PreRun: func(cmd *cobra.Command, args []string) { - cmd.SilenceUsage = false - }, - RunE: run, + Use: "run [--file=score.yaml] [--output=compose.yaml]", + Short: "Translate the SCORE file to docker-compose configuration", + RunE: run, + SilenceErrors: true, } func run(cmd *cobra.Command, args []string) error { diff --git a/internal/command/run_test.go b/internal/command/run_test.go index 6e3efed..ba2493b 100644 --- a/internal/command/run_test.go +++ b/internal/command/run_test.go @@ -17,14 +17,19 @@ import ( // executeAndResetCommand is a test helper that runs and then resets a command for executing in another test. func executeAndResetCommand(ctx context.Context, cmd *cobra.Command, args []string) (string, string, error) { beforeOut, beforeErr := cmd.OutOrStderr(), cmd.ErrOrStderr() - defer cmd.SetOut(beforeOut) - defer cmd.SetErr(beforeErr) + defer func() { + cmd.SetOut(beforeOut) + cmd.SetErr(beforeErr) + }() + nowOut, nowErr := new(bytes.Buffer), new(bytes.Buffer) cmd.SetOut(nowOut) cmd.SetErr(nowErr) cmd.SetArgs(args) subCmd, err := cmd.ExecuteContextC(ctx) subCmd.SetContext(nil) + subCmd.SilenceUsage = false + subCmd.SilenceErrors = false subCmd.Flags().VisitAll(func(f *pflag.Flag) { _ = f.Value.Set(f.DefValue) }) @@ -147,7 +152,7 @@ containers: stdout, stderr, err := executeAndResetCommand(context.Background(), rootCmd, []string{"run", "--file", filepath.Join(td, "score.yaml"), "--output", filepath.Join(td, "compose.yaml")}) assert.EqualError(t, err, "building docker-compose configuration: can't mount named volume with sub path '/sub/path': not supported") assert.Equal(t, "", stdout) - assert.Contains(t, stderr, "Error: building docker-compose configuration:") + assert.Equal(t, "", stderr) } func TestInvalidWorkloadName(t *testing.T) { @@ -163,5 +168,5 @@ containers: stdout, stderr, err := executeAndResetCommand(context.Background(), rootCmd, []string{"run", "--file", filepath.Join(td, "score.yaml")}) assert.EqualError(t, err, "validating workload spec: jsonschema: '/metadata/name' does not validate with https://score.dev/schemas/score#/properties/metadata/properties/name/pattern: does not match pattern '^[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$'") assert.Equal(t, "", stdout) - assert.Contains(t, stderr, "Error: validating workload spec:") + assert.Equal(t, "", stderr) }