cli: Unify matcher formatting

Signed-off-by: Kiril Vladimirov <kiril@vladimiroff.org>
This commit is contained in:
Kiril Vladimirov 2021-01-29 23:39:15 +02:00
parent 2b6315f399
commit 133b5bab42
2 changed files with 13 additions and 12 deletions

View File

@ -132,14 +132,7 @@ func extendedFormatAnnotations(labels models.LabelSet) string {
func extendedFormatMatchers(matchers models.Matchers) string {
output := []string{}
for _, matcher := range matchers {
output = append(output, extendedFormatMatcher(*matcher))
output = append(output, simpleFormatMatcher(*matcher))
}
return strings.Join(output, " ")
}
func extendedFormatMatcher(matcher models.Matcher) string {
if *matcher.IsRegex {
return fmt.Sprintf("%s=~%s", *matcher.Name, *matcher.Value)
}
return fmt.Sprintf("%s=%s", *matcher.Name, *matcher.Value)
}

View File

@ -94,9 +94,17 @@ func simpleFormatMatchers(matchers models.Matchers) string {
return strings.Join(output, " ")
}
func simpleFormatMatcher(matcher models.Matcher) string {
if *matcher.IsRegex {
return fmt.Sprintf("%s=~%s", *matcher.Name, *matcher.Value)
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", *matcher.Name, *matcher.Value)
return fmt.Sprintf("%s%s%q", *m.Name, op, *m.Value)
}