search: support wildcards in filename search

Change-Id: Ic8a1b2cdad3215f7e850bcc47467e0b4f8e92c4f
This commit is contained in:
Brad Fitzpatrick 2018-04-20 13:34:50 -07:00
parent 951e9a45bf
commit 973df545ef
1 changed files with 19 additions and 7 deletions

View File

@ -22,6 +22,7 @@ import (
"bytes" "bytes"
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"regexp" "regexp"
"strconv" "strconv"
@ -868,14 +869,25 @@ func newFilename() keyword {
} }
func (fn filename) Description() string { 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) { func (fn filename) Predicate(ctx context.Context, args []string) (*Constraint, error) {
c := permOfFile(&FileConstraint{ arg := args[0]
FileName: &StringConstraint{ switch {
Equals: args[0], case !strings.Contains(arg, "*"):
}, return permOfFile(&FileConstraint{FileName: &StringConstraint{Equals: arg}}), nil
}) case strings.HasPrefix(arg, "*") && !strings.Contains(arg[1:], "*"):
return c, nil 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")
} }