diff --git a/cli/alert.go b/cli/alert.go index a2a733f9..1fa289ec 100644 --- a/cli/alert.go +++ b/cli/alert.go @@ -63,7 +63,7 @@ var alertCmd = &cobra.Command{ (similar to prometheus) is used to represent a regex match. Regex matching can be used in combination with a direct match. `, - RunE: queryAlerts, + Run: CommandWrapper(queryAlerts), } var alertQueryCmd = &cobra.Command{ diff --git a/cli/silence.go b/cli/silence.go index d5b287e4..d7d2a6ac 100644 --- a/cli/silence.go +++ b/cli/silence.go @@ -21,7 +21,7 @@ var silenceCmd = &cobra.Command{ Use: "silence", Short: "Manage silences", Long: `Add, expire or view silences. For more information and additional flags see query help`, - RunE: query, + Run: CommandWrapper(query), } func init() { diff --git a/cli/silence_add.go b/cli/silence_add.go index c77bcbec..deb5e2e0 100644 --- a/cli/silence_add.go +++ b/cli/silence_add.go @@ -52,7 +52,7 @@ var addCmd = &cobra.Command{ (similar to prometheus) is used to represent a regex match. Regex matching can be used in combination with a direct match. `, - RunE: add, + Run: CommandWrapper(add), } func init() { diff --git a/cli/silence_expire.go b/cli/silence_expire.go index 9f10195e..871d4e58 100644 --- a/cli/silence_expire.go +++ b/cli/silence_expire.go @@ -13,7 +13,7 @@ var expireCmd = &cobra.Command{ Use: "expire", Short: "expire silence", Long: `expire an alertmanager silence`, - RunE: expire, + Run: CommandWrapper(expire), } func expire(cmd *cobra.Command, args []string) error { diff --git a/cli/silence_query.go b/cli/silence_query.go index 3b3c1b0a..7197f0ee 100644 --- a/cli/silence_query.go +++ b/cli/silence_query.go @@ -45,7 +45,7 @@ var queryCmd = &cobra.Command{ (similar to prometheus) is used to represent a regex match. Regex matching can be used in combination with a direct match. `, - RunE: query, + Run: CommandWrapper(query), } func init() { diff --git a/cli/utils.go b/cli/utils.go index 64502b3a..40588174 100644 --- a/cli/utils.go +++ b/cli/utils.go @@ -5,6 +5,7 @@ import ( "fmt" "net/url" + "github.com/spf13/cobra" "github.com/spf13/viper" "github.com/prometheus/alertmanager/pkg/parse" @@ -85,3 +86,12 @@ func TypeMatcher(matcher labels.Matcher) (types.Matcher, error) { } return *typeMatcher, nil } + +func CommandWrapper(command func(*cobra.Command, []string) error) func(*cobra.Command, []string) { + return func(cmd *cobra.Command, args []string) { + err := command(cmd, args) + if err != nil { + fmt.Printf("Error: %s\n", err) + } + } +}