-
Notifications
You must be signed in to change notification settings - Fork 592
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19839 from daisukebe/rpk-describe-group-regex
rpk: group describe supporting regex
- Loading branch information
Showing
5 changed files
with
178 additions
and
87 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2024 Redpanda Data, Inc. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.md | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0 | ||
|
||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
// RegexListedItems returns items that match the 'expressions' from the 'list'. | ||
func RegexListedItems(list, expressions []string) ([]string, error) { | ||
var compiled []*regexp.Regexp | ||
for _, expression := range expressions { | ||
if !strings.HasPrefix(expression, "^") { | ||
expression = "^" + expression | ||
} | ||
if !strings.HasSuffix(expression, "$") { | ||
expression += "$" | ||
} | ||
re, err := regexp.Compile(expression) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to compile regex %q: %w", expression, err) | ||
} | ||
compiled = append(compiled, re) | ||
} | ||
|
||
var matched []string | ||
for _, re := range compiled { | ||
remaining := list[:0] | ||
for _, item := range list { | ||
if re.MatchString(item) { | ||
matched = append(matched, item) | ||
} else { | ||
remaining = append(remaining, item) | ||
} | ||
} | ||
list = remaining | ||
if len(list) == 0 { | ||
break | ||
} | ||
} | ||
return matched, nil | ||
} |
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,88 @@ | ||
// Copyright 2024 Redpanda Data, Inc. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.md | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0 | ||
|
||
package utils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRegexListedItems(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
list []string | ||
exprs []string | ||
want []string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "no list, no expressions: no error", | ||
}, | ||
|
||
{ | ||
name: "list, no expressions: no change", | ||
list: []string{"foo", "bar"}, | ||
}, | ||
|
||
{ | ||
name: "matching ^f", | ||
list: []string{"foo", "bar", "fdsa"}, | ||
exprs: []string{"^f.*"}, | ||
want: []string{"foo", "fdsa"}, | ||
}, | ||
|
||
{ | ||
name: "matching three", | ||
list: []string{"foo", "bar", "biz", "baz", "buzz"}, | ||
exprs: []string{".a.", "^f.."}, | ||
want: []string{"bar", "baz", "foo"}, | ||
}, | ||
|
||
{ | ||
name: "dot matches nothing by default, because we anchor with ^ and $", | ||
list: []string{"foo", "bar", "biz", "baz", "buzz"}, | ||
exprs: []string{"."}, | ||
}, | ||
|
||
{ | ||
name: ".* matches everything", | ||
list: []string{"foo", "bar", "biz", "baz", "buzz"}, | ||
exprs: []string{".*"}, | ||
want: []string{"foo", "bar", "biz", "baz", "buzz"}, | ||
}, | ||
|
||
{ | ||
name: "* matches everything", | ||
list: []string{"foo", "bar", "biz", "baz", "buzz"}, | ||
exprs: []string{".*"}, | ||
want: []string{"foo", "bar", "biz", "baz", "buzz"}, | ||
}, | ||
|
||
{ | ||
name: "no list", | ||
exprs: []string{"as[df"}, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := RegexListedItems(tt.list, tt.exprs) | ||
gotErr := err != nil | ||
if gotErr != tt.wantErr { | ||
t.Errorf("got err? %v, exp? %v", gotErr, tt.wantErr) | ||
} | ||
if tt.wantErr { | ||
return | ||
} | ||
require.Equal(t, tt.want, got, "got topics != expected") | ||
}) | ||
} | ||
} |