Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support mid tag wildcard for enabling logger #31

Merged
merged 1 commit into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ When the value is set (ex. `DEBUG=example:*,tool:details` and/or `KEMBA=plugin:f

The value of these flags can be a simple regex alternative where a wildcard (`*`) are replaced with `.*` and all terms are prepended with `^` and appended with `$`. If a term does not include a wildcard, then an exact match it required.

Example of a wildcard in the middle of a tag string: `DEBUG=example:*:fxn` will match tags like `[example:tag1:fxn, example:tag2:fxn, example:anything:fxn, ...]`

To disabled colors, set the `NOCOLOR` environment variable to any value.

![image](https://user-images.githubusercontent.com/1429775/88557149-7973ff80-cfef-11ea-8ec2-ff332fd1b25f.png)
Expand Down
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ module github.com/clok/kemba
go 1.16

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gookit/color v1.5.1
github.com/kr/pretty v0.3.0
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/stretchr/testify v1.7.2
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
)
2 changes: 1 addition & 1 deletion kemba.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func determineEnabled(tag string, allowed string) bool {
for _, l := range strings.Split(allowed, ",") {
if strings.Contains(l, "*") {
reg := strings.ReplaceAll(l, "*", ".*")
if !strings.HasPrefix(reg, "^") {
if !strings.HasPrefix(reg, "^") && !strings.HasPrefix(reg, "*") {
reg = fmt.Sprintf("^%s", reg)
}

Expand Down
14 changes: 14 additions & 0 deletions kemba_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,25 @@ func Test_New(t *testing.T) {
})

t.Run("fuzzy match tag", func(t *testing.T) {
_ = os.Setenv("DEBUG", "*kemba*")

k := New("test:kemba:fail")
is.True(k.enabled, "Logger should be enabled")
})

t.Run("fuzzy match tag [failure]", func(t *testing.T) {
_ = os.Setenv("DEBUG", "*kemba")

k := New("test:kemba:fail")
is.False(k.enabled, "Logger should NOT be enabled")
})

t.Run("fuzzy match tag [mid star]", func(t *testing.T) {
_ = os.Setenv("DEBUG", "test:*:fail")

k := New("test:kemba:fail")
is.True(k.enabled, "Logger should be enabled")
})
}

func Example() {
Expand Down