@@ -26,13 +26,31 @@ import (
26
26
"k8s.io/enhancements/pkg/kepval/keps/validations"
27
27
)
28
28
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
+ StructuredOutputFormats = []string {
41
+ "json" ,
42
+ "yaml" ,
43
+ }
44
+ )
45
+
29
46
type QueryOpts struct {
30
47
CommonArgs
31
48
SIG []string
32
49
Status []string
33
50
Stage []string
34
51
PRRApprover []string
35
52
IncludePRs bool
53
+ Output string
36
54
}
37
55
38
56
// Validate checks the args and cleans them up if needed
@@ -47,14 +65,33 @@ func (c *QueryOpts) Validate(args []string) error {
47
65
}
48
66
c .SIG = sigs
49
67
}
68
+
69
+ // check if the Output specified is one of "", "json" or "yaml"
70
+ if ! sliceContains (SupportedOutputOpts , c .Output ) {
71
+ return fmt .Errorf ("unsupported output format: %s. Valid values: %v" , c .Output , SupportedOutputOpts )
72
+ }
73
+
50
74
//TODO: check the valid values of stage, status, etc.
51
75
return nil
52
76
}
53
77
54
78
// Query searches the local repo and possibly GitHub for KEPs
55
79
// that match the search criteria.
56
80
func (c * Client ) Query (opts QueryOpts ) error {
57
- fmt .Fprintf (c .Out , "Searching for KEPs...\n " )
81
+ // if output format is json/yaml, suppress other outputs
82
+ // json/yaml are structured formats, logging events which
83
+ // do not conform to the spec will create formatting issues
84
+ var suppressOutputs bool
85
+ if sliceContains (StructuredOutputFormats , opts .Output ) {
86
+ suppressOutputs = true
87
+ } else {
88
+ suppressOutputs = false
89
+ }
90
+
91
+ if ! suppressOutputs {
92
+ fmt .Fprintf (c .Out , "Searching for KEPs...\n " )
93
+ }
94
+
58
95
repoPath , err := c .findEnhancementsRepo (opts .CommonArgs )
59
96
if err != nil {
60
97
return errors .Wrap (err , "unable to search KEPs" )
@@ -99,7 +136,18 @@ func (c *Client) Query(opts QueryOpts) error {
99
136
keep = append (keep , k )
100
137
}
101
138
102
- c .PrintTable (DefaultPrintConfigs ("LastUpdated" , "Stage" , "Status" , "SIG" , "Authors" , "Title" , "Link" ), keep )
139
+ switch opts .Output {
140
+ case "table" :
141
+ c .PrintTable (DefaultPrintConfigs ("LastUpdated" , "Stage" , "Status" , "SIG" , "Authors" , "Title" , "Link" ), keep )
142
+ case "yaml" :
143
+ c .PrintYAML (keep )
144
+ case "json" :
145
+ c .PrintJSON (keep )
146
+ default :
147
+ // this check happens as a validation step in cobra as well
148
+ // added it for additional verbosity
149
+ return fmt .Errorf ("unsupported output format: %s. Valid values: %s" , opts .Output , SupportedOutputOpts )
150
+ }
103
151
return nil
104
152
}
105
153
@@ -111,6 +159,16 @@ func sliceToMap(s []string) map[string]bool {
111
159
return m
112
160
}
113
161
162
+ func sliceContains (s []string , e string ) bool {
163
+ for _ , k := range s {
164
+ if k == e {
165
+ return true
166
+ }
167
+ }
168
+
169
+ return false
170
+ }
171
+
114
172
// returns all strings in vals that match at least one
115
173
// regexp in regexps
116
174
func selectByRegexp (vals []string , regexps []string ) ([]string , error ) {
0 commit comments