Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support filtering on annotation with regex #259

Merged
merged 6 commits into from
Aug 5, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
moved regex compilation outside the loop
Signed-off-by: wangxiaoxuan273 <wangxiaoxuan119@gmail.com>
  • Loading branch information
wangxiaoxuan273 committed Aug 5, 2022
commit a0d795ea55cbf3ab8c2d81c7b72ec50a227d2730
22 changes: 13 additions & 9 deletions extendedcopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,28 @@ func ExtendedCopyGraph(ctx context.Context, src content.GraphStorage, dst conten
return nil
}

func (opts *ExtendedCopyGraphOptions) FilterAnnotation(key string, regex string) {
opts.FindPredecessors = matchRegexAnnotation(key, regex, opts.FindPredecessors)
// FilterArtifactType will configure opts.FindPredecessors to filter the predecessors
wangxiaoxuan273 marked this conversation as resolved.
Show resolved Hide resolved
// whose annotation matches a given regex pattern. The regex syntax supported
// is RE2. Reference: https://github.com/google/re2/wiki/Syntax.
func (opts *ExtendedCopyGraphOptions) FilterAnnotation(key string, pattern string) {
wangxiaoxuan273 marked this conversation as resolved.
Show resolved Hide resolved
wangxiaoxuan273 marked this conversation as resolved.
Show resolved Hide resolved
opts.FindPredecessors = matchRegexAnnotation(key, pattern, opts.FindPredecessors)
}

func matchRegexAnnotation(key string, regex string,
// matchRegexAnnotation takes an old opts.FindPredecessors function and return
// a new one with regex filter applied.
func matchRegexAnnotation(key string, pattern string,
fp func(ctx context.Context,
src content.GraphStorage,
desc ocispec.Descriptor) ([]ocispec.Descriptor, error)) func(ctx context.Context,
src content.GraphStorage, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
return func(ctx context.Context,
src content.GraphStorage,
desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
regex, err := regexp.Compile(pattern)
if err != nil {
return nil, err
}
var predecessors []ocispec.Descriptor
var err error
if fp == nil {
predecessors, err = src.Predecessors(ctx, desc)
} else {
Expand All @@ -129,11 +137,7 @@ func matchRegexAnnotation(key string, regex string,
}
var filtered []ocispec.Descriptor
wangxiaoxuan273 marked this conversation as resolved.
Show resolved Hide resolved
for _, p := range predecessors {
matched, err := regexp.MatchString(regex, p.Annotations[key])
if err != nil {
return nil, err
}
if matched {
if regex.MatchString(p.Annotations[key]) {
filtered = append(filtered, p)
}
}
Expand Down