cli/format: Create labels.Matcher to format

There is non-trivial value escaping going inside pkg/labels.

Signed-off-by: Kiril Vladimirov <kiril@vladimiroff.org>
This commit is contained in:
Kiril Vladimirov 2021-02-16 14:20:30 +02:00
parent f503012c0b
commit 217562e838
2 changed files with 18 additions and 12 deletions

View File

@ -21,6 +21,7 @@ import (
"github.com/go-openapi/strfmt"
"github.com/prometheus/alertmanager/api/v2/models"
"github.com/prometheus/alertmanager/pkg/labels"
)
const DefaultDateFormat = "2006-01-02 15:04:05 MST"
@ -48,3 +49,19 @@ var Formatters = map[string]Formatter{}
func FormatDate(input strfmt.DateTime) string {
return time.Time(input).Format(*dateFormat)
}
func labelsMatcher(m models.Matcher) *labels.Matcher {
var t labels.MatchType
switch {
case !*m.IsRegex && *m.IsEqual:
t = labels.MatchEqual
case !*m.IsRegex && !*m.IsEqual:
t = labels.MatchNotEqual
case *m.IsRegex && *m.IsEqual:
t = labels.MatchRegexp
case *m.IsRegex && !*m.IsEqual:
t = labels.MatchNotRegexp
}
return &labels.Matcher{Type: t, Name: *m.Name, Value: *m.Value}
}

View File

@ -95,16 +95,5 @@ func simpleFormatMatchers(matchers models.Matchers) string {
}
func simpleFormatMatcher(m models.Matcher) string {
var op string
switch {
case !*m.IsRegex && *m.IsEqual:
op = "="
case !*m.IsRegex && !*m.IsEqual:
op = "!="
case *m.IsRegex && *m.IsEqual:
op = "=~"
case *m.IsRegex && !*m.IsEqual:
op = "!~"
}
return fmt.Sprintf("%s%s%q", *m.Name, op, *m.Value)
return labelsMatcher(m).String()
}