command: helpers for columns

This commit is contained in:
Ryan Uber 2015-09-15 16:44:38 -07:00
parent c577cd2143
commit 1299fabf1f
4 changed files with 63 additions and 25 deletions

22
command/helpers.go Normal file
View File

@ -0,0 +1,22 @@
package command
import (
"github.com/ryanuber/columnize"
)
// formatKV takes a set of strings and formats them into properly
// aligned k = v pairs using the columnize library.
func formatKV(in []string) string {
columnConf := columnize.DefaultConfig()
columnConf.Glue = " = "
return columnize.Format(in, columnConf)
}
// formatList takes a set of strings and formats them into properly
// aligned output, replacing any blank fields with a placeholder
// for awk-ability.
func formatList(in []string) string {
columnConf := columnize.DefaultConfig()
columnConf.Empty = "<none>"
return columnize.Format(in, columnConf)
}

28
command/helpers_test.go Normal file
View File

@ -0,0 +1,28 @@
package command
import (
"testing"
)
func TestHelpers_FormatKV(t *testing.T) {
in := []string{"alpha|beta", "charlie|delta"}
out := formatKV(in)
expect := "alpha = beta\n"
expect += "charlie = delta"
if out != expect {
t.Fatalf("expect: %s, got: %s", expect, out)
}
}
func TestHelpers_FormatList(t *testing.T) {
in := []string{"alpha|beta||delta"}
out := formatList(in)
expect := "alpha beta <none> delta"
if out != expect {
t.Fatalf("expect: %s, got: %s", expect, out)
}
}

View File

@ -3,8 +3,6 @@ package command
import (
"fmt"
"strings"
"github.com/ryanuber/columnize"
)
type NodeStatusCommand struct {
@ -93,7 +91,7 @@ func (c *NodeStatusCommand) Run(args []string) int {
}
// Dump the output
c.Ui.Output(columnize.SimpleFormat(out))
c.Ui.Output(formatList(out))
return 0
}
@ -105,10 +103,6 @@ func (c *NodeStatusCommand) Run(args []string) int {
return 1
}
// Make the column config so we can dump k = v pairs
columnConf := columnize.DefaultConfig()
columnConf.Glue = " = "
// Format the output
basic := []string{
fmt.Sprintf("ID | %s", node.ID),
@ -144,10 +138,10 @@ func (c *NodeStatusCommand) Run(args []string) int {
}
// Dump the output
c.Ui.Output(columnize.Format(basic, columnConf))
c.Ui.Output(formatKV(basic))
if !short {
c.Ui.Output("\nAllocations")
c.Ui.Output(columnize.SimpleFormat(allocs))
c.Ui.Output(formatList(allocs))
}
return 0
}

View File

@ -3,8 +3,6 @@ package command
import (
"fmt"
"strings"
"github.com/ryanuber/columnize"
)
type StatusCommand struct {
@ -83,7 +81,7 @@ func (c *StatusCommand) Run(args []string) int {
job.Priority,
job.Status)
}
c.Ui.Output(columnize.SimpleFormat(out))
c.Ui.Output(formatList(out))
return 0
}
@ -95,18 +93,14 @@ func (c *StatusCommand) Run(args []string) int {
return 1
}
// Make the column config so we can dump k = v pairs
columnConf := columnize.DefaultConfig()
columnConf.Glue = " = "
// Format the job info
basic := []string{
fmt.Sprintf("ID | %s", job.ID),
fmt.Sprintf("Name | %s", job.Name),
fmt.Sprintf("Type | %s", job.Type),
fmt.Sprintf("Priority | %d", job.Priority),
fmt.Sprintf("Datacenters | %s", strings.Join(job.Datacenters, ",")),
fmt.Sprintf("Status | %s", job.Status),
fmt.Sprintf("ID|%s", job.ID),
fmt.Sprintf("Name|%s", job.Name),
fmt.Sprintf("Type|%s", job.Type),
fmt.Sprintf("Priority|%d", job.Priority),
fmt.Sprintf("Datacenters|%s", strings.Join(job.Datacenters, ",")),
fmt.Sprintf("Status|%s", job.Status),
}
var evals, allocs []string
@ -153,12 +147,12 @@ func (c *StatusCommand) Run(args []string) int {
}
// Dump the output
c.Ui.Output(columnize.Format(basic, columnConf))
c.Ui.Output(formatKV(basic))
if !short {
c.Ui.Output("\nEvaluations")
c.Ui.Output(columnize.SimpleFormat(evals))
c.Ui.Output(formatList(evals))
c.Ui.Output("\nAllocations")
c.Ui.Output(columnize.SimpleFormat(allocs))
c.Ui.Output(formatList(allocs))
}
return 0
}