alertmanager/cli/silence_expire.go
Calle Pettersson 608848390f Switch amtool to kingpin (#976)
* Switch cmd/amtool to kingpin

* Touch-ups

* Implement long help

* Add missing short-form of --output

* Fix backwards compatibility for config file options

* Fix vendoring

* Review fixes

* Fix flag word order
2017-12-22 11:17:13 +01:00

54 lines
1.1 KiB
Go

package cli
import (
"encoding/json"
"errors"
"net/http"
"path"
"github.com/alecthomas/kingpin"
)
var (
expireCmd = silenceCmd.Command("expire", "expire an alertmanager silence")
expireIds = expireCmd.Arg("silence-ids", "Ids of silences to expire").Strings()
)
func init() {
expireCmd.Action(expire)
longHelpText["silence expire"] = `Expire an alertmanager silence`
}
func expire(element *kingpin.ParseElement, ctx *kingpin.ParseContext) error {
if len(*expireIds) < 1 {
return errors.New("no silence IDs specified")
}
basePath := "/api/v1/silence"
for _, id := range *expireIds {
u := GetAlertmanagerURL(path.Join(basePath, id))
req, err := http.NewRequest("DELETE", u.String(), nil)
if err != nil {
return err
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
decoder := json.NewDecoder(res.Body)
response := alertmanagerSilenceResponse{}
err = decoder.Decode(&response)
if err != nil {
return err
}
if response.Status == "error" {
return errors.New(response.Error)
}
}
return nil
}