Created
July 18, 2022 10:23
-
-
Save alexraileanu/200a4473653efa8fbda6e555bd3fc428 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"errors" | |
"fmt" | |
"os" | |
"github.com/jedib0t/go-pretty/v6/text" | |
"github.com/spf13/cobra" | |
) | |
var rootCmd = &cobra.Command{ | |
Use: "cobratest", | |
SilenceUsage: true, | |
} | |
var first = &cobra.Command{ | |
Use: "first", | |
RunE: func(cmd *cobra.Command, args []string) error { | |
err := triggerError() | |
if err != nil { | |
errColor := text.FgCyan | |
errMsg := errColor.Sprintf("another different error msg") | |
return fmt.Errorf(errMsg) | |
} | |
return nil | |
}, | |
} | |
var second = &cobra.Command{ | |
Use: "second", | |
RunE: func(cmd *cobra.Command, args []string) error { | |
err := triggerError() | |
if err != nil { | |
errColor := text.FgRed | |
errMsg := errColor.Sprintf("different error message") | |
return fmt.Errorf(errMsg) | |
} | |
return nil | |
}, | |
} | |
func main() { | |
rootCmd.AddCommand(first) | |
rootCmd.AddCommand(second) | |
err := rootCmd.Execute() | |
if err != nil { | |
os.Exit(1) | |
} | |
} | |
func triggerError() error { | |
return errors.New("random generic error") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment