Skip to content

Commit 1186a98

Browse files
committed
change match to add wildcards instead of using substring
1 parent 217bba6 commit 1186a98

2 files changed

Lines changed: 21 additions & 14 deletions

File tree

‎cmd/chisel/cmd_find.go‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
var shortFindHelp = "Find existing slices"
1616
var longFindHelp = `
1717
The find command queries the slice definitions for matching slices.
18+
Globs are allowed in the query.
1819
1920
By default it fetches the slices for the latest Ubuntu
2021
version, unless the --release flag is used.
@@ -67,27 +68,26 @@ func (cmd *cmdFind) Execute(args []string) error {
6768

6869
// match reports whether a slice (partially) matches the query.
6970
func match(slice *setup.Slice, query string) bool {
70-
const maxStrDist = 1
71-
fuzzyMatch := func(str, query string) bool {
72-
return strdist.Distance(str, query, strdist.StandardCost, maxStrDist+1) <= maxStrDist
71+
if !strings.HasPrefix(query, "*") {
72+
query = "*" + query
7373
}
74-
return strings.Contains(slice.String(), query) ||
75-
fuzzyMatch(slice.Name, query) ||
76-
fuzzyMatch(slice.Package, query) ||
77-
fuzzyMatch(slice.String(), query)
74+
if !strings.HasSuffix(query, "*") {
75+
query += "*"
76+
}
77+
return strdist.GlobPath(slice.String(), query)
7878
}
7979

80-
// findSlices goes through the release searching for any slices that match
81-
// the query string. It returns a list of slices that match the query.
82-
func findSlices(release *setup.Release, queries []string) (slices []*setup.Slice, err error) {
80+
// findSlices returns slices from the provided release that match all of the
81+
// query strings (AND).
82+
func findSlices(release *setup.Release, query []string) (slices []*setup.Slice, err error) {
8383
for _, pkg := range release.Packages {
8484
for _, slice := range pkg.Slices {
8585
if slice == nil {
8686
continue
8787
}
8888
allMatch := true
89-
for _, query := range queries {
90-
if !match(slice, query) {
89+
for _, term := range query {
90+
if !match(slice, term) {
9191
allMatch = false
9292
break
9393
}

‎cmd/chisel/cmd_find_test.go‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,16 @@ var findTests = []findTest{{
8080
sampleRelease.Packages["python3.10"].Slices["utils"],
8181
},
8282
}, {
83-
summary: "Check partial matching",
83+
summary: "Check glob matching (*)",
8484
release: sampleRelease,
85-
query: []string{"python3.1x_bins"},
85+
query: []string{"python3.*_bins"},
86+
result: []*setup.Slice{
87+
sampleRelease.Packages["python3.10"].Slices["bins"],
88+
},
89+
}, {
90+
summary: "Check glob matching (?)",
91+
release: sampleRelease,
92+
query: []string{"python3.1?_bins"},
8693
result: []*setup.Slice{
8794
sampleRelease.Packages["python3.10"].Slices["bins"],
8895
},

0 commit comments

Comments
 (0)