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"
"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")
}