Skip to content

Commit

Permalink
Add negation to filters
Browse files Browse the repository at this point in the history
Signed-off-by: rvandernoort <s.r.vandernoort@student.tudelft.nl>
  • Loading branch information
rvandernoort committed Mar 21, 2022
1 parent c058991 commit 44573af
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions libimage/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,12 @@ func (r *Runtime) compileImageFilters(ctx context.Context, options *ListImagesOp
for _, f := range options.Filters {
var key, value string
var filter filterFunc
split := strings.SplitN(f, "=", 2)
split := strings.SplitN(f, "!=", 2)
if len(split) != 2 {
return nil, errors.Errorf("invalid image filter %q: must be in the format %q", f, "filter=value")
split = strings.SplitN(f, "=", 2)
if len(split) != 2 {
return nil, errors.Errorf("invalid image filter %q: must be in the format %q", f, "filter=value or filter!=value")
}
}

key = split[0]
Expand Down Expand Up @@ -182,12 +185,22 @@ func (r *Runtime) compileImageFilters(ctx context.Context, options *ListImagesOp
default:
return nil, errors.Errorf("unsupported image filter %q", key)
}
if len(strings.SplitN(f, "!=", 2)) == 2 {
filter = negateFilter(filter)
}
filters[key] = append(filters[key], filter)
}

return filters, nil
}

func negateFilter(f filterFunc) filterFunc {
return func(img *Image) (bool, error) {
b, err := f(img)
return !b, err
}
}

func (r *Runtime) containers(duplicate map[string]string, key, value string, externalFunc IsExternalContainerFunc) error {
if exists, ok := duplicate[key]; ok && exists != value {
return errors.Errorf("specifying %q filter more than once with different values is not supported", key)
Expand Down

0 comments on commit 44573af

Please sign in to comment.