cli: Parse properly all matcher types

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

View File

@ -144,16 +144,11 @@ func (c *silenceAddCmd) add(ctx context.Context, _ *kingpin.ParseContext) error
return errors.New("silence cannot start after it ends") return errors.New("silence cannot start after it ends")
} }
typeMatchers, err := TypeMatchers(matchers)
if err != nil {
return err
}
start := strfmt.DateTime(startsAt) start := strfmt.DateTime(startsAt)
end := strfmt.DateTime(endsAt) end := strfmt.DateTime(endsAt)
ps := &models.PostableSilence{ ps := &models.PostableSilence{
Silence: models.Silence{ Silence: models.Silence{
Matchers: typeMatchers, Matchers: TypeMatchers(matchers),
StartsAt: &start, StartsAt: &start,
EndsAt: &end, EndsAt: &end,
CreatedBy: &c.author, CreatedBy: &c.author,

View File

@ -122,21 +122,16 @@ func parseLabels(inputLabels []string) (models.LabelSet, error) {
} }
// TypeMatchers only valid for when you are going to add a silence // TypeMatchers only valid for when you are going to add a silence
func TypeMatchers(matchers []labels.Matcher) (models.Matchers, error) { func TypeMatchers(matchers []labels.Matcher) models.Matchers {
typeMatchers := models.Matchers{} typeMatchers := make(models.Matchers, len(matchers))
for _, matcher := range matchers { for i, matcher := range matchers {
typeMatcher, err := TypeMatcher(matcher) typeMatchers[i] = TypeMatcher(matcher)
if err != nil {
return models.Matchers{}, err
}
typeMatchers = append(typeMatchers, &typeMatcher)
} }
return typeMatchers, nil return typeMatchers
} }
// TypeMatcher only valid for when you are going to add a silence // TypeMatcher only valid for when you are going to add a silence
// Doesn't allow negative operators func TypeMatcher(matcher labels.Matcher) *models.Matcher {
func TypeMatcher(matcher labels.Matcher) (models.Matcher, error) {
name := matcher.Name name := matcher.Name
value := matcher.Value value := matcher.Value
typeMatcher := models.Matcher{ typeMatcher := models.Matcher{
@ -144,17 +139,11 @@ func TypeMatcher(matcher labels.Matcher) (models.Matcher, error) {
Value: &value, Value: &value,
} }
isRegex := false isEqual := (matcher.Type == labels.MatchEqual) || (matcher.Type == labels.MatchRegexp)
switch matcher.Type { isRegex := (matcher.Type == labels.MatchRegexp) || (matcher.Type == labels.MatchNotRegexp)
case labels.MatchEqual: typeMatcher.IsEqual = &isEqual
isRegex = false
case labels.MatchRegexp:
isRegex = true
default:
return models.Matcher{}, fmt.Errorf("invalid match type for creation operation: %s", matcher.Type)
}
typeMatcher.IsRegex = &isRegex typeMatcher.IsRegex = &isRegex
return typeMatcher, nil return &typeMatcher
} }
// Helper function for adding the ctx with timeout into an action. // Helper function for adding the ctx with timeout into an action.