-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
base-profiles: supports profiles with limited/no command usage
- Loading branch information
Showing
8 changed files
with
265 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package util | ||
|
||
import ( | ||
"path" | ||
"slices" | ||
"sync" | ||
|
||
"github.com/creativeprojects/clog" | ||
) | ||
|
||
// MultiPatternMatcher is a matcher for values using multiple patterns. The first matched pattern is returned | ||
type MultiPatternMatcher func(value string) (match bool, pattern string) | ||
|
||
// GlobMultiMatcher returns a function that matches path like values using a set of glob expressions | ||
func GlobMultiMatcher(patterns ...string) MultiPatternMatcher { | ||
patterns = slices.Clone(patterns) | ||
slices.Sort(patterns) | ||
slices.Compact(patterns) // unique | ||
slices.SortFunc(patterns, func(a, b string) int { return len(a) - len(b) }) // smallest first | ||
|
||
once := sync.Once{} | ||
|
||
return func(value string) (match bool, pattern string) { | ||
var err error | ||
for _, pattern = range patterns { | ||
match, err = path.Match(pattern, value) | ||
if err != nil { | ||
once.Do(func() { | ||
clog.Warningf("glob matcher (first error is logged): failed matching with %s: %s", pattern, err.Error()) | ||
}) | ||
} | ||
if match { | ||
return | ||
} | ||
} | ||
pattern = "" | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"slices" | ||
"testing" | ||
|
||
"github.com/creativeprojects/clog" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGlobMultiMatcher(t *testing.T) { | ||
t.Run("no-pattern-no-match", func(t *testing.T) { | ||
match, ptn := GlobMultiMatcher()("any") | ||
assert.False(t, match) | ||
assert.Empty(t, ptn) | ||
match, ptn = GlobMultiMatcher(nil...)("any") | ||
assert.False(t, match) | ||
assert.Empty(t, ptn) | ||
}) | ||
|
||
t.Run("matches-glob", func(t *testing.T) { | ||
tests := []struct { | ||
pattern, value string | ||
matches bool | ||
}{ | ||
{pattern: "", value: "", matches: true}, | ||
{pattern: "*", value: "", matches: true}, | ||
{pattern: "*", value: "value", matches: true}, | ||
{pattern: "", value: "value", matches: false}, | ||
{pattern: "[0-9]", value: "5", matches: true}, | ||
{pattern: "[0-9]", value: "10", matches: false}, | ||
{pattern: "direct", value: "direct", matches: true}, | ||
{pattern: "prefix", value: "prefix-suffix", matches: false}, | ||
{pattern: "suffix", value: "prefix-suffix", matches: false}, | ||
{pattern: "prefix*", value: "prefix-suffix", matches: true}, | ||
{pattern: "*suffix", value: "prefix-suffix", matches: true}, | ||
} | ||
for i, test := range tests { | ||
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { | ||
match, ptn := GlobMultiMatcher(test.pattern)(test.value) | ||
assert.Equal(t, match, test.matches, "%q / %q", test.pattern, test.value) | ||
if match { | ||
assert.Equal(t, test.pattern, ptn) | ||
} else { | ||
assert.Empty(t, ptn) | ||
} | ||
}) | ||
} | ||
}) | ||
|
||
t.Run("logs-first-error", func(t *testing.T) { | ||
defaultLogger := clog.GetDefaultLogger() | ||
defer clog.SetDefaultLogger(defaultLogger) | ||
|
||
log := clog.NewMemoryHandler() | ||
clog.SetDefaultLogger(clog.NewLogger(log)) | ||
|
||
invalidMatcher := GlobMultiMatcher("longer[", "*[") | ||
for run := 0; run < 10; run++ { | ||
invalidMatcher("longer-value") | ||
} | ||
|
||
assert.Equal(t, "glob matcher (first error is logged): failed matching with *[: syntax error in pattern", log.Pop()) | ||
assert.True(t, log.Empty()) | ||
}) | ||
|
||
t.Run("matches-shortest-first", func(t *testing.T) { | ||
patterns := []string{"-----*", "----*", "---*", "--*", "-*"} | ||
shuffle := func() { | ||
rand.Shuffle(len(patterns), func(i, j int) { patterns[i], patterns[j] = patterns[j], patterns[i] }) | ||
} | ||
for run := 0; run < 1000; run++ { | ||
shuffle() | ||
input := slices.Clone(patterns) | ||
matcher := GlobMultiMatcher(input...) | ||
for _, pattern := range patterns { | ||
match, ptn := matcher(pattern) | ||
assert.True(t, match) | ||
assert.Equal(t, "-*", ptn) | ||
} | ||
assert.Equal(t, input, patterns, "input must not change") | ||
} | ||
}) | ||
} |