From 7b787dab057a556bbc805335ba74af5aa861d785 Mon Sep 17 00:00:00 2001 From: stuart nelson Date: Tue, 9 Jan 2018 10:47:41 +0100 Subject: [PATCH] Re-introduce prometheus durations in amtool silence creation (#1185) * Fixes #1183 * Update expires comment The default time is already output thanks to kingpin. --- cli/silence_add.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/cli/silence_add.go b/cli/silence_add.go index 08a37630..6ab47570 100644 --- a/cli/silence_add.go +++ b/cli/silence_add.go @@ -11,6 +11,7 @@ import ( "github.com/alecthomas/kingpin" "github.com/prometheus/alertmanager/types" + "github.com/prometheus/common/model" ) type addResponse struct { @@ -34,7 +35,7 @@ var ( addCmd = silenceCmd.Command("add", "Add a new alertmanager silence") author = addCmd.Flag("author", "Username for CreatedBy field").Short('a').Default(username()).String() requireComment = addCmd.Flag("require-comment", "Require comment to be set").Hidden().Default("true").Bool() - expires = addCmd.Flag("expires", "Duration of silence (100h)").Short('e').Default("1h").Duration() + expires = addCmd.Flag("expires", "Duration of silence").Short('e').Default("1h").String() expireOn = addCmd.Flag("expire-on", "Expire at a certain time (Overwrites expires) RFC3339 format 2006-01-02T15:04:05Z07:00").String() comment = addCmd.Flag("comment", "A comment to help describe the silence").Short('c').String() addArgs = addCmd.Arg("matcher-groups", "Query filter").Strings() @@ -86,7 +87,14 @@ func add(element *kingpin.ParseElement, ctx *kingpin.ParseContext) error { return err } } else { - endsAt = time.Now().UTC().Add(*expires) + duration, err := model.ParseDuration(*expires) + if err != nil { + return err + } + if duration == 0 { + return fmt.Errorf("silence duration must be greater than 0") + } + endsAt = time.Now().UTC().Add(time.Duration(duration)) } if *requireComment && *comment == "" {