From 9b72c10134f93577794b64b4ea99a1d32ac4c50f Mon Sep 17 00:00:00 2001 From: Julius Volz Date: Wed, 1 Nov 2017 23:08:34 +0100 Subject: [PATCH] Minor code cleanups --- cli/check_config.go | 4 ++-- cli/format/format.go | 2 +- cli/format/format_json.go | 12 ++++++------ cli/silence_add.go | 12 ++++++------ cli/utils.go | 3 +-- config/notifiers_test.go | 4 ++-- nflog/nflogpb/set.go | 2 +- notify/impl.go | 10 +++++----- notify/notify.go | 4 ++-- test/acceptance/send_test.go | 4 ++-- types/types.go | 2 +- 11 files changed, 29 insertions(+), 30 deletions(-) diff --git a/cli/check_config.go b/cli/check_config.go index b1e069d3..66077245 100644 --- a/cli/check_config.go +++ b/cli/check_config.go @@ -39,7 +39,7 @@ func CheckConfig(args []string) error { config, _, err := config.LoadFile(arg) if err != nil { fmt.Printf(" FAILED: %s\n", err) - failed += 1 + failed++ } else { fmt.Printf(" SUCCESS\n") } @@ -50,7 +50,7 @@ func CheckConfig(args []string) error { _, err = template.FromGlobs(config.Templates...) if err != nil { fmt.Printf(" FAILED: %s\n", err) - failed += 1 + failed++ } else { fmt.Printf(" SUCCESS\n") } diff --git a/cli/format/format.go b/cli/format/format.go index d5f65ddb..29b40fd7 100644 --- a/cli/format/format.go +++ b/cli/format/format.go @@ -43,7 +43,7 @@ type Formatter interface { } // Formatters is a map of cli argument name to formatter inferface object -var Formatters map[string]Formatter = map[string]Formatter{} +var Formatters = map[string]Formatter{} func FormatDate(input time.Time) string { dateformat := viper.GetString("date.format") diff --git a/cli/format/format_json.go b/cli/format/format_json.go index aead76d5..92fe24c2 100644 --- a/cli/format/format_json.go +++ b/cli/format/format_json.go @@ -9,29 +9,29 @@ import ( "github.com/prometheus/alertmanager/types" ) -type JsonFormatter struct { +type JSONFormatter struct { writer io.Writer } func init() { - Formatters["json"] = &JsonFormatter{writer: os.Stdout} + Formatters["json"] = &JSONFormatter{writer: os.Stdout} } -func (formatter *JsonFormatter) SetOutput(writer io.Writer) { +func (formatter *JSONFormatter) SetOutput(writer io.Writer) { formatter.writer = writer } -func (formatter *JsonFormatter) FormatSilences(silences []types.Silence) error { +func (formatter *JSONFormatter) FormatSilences(silences []types.Silence) error { enc := json.NewEncoder(formatter.writer) return enc.Encode(silences) } -func (formatter *JsonFormatter) FormatAlerts(alerts []*dispatch.APIAlert) error { +func (formatter *JSONFormatter) FormatAlerts(alerts []*dispatch.APIAlert) error { enc := json.NewEncoder(formatter.writer) return enc.Encode(alerts) } -func (formatter *JsonFormatter) FormatConfig(config Config) error { +func (formatter *JSONFormatter) FormatConfig(config Config) error { enc := json.NewEncoder(formatter.writer) return enc.Encode(config) } diff --git a/cli/silence_add.go b/cli/silence_add.go index 458debe3..e249c019 100644 --- a/cli/silence_add.go +++ b/cli/silence_add.go @@ -88,7 +88,7 @@ func add(cmd *cobra.Command, args []string) error { return fmt.Errorf("No matchers specified") } - expire_on, err := addFlags.GetString("expire-on") + expireOn, err := addFlags.GetString("expire-on") if err != nil { return err } @@ -96,8 +96,8 @@ func add(cmd *cobra.Command, args []string) error { expires := viper.GetString("expires") var endsAt time.Time - if expire_on != "" { - endsAt, err = time.Parse(time.RFC3339, expire_on) + if expireOn != "" { + endsAt, err = time.Parse(time.RFC3339, expireOn) if err != nil { return err } @@ -111,9 +111,9 @@ func add(cmd *cobra.Command, args []string) error { author := viper.GetString("author") comment := viper.GetString("comment") - comment_required := viper.GetBool("comment_required") + commentRequired := viper.GetBool("comment_required") - if comment_required && comment == "" { + if commentRequired && comment == "" { return errors.New("Comment required by config") } @@ -151,7 +151,7 @@ func add(cmd *cobra.Command, args []string) error { response := addResponse{} err = json.NewDecoder(res.Body).Decode(&response) if err != nil { - return errors.New(fmt.Sprintf("Unable to parse silence json response from %s", u.String())) + return fmt.Errorf("Unable to parse silence json response from %s", u.String()) } if response.Status == "error" { diff --git a/cli/utils.go b/cli/utils.go index 2ee7afa1..a399866d 100644 --- a/cli/utils.go +++ b/cli/utils.go @@ -26,9 +26,8 @@ func (s ByAlphabetical) Less(i, j int) bool { return s[i].Type < s[j].Type } else if s[i].Value != s[j].Value { return s[i].Value < s[j].Value - } else { - return false } + return false } func GetAlertmanagerURL() (*url.URL, error) { diff --git a/config/notifiers_test.go b/config/notifiers_test.go index 5b83dd57..57c2bebb 100644 --- a/config/notifiers_test.go +++ b/config/notifiers_test.go @@ -1,8 +1,8 @@ package config import ( - "testing" "gopkg.in/yaml.v2" + "testing" ) func TestEmailToIsPresent(t *testing.T) { @@ -160,4 +160,4 @@ token: '' if err.Error() != expected { t.Errorf("\nexpected:\n%v\ngot:\n%v", expected, err.Error()) } -} \ No newline at end of file +} diff --git a/nflog/nflogpb/set.go b/nflog/nflogpb/set.go index 2452a3bc..698f7f2d 100644 --- a/nflog/nflogpb/set.go +++ b/nflog/nflogpb/set.go @@ -36,7 +36,7 @@ func (m *Entry) IsResolvedSubset(subset map[uint64]struct{}) bool { } func isSubset(set, subset map[uint64]struct{}) bool { - for k, _ := range subset { + for k := range subset { _, exists := set[k] if !exists { return false diff --git a/notify/impl.go b/notify/impl.go index af784db2..c6d51bea 100644 --- a/notify/impl.go +++ b/notify/impl.go @@ -981,12 +981,12 @@ func (n *Pushover) Notify(ctx context.Context, as ...*types.Alert) (bool, error) } parameters.Add("message", message) - supplementaryUrl := tmpl(n.conf.URL) - if len(supplementaryUrl) > 512 { - supplementaryUrl = supplementaryUrl[:509] + "..." - level.Debug(n.logger).Log("msg", "Truncated URL due to Pushover url limit", "truncated_url", supplementaryUrl, "incident", key) + supplementaryURL := tmpl(n.conf.URL) + if len(supplementaryURL) > 512 { + supplementaryURL = supplementaryURL[:509] + "..." + level.Debug(n.logger).Log("msg", "Truncated URL due to Pushover url limit", "truncated_url", supplementaryURL, "incident", key) } - parameters.Add("url", supplementaryUrl) + parameters.Add("url", supplementaryURL) parameters.Add("priority", tmpl(n.conf.Priority)) parameters.Add("retry", fmt.Sprintf("%d", int64(time.Duration(n.conf.Retry).Seconds()))) diff --git a/notify/notify.go b/notify/notify.go index c63acaf5..1ec29113 100644 --- a/notify/notify.go +++ b/notify/notify.go @@ -536,7 +536,7 @@ func (n *DedupStage) Exec(ctx context.Context, l log.Logger, alerts ...*types.Al case 1: entry = entries[0] case 2: - return ctx, nil, fmt.Errorf("Unexpected entry result size %d", len(entries)) + return ctx, nil, fmt.Errorf("unexpected entry result size %d", len(entries)) } if ok, err := n.needsUpdate(entry, firingSet, resolvedSet, repeatInterval); err != nil { return ctx, nil, err @@ -588,7 +588,7 @@ func (r RetryStage) Exec(ctx context.Context, l log.Logger, alerts ...*types.Ale numFailedNotifications.WithLabelValues(r.integration.name).Inc() level.Debug(l).Log("msg", "Notify attempt failed", "attempt", i, "integration", r.integration.name, "err", err) if !retry { - return ctx, alerts, fmt.Errorf("Cancelling notify retry for %q due to unrecoverable error: %s", r.integration.name, err) + return ctx, alerts, fmt.Errorf("cancelling notify retry for %q due to unrecoverable error: %s", r.integration.name, err) } // Save this error to be able to return the last seen error by an diff --git a/test/acceptance/send_test.go b/test/acceptance/send_test.go index 1b8f4e1d..a9af5761 100644 --- a/test/acceptance/send_test.go +++ b/test/acceptance/send_test.go @@ -112,7 +112,7 @@ route: group_by: [] group_wait: 1s group_interval: 1s - repeat_interval: 0s + repeat_interval: 0s receivers: - name: "default" @@ -283,7 +283,7 @@ global: route: receiver: "default" group_by: [alertname] - group_wait: 1s + group_wait: 1s group_interval: 5s receivers: diff --git a/types/types.go b/types/types.go index f2af722a..dc7f051d 100644 --- a/types/types.go +++ b/types/types.go @@ -82,7 +82,7 @@ func (m *memMarker) Count(states ...AlertState) int { for _, status := range m.m { for _, state := range states { if status.State == state { - count += 1 + count++ } } }