Skip to content

Commit 7404c28

Browse files
authored
fix: match kv adapt glob expression (#110)
1 parent 8efee0e commit 7404c28

3 files changed

Lines changed: 35 additions & 7 deletions

File tree

pkg/tools/tools.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"strconv"
2222
"strings"
2323

24+
"github.com/gobwas/glob"
25+
2426
"github.com/TencentBlueKing/bk-bscp/pkg/i18n"
2527
"github.com/TencentBlueKing/bk-bscp/pkg/kit"
2628
)
@@ -287,8 +289,11 @@ func MatchPattern(name string, match []string) bool {
287289
}
288290

289291
for _, m := range match {
290-
ok, _ := path.Match(m, name)
291-
if ok {
292+
g, err := glob.Compile(m)
293+
if err != nil {
294+
continue
295+
}
296+
if g.Match(name) {
292297
return true
293298
}
294299
}

pkg/tools/tools_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,26 @@ func TestSplitPathAndName(t *testing.T) {
141141
}
142142
}
143143
}
144+
145+
func TestMatch(t *testing.T) {
146+
tests := []struct {
147+
input string
148+
patterns []string
149+
expected bool
150+
}{
151+
{"file.txt", []string{"*.txt"}, true},
152+
{"file.txt", []string{"file.txt"}, true},
153+
{"file.txt", []string{"file.*"}, true},
154+
{"file.txt", []string{"file"}, false},
155+
{"file.txt", []string{"file.txt*"}, true},
156+
{"key1", []string{"{key1,key2}"}, true},
157+
{"key2", []string{"{key1,key2}"}, true},
158+
{"key3", []string{"key1", "key2", "key3"}, true},
159+
}
160+
for _, test := range tests {
161+
result := MatchPattern(test.input, test.patterns)
162+
if result != test.expected {
163+
t.Errorf("MatchPattern(%s, %v) = %t, want %t", test.input, test.patterns, result, test.expected)
164+
}
165+
}
166+
}

pkg/types/cache.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ package types
1414

1515
import (
1616
"fmt"
17-
"path"
1817
"strings"
1918
"time"
2019

20+
"github.com/gobwas/glob"
2121
"github.com/samber/lo"
2222

2323
"github.com/TencentBlueKing/bk-bscp/pkg/dal/table"
@@ -175,12 +175,12 @@ func (c *CredentialCache) MatchKv(app, key string) bool {
175175
return false
176176
}
177177

178-
for _, v := range scopes {
179-
ok, err := path.Match(v, key)
178+
for _, s := range scopes {
179+
g, err := glob.Compile(s)
180180
if err != nil {
181-
return false
181+
continue
182182
}
183-
if ok {
183+
if g.Match(key) {
184184
return true
185185
}
186186
}

0 commit comments

Comments
 (0)