From 973df545ef4dc247f1afd68ab7385b2b1bab17db Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Fri, 20 Apr 2018 13:34:50 -0700 Subject: [PATCH] search: support wildcards in filename search Change-Id: Ic8a1b2cdad3215f7e850bcc47467e0b4f8e92c4f --- pkg/search/predicate.go | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/pkg/search/predicate.go b/pkg/search/predicate.go index a4d3ccc67..48fce134b 100644 --- a/pkg/search/predicate.go +++ b/pkg/search/predicate.go @@ -22,6 +22,7 @@ import ( "bytes" "context" "encoding/json" + "errors" "fmt" "regexp" "strconv" @@ -868,14 +869,25 @@ func newFilename() keyword { } func (fn filename) Description() string { - return "Match filename" + return "Match filename, case sensitively. Supports optional '*' wildcard at beginning, end, or both." } func (fn filename) Predicate(ctx context.Context, args []string) (*Constraint, error) { - c := permOfFile(&FileConstraint{ - FileName: &StringConstraint{ - Equals: args[0], - }, - }) - return c, nil + arg := args[0] + switch { + case !strings.Contains(arg, "*"): + return permOfFile(&FileConstraint{FileName: &StringConstraint{Equals: arg}}), nil + case strings.HasPrefix(arg, "*") && !strings.Contains(arg[1:], "*"): + suffix := arg[1:] + return permOfFile(&FileConstraint{FileName: &StringConstraint{HasSuffix: suffix}}), nil + case strings.HasSuffix(arg, "*") && !strings.Contains(arg[:len(arg)-1], "*"): + prefix := arg[:len(arg)-1] + return permOfFile(&FileConstraint{FileName: &StringConstraint{ + HasPrefix: prefix, + }}), nil + case strings.HasSuffix(arg, "*") && strings.HasPrefix(arg, "*") && !strings.Contains(arg[1:len(arg)-1], "*"): + sub := arg[1 : len(arg)-1] + return permOfFile(&FileConstraint{FileName: &StringConstraint{Contains: sub}}), nil + } + return nil, errors.New("unsupported glob wildcard in filename search predicate") }