Skip to content

Instantly share code, notes, and snippets.

@alexraileanu
Created July 18, 2022 10:23
Show Gist options
  • Save alexraileanu/200a4473653efa8fbda6e555bd3fc428 to your computer and use it in GitHub Desktop.
Save alexraileanu/200a4473653efa8fbda6e555bd3fc428 to your computer and use it in GitHub Desktop.
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