Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Make regex an alias of regexp tag filter type #1915

Merged
merged 1 commit into from
Apr 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions policy/pattern.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package policy

import (
"regexp"
"strings"

"github.com/Masterminds/semver"
"github.com/ryanuber/go-glob"
"github.com/weaveworks/flux/image"
"strings"
"regexp"
)

const (
globPrefix = "glob:"
semverPrefix = "semver:"
regexpPrefix = "regexp:"
globPrefix = "glob:"
semverPrefix = "semver:"
regexpPrefix = "regexp:"
regexpAltPrefix = "regex:"
)

var (
Expand Down Expand Up @@ -43,8 +45,8 @@ type SemverPattern struct {

// RegexpPattern matches by regular expression.
type RegexpPattern struct {
pattern string // pattern without prefix
regexp *regexp.Regexp
pattern string // pattern without prefix
regexp *regexp.Regexp
}

// NewPattern instantiates a Pattern according to the prefix
Expand All @@ -60,6 +62,10 @@ func NewPattern(pattern string) Pattern {
pattern = strings.TrimPrefix(pattern, regexpPrefix)
r, _ := regexp.Compile(pattern)
return RegexpPattern{pattern, r}
case strings.HasPrefix(pattern, regexpAltPrefix):
pattern = strings.TrimPrefix(pattern, regexpAltPrefix)
r, _ := regexp.Compile(pattern)
return RegexpPattern{pattern, r}
default:
return GlobPattern(strings.TrimPrefix(pattern, globPrefix))
}
Expand Down
8 changes: 7 additions & 1 deletion policy/pattern_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package policy

import (
"fmt"
"testing"

"fmt"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -106,6 +106,12 @@ func TestRegexpPattern_Matches(t *testing.T) {
true: []string{"foo", "BAR", "fooBAR"},
false: []string{"1", "foo-1"},
},
{
name: "regex",
pattern: `regex:^\w{7}(?:\w)?$`,
true: []string{"af14eb2", "bb73ed94", "946427ff"},
false: []string{"1", "foo", "946427ff-foo"},
},
} {
pattern := NewPattern(tt.pattern)
assert.IsType(t, RegexpPattern{}, pattern)
Expand Down
1 change: 1 addition & 0 deletions site/fluxctl.md
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ If your images have complex tags you can filter by regular expression:
fluxctl policy --workload=default:deployment/helloworld --tag-all='regexp:^([a-zA-Z]+)$'
```

Instead of `regexp` it is also possible to use its alias `regex`.
Please bear in mind that if you want to match the whole tag,
you must bookend your pattern with `^` and `$`.

Expand Down