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

Commit

Permalink
Merge pull request #2751 from wsong/support_regex_filter
Browse files Browse the repository at this point in the history
Support asterisks in image reference filter
  • Loading branch information
nishanttotla authored Jul 11, 2017
2 parents 7270d39 + b0d9b0e commit 91b9562
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
21 changes: 15 additions & 6 deletions cluster/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cluster
import (
"strings"

"github.com/docker/distribution/reference"
"github.com/docker/docker/api/types"
)

Expand Down Expand Up @@ -108,22 +109,30 @@ func (images Images) Filter(opts ImageFilterOptions) Images {
if !opts.Filters.Include(filter) {
return true
}
candidates := map[string]struct{}{}
for _, repoTag := range image.RepoTags {
imageName, _ := ParseRepositoryTag(repoTag)
for _, pattern := range opts.Filters.Get(filter) {
if repoTag == pattern || pattern == imageName {
return true
}
}
candidates[repoTag] = struct{}{}
candidates[imageName] = struct{}{}
}
for _, repoDigests := range image.RepoDigests {
imageName, _ := ParseRepositoryTag(repoDigests)
candidates[repoDigests] = struct{}{}
candidates[imageName] = struct{}{}
}
for candidate := range candidates {
for _, pattern := range opts.Filters.Get(filter) {
if repoDigests == pattern || pattern == imageName {
ref, err := reference.Parse(candidate)
if err != nil {
continue
}
found, matchErr := reference.FamiliarMatch(pattern, ref)
if matchErr == nil && found {
return true
}
}
}

return false
}

Expand Down
5 changes: 5 additions & 0 deletions test/integration/api/images.bats
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ function teardown() {
# lines are: header, busybox, <none>
run docker_swarm images -a
[ "${#lines[@]}" -ge 3 ]

run docker_swarm images --filter reference='busy*'
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 2 ]
[[ "${lines[1]}" == *"busybox"* ]]
}

@test "docker images -f label" {
Expand Down

0 comments on commit 91b9562

Please sign in to comment.