Skip to content

Commit d42c1f5

Browse files
authored
Merge pull request kubernetes#1957 from johnbelamaric/query-regex
Use a regexp instead of strings for SIGs in query
2 parents ae30531 + bd3b74b commit d42c1f5

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

pkg/kepctl/query.go

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ package kepctl
1818

1919
import (
2020
"fmt"
21-
"strings"
21+
"regexp"
2222

2323
"github.com/pkg/errors"
2424

2525
"k8s.io/enhancements/pkg/kepval/keps"
26+
"k8s.io/enhancements/pkg/kepval/keps/validations"
2627
)
2728

2829
type QueryOpts struct {
@@ -36,15 +37,14 @@ type QueryOpts struct {
3637
// Validate checks the args and cleans them up if needed
3738
func (c *QueryOpts) Validate(args []string) error {
3839
if len(c.SIG) > 0 {
39-
var fixed []string
40-
for _, s := range c.SIG {
41-
if strings.HasPrefix(s, "sig-") {
42-
fixed = append(fixed, s)
43-
} else {
44-
fixed = append(fixed, "sig-"+s)
45-
}
40+
sigs, err := selectByRegexp(validations.Sigs(), c.SIG)
41+
if err != nil {
42+
return err
4643
}
47-
c.SIG = fixed
44+
if len(sigs) == 0 {
45+
return fmt.Errorf("No SIG matches any of the passed regular expressions")
46+
}
47+
c.SIG = sigs
4848
}
4949
//TODO: check the valid values of stage, status, etc.
5050
return nil
@@ -67,13 +67,13 @@ func (c *Client) Query(opts QueryOpts) error {
6767
// KEPs in the local filesystem
6868
names, err := findLocalKEPs(repoPath, sig)
6969
if err != nil {
70-
return errors.Wrap(err, "unable to search for local KEPs")
70+
fmt.Fprintf(c.Err, "error searching for local KEPs from %s: %s\n", sig, err)
7171
}
7272

7373
for _, k := range names {
7474
kep, err := c.readKEP(repoPath, sig, k)
7575
if err != nil {
76-
fmt.Fprintf(c.Err, "ERROR READING KEP %s: %s\n", k, err)
76+
fmt.Fprintf(c.Err, "error reading KEP %s: %s\n", k, err)
7777
} else {
7878
allKEPs = append(allKEPs, kep)
7979
}
@@ -83,7 +83,7 @@ func (c *Client) Query(opts QueryOpts) error {
8383
if opts.IncludePRs {
8484
prKeps, err := c.findKEPPullRequests(sig)
8585
if err != nil {
86-
return errors.Wrap(err, "unable to search for KEP PRs")
86+
fmt.Fprintf(c.Err, "error searching for KEP PRs from %s: %s\n", sig, err)
8787
}
8888
if prKeps != nil {
8989
allKEPs = append(allKEPs, prKeps...)
@@ -117,3 +117,22 @@ func sliceToMap(s []string) map[string]bool {
117117
}
118118
return m
119119
}
120+
121+
// returns all strings in vals that match at least one
122+
// regexp in regexps
123+
func selectByRegexp(vals []string, regexps []string) ([]string, error) {
124+
var matches []string
125+
for _, s := range vals {
126+
for _, r := range regexps {
127+
found, err := regexp.MatchString(r, s)
128+
if err != nil {
129+
return matches, err
130+
}
131+
if found {
132+
matches = append(matches, s)
133+
break
134+
}
135+
}
136+
}
137+
return matches, nil
138+
}

pkg/kepval/keps/validations/yaml.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ var (
9494
prrApprovers []string
9595
)
9696

97+
func Sigs() []string {
98+
return listGroups
99+
}
100+
97101
func init() {
98102
resp, err := http.Get("https://raw.githubusercontent.com/kubernetes/community/master/sigs.yaml")
99103
if err != nil {

0 commit comments

Comments
 (0)