Skip to content

Commit

Permalink
chore: improved error responses from cli
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Meier <[email protected]>
  • Loading branch information
astromechza committed Feb 26, 2024
1 parent 0da2e2a commit b577a9e
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 32 deletions.
2 changes: 1 addition & 1 deletion cmd/score-compose/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
18 changes: 1 addition & 17 deletions e2e-tests/resources/outputs/run --verbose-output.txt
Original file line number Diff line number Diff line change
@@ -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
Error: open ./score.yaml: no such file or directory
4 changes: 1 addition & 3 deletions e2e-tests/resources/outputs/unknown-output.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
Error: unknown command "unknown" for "score-compose"
Run 'score-compose --help' for usage.
unknown command "unknown" for "score-compose"
Error: unknown command "unknown" for "score-compose"
3 changes: 2 additions & 1 deletion internal/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
)

Expand Down
10 changes: 4 additions & 6 deletions internal/command/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 9 additions & 4 deletions internal/command/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
}

0 comments on commit b577a9e

Please sign in to comment.