Skip to content

Commit bd0beab

Browse files
committed
Add an --output option to kepctl query
Signed-off-by: Nabarun Pal <[email protected]>
1 parent 4571475 commit bd0beab

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

cmd/kepctl/query.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package main
1818

1919
import (
20+
"fmt"
21+
2022
"github.com/spf13/cobra"
2123

2224
"k8s.io/enhancements/pkg/kepctl"
@@ -43,6 +45,7 @@ func buildQueryCommand(k *kepctl.Client) *cobra.Command {
4345
f.StringSliceVar(&opts.Stage, "stage", nil, "Stage")
4446
f.StringSliceVar(&opts.PRRApprover, "prr", nil, "Prod Readiness Approver")
4547
f.BoolVar(&opts.IncludePRs, "include-prs", false, "Include PRs in the results")
48+
f.StringVar(&opts.Output, "output", kepctl.DefaultOutputOpt, fmt.Sprintf("Output format. Can be %v", kepctl.SupportedOutputOpts))
4649

4750
addRepoPathFlag(f, &opts.CommonArgs)
4851

pkg/kepctl/query.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,26 @@ import (
2626
"k8s.io/enhancements/pkg/kepval/keps/validations"
2727
)
2828

29+
var (
30+
// SupportedOutputOpts stores all allowed query output formats
31+
SupportedOutputOpts = []string{
32+
"table",
33+
"json",
34+
"yaml",
35+
}
36+
37+
// DefaultOutputOpt is the default output format for kepctl query
38+
DefaultOutputOpt = "table"
39+
)
40+
2941
type QueryOpts struct {
3042
CommonArgs
3143
SIG []string
3244
Status []string
3345
Stage []string
3446
PRRApprover []string
3547
IncludePRs bool
48+
Output string
3649
}
3750

3851
// Validate checks the args and cleans them up if needed
@@ -47,6 +60,12 @@ func (c *QueryOpts) Validate(args []string) error {
4760
}
4861
c.SIG = sigs
4962
}
63+
64+
// check if the Output specified is one of "", "json" or "yaml"
65+
if !sliceContains(SupportedOutputOpts, c.Output) {
66+
return fmt.Errorf("unsupported output format: %s. Valid values: %v", c.Output, SupportedOutputOpts)
67+
}
68+
5069
//TODO: check the valid values of stage, status, etc.
5170
return nil
5271
}
@@ -111,6 +130,16 @@ func sliceToMap(s []string) map[string]bool {
111130
return m
112131
}
113132

133+
func sliceContains(s []string, e string) bool {
134+
for _, k := range s {
135+
if k == e {
136+
return true
137+
}
138+
}
139+
140+
return false
141+
}
142+
114143
// returns all strings in vals that match at least one
115144
// regexp in regexps
116145
func selectByRegexp(vals []string, regexps []string) ([]string, error) {

0 commit comments

Comments
 (0)