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