Skip to content

Commit

Permalink
fix: add custom error printing for Zarf commands (#2575)
Browse files Browse the repository at this point in the history
## Description

This change adds some custom logic for printing returned errors from
Cobra commands. This will allow embedded tools like Helm to behave the
same as the original tools. While the Zarf commands will print errors in
the same expected error format.

## Related Issue

Relates to #2567

## Checklist before merging

- [x] Test, docs, adr added or updated as needed
- [x] [Contributor Guide
Steps](https://github.com/defenseunicorns/zarf/blob/main/.github/CONTRIBUTING.md#developer-workflow)
followed
  • Loading branch information
phillebaba committed Jun 3, 2024
1 parent 0c02643 commit 90f038e
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/defenseunicorns/zarf/src/pkg/layout"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/types"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -44,9 +45,11 @@ var rootCmd = &cobra.Command{

common.SetupCLI()
},
Short: lang.RootCmdShort,
Long: lang.RootCmdLong,
Args: cobra.MaximumNArgs(1),
Short: lang.RootCmdShort,
Long: lang.RootCmdLong,
Args: cobra.MaximumNArgs(1),
SilenceUsage: true,
SilenceErrors: true,
Run: func(cmd *cobra.Command, args []string) {
zarfLogo := message.GetLogo()
_, _ = fmt.Fprintln(os.Stderr, zarfLogo)
Expand All @@ -65,7 +68,17 @@ var rootCmd = &cobra.Command{

// Execute is the entrypoint for the CLI.
func Execute() {
cobra.CheckErr(rootCmd.Execute())
cmd, err := rootCmd.ExecuteC()
if err == nil {
return
}
comps := strings.Split(cmd.CommandPath(), " ")
if len(comps) > 1 && comps[1] == "tools" {
cmd.PrintErrln(cmd.ErrPrefix(), err.Error())
} else {
pterm.Error.Println(err.Error())
}
os.Exit(1)
}

func init() {
Expand Down

0 comments on commit 90f038e

Please sign in to comment.