From 16aa1ff7bdf149ffd3502f48c250388ff3252004 Mon Sep 17 00:00:00 2001 From: Philip Laine Date: Mon, 3 Jun 2024 10:25:16 +0200 Subject: [PATCH] Add custom error printing for Zarf commands --- src/cmd/root.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/cmd/root.go b/src/cmd/root.go index 83fb28e61d..52e10044c1 100644 --- a/src/cmd/root.go +++ b/src/cmd/root.go @@ -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" ) @@ -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) @@ -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() {