Skip to content

Commit

Permalink
[ref] Use olekukonko/tablewriter (initial pass) (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
elithrar committed Oct 25, 2017
1 parent f31bc3a commit f04a498
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
11 changes: 11 additions & 0 deletions cmd/flarectl/flarectl.go
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"strings"

"github.com/olekukonko/tablewriter"

"github.com/pkg/errors"

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

func writeTable(data [][]string, cols ...string) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(cols)
table.SetBorder(false)
table.AppendBulk(data)

table.Render()
}

// Print a nicely-formatted table
func makeTable(zones []table, cols ...string) {
// Store the maximum length of all columns
Expand Down
30 changes: 15 additions & 15 deletions cmd/flarectl/user_agent.go
Expand Up @@ -9,13 +9,13 @@ import (
"github.com/codegangsta/cli"
)

func formatUserAgentRule(rule cloudflare.UserAgentRule) table {
return table{
"ID": rule.ID,
"Description": rule.Description,
"Mode": rule.Mode,
"Value": rule.Configuration.Value,
"Paused": strconv.FormatBool(rule.Paused),
func formatUserAgentRule(rule cloudflare.UserAgentRule) []string {
return []string{
rule.ID,
rule.Description,
rule.Mode,
rule.Configuration.Value,
strconv.FormatBool(rule.Paused),
}
}

Expand Down Expand Up @@ -52,11 +52,11 @@ func userAgentCreate(c *cli.Context) {
return
}

output := []table{
output := [][]string{
formatUserAgentRule(resp.Result),
}

makeTable(output, "ID", "Description", "Mode", "Value", "Paused")
writeTable(output, "ID", "Description", "Mode", "Value", "Paused")
}

func userAgentUpdate(c *cli.Context) {
Expand Down Expand Up @@ -91,11 +91,11 @@ func userAgentUpdate(c *cli.Context) {
return
}

output := []table{
output := [][]string{
formatUserAgentRule(resp.Result),
}

makeTable(output, "ID", "Description", "Mode", "Value", "Paused")
writeTable(output, "ID", "Description", "Mode", "Value", "Paused")
}

func userAgentDelete(c *cli.Context) {
Expand All @@ -120,11 +120,11 @@ func userAgentDelete(c *cli.Context) {
return
}

output := []table{
output := [][]string{
formatUserAgentRule(resp.Result),
}

makeTable(output, "ID", "Description", "Mode", "Value", "Paused")
writeTable(output, "ID", "Description", "Mode", "Value", "Paused")
}

func userAgentList(c *cli.Context) {
Expand All @@ -149,10 +149,10 @@ func userAgentList(c *cli.Context) {
return
}

output := make([]table, 0, len(resp.Result))
output := make([][]string, 0, len(resp.Result))
for _, rule := range resp.Result {
output = append(output, formatUserAgentRule(rule))
}

makeTable(output, "ID", "Description", "Mode", "Value", "Paused")
writeTable(output, "ID", "Description", "Mode", "Value", "Paused")
}

0 comments on commit f04a498

Please sign in to comment.