Add command wrapper to handle printing errors for any subcommands (#724)
Fixes #721 I was using the RunE method of cobra.Command which does things with an error returned from a function. I doesn't seem possible to keep it from printing usage every time, so I've make a wrapper to use the other function.
This commit is contained in:
parent
0659dfbde3
commit
b2c656a071
|
@ -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{
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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() {
|
||||
|
|
10
cli/utils.go
10
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue