Skip to content

Commit c2f3b90

Browse files
authored
Merge pull request kubernetes#2322 from palnabarun/kepctl-query-output-csv
kepctl: add CSV output support to query subcommand
2 parents 8f41a6e + 2d893e4 commit c2f3b90

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

pkg/repo/output.go

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

1919
import (
20+
"encoding/csv"
2021
"encoding/json"
2122
"fmt"
2223
"strings"
@@ -130,3 +131,25 @@ func (r *Repo) PrintJSON(proposals []*api.Proposal) {
130131

131132
fmt.Fprintln(r.Out, string(data))
132133
}
134+
135+
// PrintCSV outputs keps array as CSV to r.Out
136+
func (r *Repo) PrintCSV(configs []PrintConfig, proposals []*api.Proposal) {
137+
w := csv.NewWriter(r.Out)
138+
defer w.Flush()
139+
140+
var headers []string
141+
for _, c := range configs {
142+
headers = append(headers, c.Title())
143+
}
144+
w.Write(headers)
145+
146+
for _, p := range proposals {
147+
var row []string
148+
for _, c := range configs {
149+
row = append(row, c.Value(p))
150+
}
151+
if err := w.Write(row); err != nil {
152+
fmt.Fprintf(r.Err, "error printing keps as CSV: %s", err)
153+
}
154+
}
155+
}

pkg/repo/query.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ var (
3232
"table",
3333
"json",
3434
"yaml",
35+
"csv",
3536
}
3637

3738
// DefaultOutputOpt is the default output format for kepctl query
@@ -40,6 +41,7 @@ var (
4041
StructuredOutputFormats = []string{
4142
"json",
4243
"yaml",
44+
"csv",
4345
}
4446
)
4547

@@ -194,6 +196,8 @@ func (r *Repo) Query(opts *QueryOpts) error {
194196
r.PrintYAML(results)
195197
case "json":
196198
r.PrintJSON(results)
199+
case "csv":
200+
r.PrintCSV(DefaultPrintConfigs("Title", "Authors", "SIG", "Stage", "Status", "LastUpdated", "Link"), results)
197201
default:
198202
// this check happens as a validation step in cobra as well
199203
// added it for additional verbosity

0 commit comments

Comments
 (0)