Skip to content

Commit 23bbfe4

Browse files
committed
Review feedback; use tablewriter library
1 parent 0d17860 commit 23bbfe4

File tree

5 files changed

+64
-30
lines changed

5 files changed

+64
-30
lines changed

cmd/kepctl/query.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package main
218

319
import (
420
"github.com/spf13/cobra"
21+
522
"k8s.io/enhancements/pkg/kepctl"
623
)
724

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.12
44

55
require (
66
github.com/google/go-github/v32 v32.1.0
7+
github.com/olekukonko/tablewriter v0.0.4
78
github.com/pkg/errors v0.8.0
89
github.com/spf13/cobra v1.0.0
910
github.com/spf13/pflag v1.0.3

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,15 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
6060
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
6161
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
6262
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
63+
github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
64+
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
6365
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
6466
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
6567
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
6668
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
6769
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
70+
github.com/olekukonko/tablewriter v0.0.4 h1:vHD/YYe1Wolo78koG299f7V/VAS08c6IpCLn+Ejf/w8=
71+
github.com/olekukonko/tablewriter v0.0.4/go.mod h1:zq6QwlOf5SlnkVbMSr5EoBv3636FWnp+qbPhuoO21uA=
6872
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
6973
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
7074
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=

pkg/kepctl/kepctl.go

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ import (
2828
"strings"
2929

3030
"github.com/google/go-github/v32/github"
31+
"github.com/olekukonko/tablewriter"
3132
"gopkg.in/yaml.v2"
33+
3234
"k8s.io/enhancements/pkg/kepval/keps"
3335
)
3436

@@ -173,7 +175,7 @@ func findLocalKEPs(repoPath string, sig string) ([]string, error) {
173175
return keps, err
174176
}
175177

176-
func (c *Client) findPRKEPs(sig string) (*keps.Proposal, error) {
178+
func (c *Client) findKEPPullRequests(sig string) (*keps.Proposal, error) {
177179
gh := github.NewClient(nil)
178180
pulls, _, err := gh.PullRequests.List(context.Background(), "kubernetes", "enhancements", &github.PullRequestListOptions{})
179181
if err != nil {
@@ -266,42 +268,39 @@ func (c *Client) writeKEP(kep *keps.Proposal, opts CommonArgs) error {
266268

267269
type PrintConfig interface {
268270
Title() string
269-
Format() string
270271
Value(*keps.Proposal) string
271272
}
272273

273274
type printConfig struct {
274275
title string
275-
format string
276276
valueFunc func(*keps.Proposal) string
277277
}
278278

279-
func (p *printConfig) Title() string { return p.title }
280-
func (p *printConfig) Format() string { return p.format }
279+
func (p *printConfig) Title() string { return p.title }
281280
func (p *printConfig) Value(k *keps.Proposal) string {
282281
return p.valueFunc(k)
283282
}
284283

285-
var dfltConfig = map[string]printConfig{
286-
"Authors": {"Authors", "%-30s", func(k *keps.Proposal) string { return strings.Join(k.Authors, ", ") }},
287-
"LastUpdated": {"Updated", "%-10s", func(k *keps.Proposal) string { return k.LastUpdated }},
288-
"SIG": {"SIG", "%-12s", func(k *keps.Proposal) string {
284+
var defaultConfig = map[string]printConfig{
285+
"Authors": {"Authors", func(k *keps.Proposal) string { return strings.Join(k.Authors, ", ") }},
286+
"LastUpdated": {"Updated", func(k *keps.Proposal) string { return k.LastUpdated }},
287+
"SIG": {"SIG", func(k *keps.Proposal) string {
289288
if strings.HasPrefix(k.OwningSIG, "sig-") {
290289
return k.OwningSIG[4:]
291290
} else {
292291
return k.OwningSIG
293292
}
294293
}},
295-
"Stage": {"Stage", "%-6s", func(k *keps.Proposal) string { return k.Stage }},
296-
"Status": {"Status", "%-16s", func(k *keps.Proposal) string { return k.Status }},
297-
"Title": {"Title", "%-30s", func(k *keps.Proposal) string { return k.Title }},
294+
"Stage": {"Stage", func(k *keps.Proposal) string { return k.Stage }},
295+
"Status": {"Status", func(k *keps.Proposal) string { return k.Status }},
296+
"Title": {"Title", func(k *keps.Proposal) string { return k.Title }},
298297
}
299298

300299
func DefaultPrintConfigs(names ...string) []PrintConfig {
301300
var configs []PrintConfig
302301
for _, n := range names {
303302
// copy to allow it to be tweaked by the caller
304-
c := dfltConfig[n]
303+
c := defaultConfig[n]
305304
configs = append(configs, &c)
306305
}
307306
return configs
@@ -312,24 +311,21 @@ func (c *Client) PrintTable(configs []PrintConfig, proposals []*keps.Proposal) {
312311
return
313312
}
314313

315-
fstr := configs[0].Format()
316-
for _, c := range configs[1:] {
317-
fstr += " " + c.Format()
318-
}
319-
fstr += "\n"
320-
321-
fmt.Fprintf(c.Out, fstr, mapPrintConfigs(configs, func(p PrintConfig) string { return p.Title() })...)
322-
fmt.Fprintf(c.Out, fstr, mapPrintConfigs(configs, func(p PrintConfig) string { return strings.Repeat("-", len(p.Title())) })...)
314+
table := tablewriter.NewWriter(c.Out)
323315

324-
for _, k := range proposals {
325-
fmt.Fprintf(c.Out, fstr, mapPrintConfigs(configs, func(p PrintConfig) string { return p.Value(k) })...)
316+
var headers []string
317+
for _, c := range configs {
318+
headers = append(headers, c.Title())
326319
}
327-
}
320+
table.SetHeader(headers)
321+
table.SetAlignment(tablewriter.ALIGN_LEFT)
328322

329-
func mapPrintConfigs(configs []PrintConfig, mapFunc func(p PrintConfig) string) []interface{} {
330-
var s []interface{}
331-
for _, c := range configs {
332-
s = append(s, mapFunc(c))
323+
for _, k := range proposals {
324+
var s []string
325+
for _, c := range configs {
326+
s = append(s, c.Value(k))
327+
}
328+
table.Append(s)
333329
}
334-
return s
330+
table.Render()
335331
}

pkg/kepctl/query.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package kepctl
218

319
import (
@@ -63,7 +79,7 @@ func (c *Client) Query(opts QueryOpts) error {
6379

6480
// Open PRs; existing KEPs with open PRs will be shown twice
6581
if opts.IncludePRs {
66-
prKeps, err := c.findPRKEPs(sig)
82+
prKeps, err := c.findKEPPullRequests(sig)
6783
if err != nil {
6884
return errors.Wrap(err, "unable to search for KEP PRs")
6985
}

0 commit comments

Comments
 (0)