From dfc63b6bdf0c6cf7bfddbf8f40793249807755ea Mon Sep 17 00:00:00 2001 From: Hidde Beydals Date: Wed, 10 Apr 2019 11:24:35 +0200 Subject: [PATCH] Make `regex` an alias of `regexp` tag filter type The word `regex` seems to be a common alias of `regexp`, so people often use this and are left with confusion when Flux does not adhere to what they configured. Make it an alias to better live up to people their expectations. --- policy/pattern.go | 20 +++++++++++++------- policy/pattern_test.go | 8 +++++++- site/fluxctl.md | 1 + 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/policy/pattern.go b/policy/pattern.go index c89864767..56380ab05 100644 --- a/policy/pattern.go +++ b/policy/pattern.go @@ -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 ( @@ -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 @@ -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)) } diff --git a/policy/pattern_test.go b/policy/pattern_test.go index 35678b42f..aeb8f25f2 100644 --- a/policy/pattern_test.go +++ b/policy/pattern_test.go @@ -1,9 +1,9 @@ package policy import ( + "fmt" "testing" - "fmt" "github.com/stretchr/testify/assert" ) @@ -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) diff --git a/site/fluxctl.md b/site/fluxctl.md index c37a1515e..68fa6d19b 100644 --- a/site/fluxctl.md +++ b/site/fluxctl.md @@ -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 `$`.