Skip to content

Commit f04a498

Browse files
authored
[ref] Use olekukonko/tablewriter (initial pass) (cloudflare#149)
1 parent f31bc3a commit f04a498

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

cmd/flarectl/flarectl.go

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"os"
77
"strings"
88

9+
"github.com/olekukonko/tablewriter"
10+
911
"github.com/pkg/errors"
1012

1113
"github.com/cloudflare/cloudflare-go"
@@ -17,6 +19,15 @@ var api *cloudflare.API
1719
// Map type used for printing a table
1820
type table map[string]string
1921

22+
func writeTable(data [][]string, cols ...string) {
23+
table := tablewriter.NewWriter(os.Stdout)
24+
table.SetHeader(cols)
25+
table.SetBorder(false)
26+
table.AppendBulk(data)
27+
28+
table.Render()
29+
}
30+
2031
// Print a nicely-formatted table
2132
func makeTable(zones []table, cols ...string) {
2233
// Store the maximum length of all columns

cmd/flarectl/user_agent.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import (
99
"github.com/codegangsta/cli"
1010
)
1111

12-
func formatUserAgentRule(rule cloudflare.UserAgentRule) table {
13-
return table{
14-
"ID": rule.ID,
15-
"Description": rule.Description,
16-
"Mode": rule.Mode,
17-
"Value": rule.Configuration.Value,
18-
"Paused": strconv.FormatBool(rule.Paused),
12+
func formatUserAgentRule(rule cloudflare.UserAgentRule) []string {
13+
return []string{
14+
rule.ID,
15+
rule.Description,
16+
rule.Mode,
17+
rule.Configuration.Value,
18+
strconv.FormatBool(rule.Paused),
1919
}
2020
}
2121

@@ -52,11 +52,11 @@ func userAgentCreate(c *cli.Context) {
5252
return
5353
}
5454

55-
output := []table{
55+
output := [][]string{
5656
formatUserAgentRule(resp.Result),
5757
}
5858

59-
makeTable(output, "ID", "Description", "Mode", "Value", "Paused")
59+
writeTable(output, "ID", "Description", "Mode", "Value", "Paused")
6060
}
6161

6262
func userAgentUpdate(c *cli.Context) {
@@ -91,11 +91,11 @@ func userAgentUpdate(c *cli.Context) {
9191
return
9292
}
9393

94-
output := []table{
94+
output := [][]string{
9595
formatUserAgentRule(resp.Result),
9696
}
9797

98-
makeTable(output, "ID", "Description", "Mode", "Value", "Paused")
98+
writeTable(output, "ID", "Description", "Mode", "Value", "Paused")
9999
}
100100

101101
func userAgentDelete(c *cli.Context) {
@@ -120,11 +120,11 @@ func userAgentDelete(c *cli.Context) {
120120
return
121121
}
122122

123-
output := []table{
123+
output := [][]string{
124124
formatUserAgentRule(resp.Result),
125125
}
126126

127-
makeTable(output, "ID", "Description", "Mode", "Value", "Paused")
127+
writeTable(output, "ID", "Description", "Mode", "Value", "Paused")
128128
}
129129

130130
func userAgentList(c *cli.Context) {
@@ -149,10 +149,10 @@ func userAgentList(c *cli.Context) {
149149
return
150150
}
151151

152-
output := make([]table, 0, len(resp.Result))
152+
output := make([][]string, 0, len(resp.Result))
153153
for _, rule := range resp.Result {
154154
output = append(output, formatUserAgentRule(rule))
155155
}
156156

157-
makeTable(output, "ID", "Description", "Mode", "Value", "Paused")
157+
writeTable(output, "ID", "Description", "Mode", "Value", "Paused")
158158
}

0 commit comments

Comments
 (0)