Skip to content

Commit

Permalink
make regex names backwards compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
piksel committed Aug 1, 2022
1 parent 5999d0b commit c14d6df
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
23 changes: 17 additions & 6 deletions pkg/filters/filters.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package filters

import (
t "github.com/containrrr/watchtower/pkg/types"
"strings"
"regexp"
"strings"

t "github.com/containrrr/watchtower/pkg/types"
)

// WatchtowerContainersFilter filters only watchtower containers
Expand All @@ -20,11 +21,21 @@ func FilterByNames(names []string, baseFilter t.Filter) t.Filter {

return func(c t.FilterableContainer) bool {
for _, name := range names {
match, _ := (regexp.MatchString(name, c.Name()))
match1, _ := (regexp.MatchString(name, c.Name()[1:]))
if (name == c.Name()) || (name == c.Name()[1:]) || match || match1 {
if name == c.Name() || name == c.Name()[1:] {
return baseFilter(c)
}

if re, err := regexp.Compile(name); err == nil {
indices := re.FindStringIndex(c.Name())
if indices == nil {
continue
}
start := indices[0]
end := indices[1]
if start <= 1 && end >= len(c.Name())-1 {
return baseFilter(c)
}
}
}
return false
}
Expand Down Expand Up @@ -80,7 +91,7 @@ func BuildFilter(names []string, enableLabel bool, scope string) (t.Filter, stri
filter = FilterByNames(names, filter)

if len(names) > 0 {
sb.WriteString("with name \"")
sb.WriteString("which name matches \"")
for i, n := range names {
sb.WriteString(n)
if i < len(names)-1 {
Expand Down
30 changes: 26 additions & 4 deletions pkg/filters/filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ func TestFilterByNames(t *testing.T) {
container.AssertExpectations(t)
}

func TestFilterByNamesRegex(t *testing.T) {
names := []string{`ba(b|ll)oon`}

filter := FilterByNames(names, NoFilter)
assert.NotNil(t, filter)

container := new(mocks.FilterableContainer)
container.On("Name").Return("balloon")
assert.True(t, filter(container))
container.AssertExpectations(t)

container = new(mocks.FilterableContainer)
container.On("Name").Return("spoon")
assert.False(t, filter(container))
container.AssertExpectations(t)

container = new(mocks.FilterableContainer)
container.On("Name").Return("baboonious")
assert.False(t, filter(container))
container.AssertExpectations(t)
}

func TestFilterByEnableLabel(t *testing.T) {
filter := FilterByEnableLabel(NoFilter)
assert.NotNil(t, filter)
Expand All @@ -68,8 +90,7 @@ func TestFilterByEnableLabel(t *testing.T) {
}

func TestFilterByScope(t *testing.T) {
var scope string
scope = "testscope"
scope := "testscope"

filter := FilterByScope(scope, NoFilter)
assert.NotNil(t, filter)
Expand Down Expand Up @@ -111,11 +132,12 @@ func TestFilterByDisabledLabel(t *testing.T) {
}

func TestBuildFilter(t *testing.T) {
var names []string
names = append(names, "test")
names := []string{"test", "valid"}

filter, desc := BuildFilter(names, false, "")
assert.Contains(t, desc, "test")
assert.Contains(t, desc, "or")
assert.Contains(t, desc, "valid")

container := new(mocks.FilterableContainer)
container.On("Name").Return("Invalid")
Expand Down

0 comments on commit c14d6df

Please sign in to comment.