diff --git a/acceptance/bundle/exec/basic/output.txt b/acceptance/bundle/exec/basic/output.txt index 01d3479f0..3584582db 100644 --- a/acceptance/bundle/exec/basic/output.txt +++ b/acceptance/bundle/exec/basic/output.txt @@ -7,3 +7,8 @@ hello, world >>> [CLI] bundle exec -- echo --help --help + +>>> [CLI] bundle exec -- bash -c exit 5 +Error: Running "bash -c exit 5" failed with exit code: 5 + +Exit code: 1 diff --git a/acceptance/bundle/exec/basic/script b/acceptance/bundle/exec/basic/script index fb07fbfb6..41c57725f 100644 --- a/acceptance/bundle/exec/basic/script +++ b/acceptance/bundle/exec/basic/script @@ -4,3 +4,6 @@ trace $CLI bundle exec -- pwd # The CLI should not parse the --help flag and should pass it as is to echo. trace $CLI bundle exec -- echo --help + +# The error message should include the exit code. +trace $CLI bundle exec -- bash -c "exit 5" diff --git a/acceptance/bundle/exec/databricks-cli/target-is-passed/output.txt b/acceptance/bundle/exec/databricks-cli/target-is-passed/output.txt index 48fcf8a61..a17f177be 100644 --- a/acceptance/bundle/exec/databricks-cli/target-is-passed/output.txt +++ b/acceptance/bundle/exec/databricks-cli/target-is-passed/output.txt @@ -13,7 +13,7 @@ >>> errcode [CLI] bundle exec -t pat -- databricks current-user me -t oauth Error: cannot resolve bundle auth configuration: validate: more than one authorization method configured: oauth and pat. Config: host=[DATABRICKS_URL], token=***, client_id=client_id, databricks_cli_path=[CLI]. Env: DATABRICKS_HOST, DATABRICKS_TOKEN, DATABRICKS_CLI_PATH -Error: Command exited with code: 1 +Error: Running "databricks current-user me -t oauth" failed with exit code: 1 Exit code: 1 diff --git a/cmd/bundle/exec.go b/cmd/bundle/exec.go index 39224955a..bd96b41de 100644 --- a/cmd/bundle/exec.go +++ b/cmd/bundle/exec.go @@ -13,6 +13,15 @@ import ( "github.com/spf13/cobra" ) +type exitCodeErr struct { + exitCode int + args []string +} + +func (e *exitCodeErr) Error() string { + return fmt.Sprintf("Running %q failed with exit code: %d", strings.Join(e.args, " "), e.exitCode) +} + func newExecCommand() *cobra.Command { execCmd := &cobra.Command{ Use: "exec", @@ -110,11 +119,16 @@ Examples: }() // Wait for the command to finish. - // TODO: Pretty exit codes? - // TODO: Make CLI return the same exit codes? It has to, that's a requirement. err = childCmd.Wait() if exitErr, ok := err.(*exec.ExitError); ok { - return fmt.Errorf("Command exited with code: %d", exitErr.ExitCode()) + // We don't propagate the exit code as is because exit codes for + // the CLI have not been standardized yet. At some point in the + // future we might want to associate specific exit codes with + // specific classes of errors. + return &exitCodeErr{ + exitCode: exitErr.ExitCode(), + args: args, + } } if err != nil { return fmt.Errorf("Error waiting for command: %w", err)