Formatting abilities

This commit is contained in:
Alex Dadgar 2017-06-30 18:10:19 -07:00
parent 9ee5540d3c
commit 780de092ce
10 changed files with 158 additions and 208 deletions

View File

@ -85,41 +85,21 @@ func (c *AllocStatusCommand) Run(args []string) int {
}
// If args not specified but output format is specified, format and output the allocations data list
if len(args) == 0 {
var format string
if json && len(tmpl) > 0 {
c.Ui.Error("Both -json and -t are not allowed")
if len(args) == 0 && json || len(tmpl) > 0 {
allocs, _, err := client.Allocations().List(nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error querying allocations: %v", err))
return 1
} else if json {
format = "json"
} else if len(tmpl) > 0 {
format = "template"
}
if len(format) > 0 {
allocs, _, err := client.Allocations().List(nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error querying allocations: %v", err))
return 1
}
// Return nothing if no allocations found
if len(allocs) == 0 {
return 0
}
f, err := DataFormat(format, tmpl)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error getting formatter: %s", err))
return 1
}
out, err := f.TransformData(allocs)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error formatting the data: %s", err))
return 1
}
c.Ui.Output(out)
return 0
out, err := Format(json, tmpl, allocs)
if err != nil {
c.Ui.Error(err.Error())
return 1
}
c.Ui.Output(out)
return 0
}
if len(args) != 1 {
@ -179,27 +159,13 @@ func (c *AllocStatusCommand) Run(args []string) int {
}
// If output format is specified, format and output the data
var format string
if json && len(tmpl) > 0 {
c.Ui.Error("Both -json and -t are not allowed")
return 1
} else if json {
format = "json"
} else if len(tmpl) > 0 {
format = "template"
}
if len(format) > 0 {
f, err := DataFormat(format, tmpl)
if json || len(tmpl) > 0 {
out, err := Format(json, tmpl, alloc)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error getting formatter: %s", err))
c.Ui.Error(err.Error())
return 1
}
out, err := f.TransformData(alloc)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error formatting the data: %s", err))
return 1
}
c.Ui.Output(out)
return 0
}

View File

@ -73,3 +73,28 @@ func (p *TemplateFormat) TransformData(data interface{}) (string, error) {
}
return fmt.Sprint(out), nil
}
func Format(json bool, template string, data interface{}) (string, error) {
var format string
if json && len(template) > 0 {
return "", fmt.Errorf("Both json and template formatting are not allowed")
} else if json {
format = "json"
} else if len(template) > 0 {
format = "template"
} else {
return "", fmt.Errorf("no formatting option given")
}
f, err := DataFormat(format, template)
if err != nil {
return "", err
}
out, err := f.TransformData(data)
if err != nil {
return "", fmt.Errorf("Error formatting the data: %s", err)
}
return out, nil
}

View File

@ -23,6 +23,12 @@ General Options:
List Options:
-json
Output the deployments in a JSON format.
-t
Format and display the deployments using a Go template.
-verbose
Display full information.
`
@ -34,11 +40,14 @@ func (c *DeploymentListCommand) Synopsis() string {
}
func (c *DeploymentListCommand) Run(args []string) int {
var verbose bool
var json, verbose bool
var tmpl string
flags := c.Meta.FlagSet("deployment list", FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.BoolVar(&verbose, "verbose", false, "")
flags.BoolVar(&json, "json", false, "")
flags.StringVar(&tmpl, "t", "", "")
if err := flags.Parse(args); err != nil {
return 1
@ -70,6 +79,17 @@ func (c *DeploymentListCommand) Run(args []string) int {
return 1
}
if json || len(tmpl) > 0 {
out, err := Format(json, tmpl, deploys)
if err != nil {
c.Ui.Error(err.Error())
return 1
}
c.Ui.Output(out)
return 0
}
c.Ui.Output(formatDeployments(deploys, length))
return 0
}

View File

@ -28,10 +28,10 @@ Status Options:
Display full information.
-json
Output the allocation in its JSON format.
Output the deployment in its JSON format.
-t
Format and display allocation using a Go template.
Format and display deployment using a Go template.
`
return strings.TrimSpace(helpText)
}
@ -88,27 +88,13 @@ func (c *DeploymentStatusCommand) Run(args []string) int {
return 0
}
var format string
if json && len(tmpl) > 0 {
c.Ui.Error("Both -json and -t are not allowed")
return 1
} else if json {
format = "json"
} else if len(tmpl) > 0 {
format = "template"
}
if len(format) > 0 {
f, err := DataFormat(format, tmpl)
if json || len(tmpl) > 0 {
out, err := Format(json, tmpl, deploy)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error getting formatter: %s", err))
c.Ui.Error(err.Error())
return 1
}
out, err := f.TransformData(deploy)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error formatting the data: %s", err))
return 1
}
c.Ui.Output(out)
return 0
}

View File

@ -72,41 +72,21 @@ func (c *EvalStatusCommand) Run(args []string) int {
}
// If args not specified but output format is specified, format and output the evaluations data list
if len(args) == 0 {
var format string
if json && len(tmpl) > 0 {
c.Ui.Error("Both -json and -t are not allowed")
if len(args) == 0 && json || len(tmpl) > 0 {
evals, _, err := client.Evaluations().List(nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error querying evaluations: %v", err))
return 1
} else if json {
format = "json"
} else if len(tmpl) > 0 {
format = "template"
}
if len(format) > 0 {
evals, _, err := client.Evaluations().List(nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error querying evaluations: %v", err))
return 1
}
// Return nothing if no evaluations found
if len(evals) == 0 {
return 0
}
f, err := DataFormat(format, tmpl)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error getting formatter: %s", err))
return 1
}
out, err := f.TransformData(evals)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error formatting the data: %s", err))
return 1
}
c.Ui.Output(out)
return 0
out, err := Format(json, tmpl, evals)
if err != nil {
c.Ui.Error(err.Error())
return 1
}
c.Ui.Output(out)
return 0
}
if len(args) != 1 {
@ -175,24 +155,13 @@ func (c *EvalStatusCommand) Run(args []string) int {
}
// If output format is specified, format and output the data
var format string
if json {
format = "json"
} else if len(tmpl) > 0 {
format = "template"
}
if len(format) > 0 {
f, err := DataFormat(format, tmpl)
if json || len(tmpl) > 0 {
out, err := Format(json, tmpl, eval)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error getting formatter: %s", err))
c.Ui.Error(err.Error())
return 1
}
out, err := f.TransformData(eval)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error formatting the data: %s", err))
return 1
}
c.Ui.Output(out)
return 0
}

View File

@ -24,10 +24,10 @@ General Options:
Inspect Options:
-json
Output the evaluation in its JSON format.
Output the job in its JSON format.
-t
Format and display evaluation using a Go template.
Format and display job using a Go template.
`
return strings.TrimSpace(helpText)
}
@ -37,12 +37,12 @@ func (c *InspectCommand) Synopsis() string {
}
func (c *InspectCommand) Run(args []string) int {
var ojson bool
var json bool
var tmpl string
flags := c.Meta.FlagSet("inspect", FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.BoolVar(&ojson, "json", false, "")
flags.BoolVar(&json, "json", false, "")
flags.StringVar(&tmpl, "t", "", "")
if err := flags.Parse(args); err != nil {
@ -58,40 +58,21 @@ func (c *InspectCommand) Run(args []string) int {
}
// If args not specified but output format is specified, format and output the jobs data list
if len(args) == 0 {
var format string
if ojson && len(tmpl) > 0 {
c.Ui.Error("Both -json and -t are not allowed")
if len(args) == 0 && json || len(tmpl) > 0 {
jobs, _, err := client.Jobs().List(nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error querying jobs: %v", err))
return 1
} else if ojson {
format = "json"
} else if len(tmpl) > 0 {
format = "template"
}
if len(format) > 0 {
jobs, _, err := client.Jobs().List(nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error querying jobs: %v", err))
return 1
}
f, err := DataFormat(format, tmpl)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error getting formatter: %s", err))
return 1
}
// Return nothing if no jobs found
if len(jobs) == 0 {
return 0
}
out, err := f.TransformData(jobs)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error formatting the data: %s", err))
return 1
}
c.Ui.Output(out)
return 0
out, err := Format(json, tmpl, jobs)
if err != nil {
c.Ui.Error(err.Error())
return 1
}
c.Ui.Output(out)
return 0
}
// Check that we got exactly one job
@ -124,24 +105,13 @@ func (c *InspectCommand) Run(args []string) int {
}
// If output format is specified, format and output the data
var format string
if ojson {
format = "json"
} else if len(tmpl) > 0 {
format = "template"
}
if len(format) > 0 {
f, err := DataFormat(format, tmpl)
if json || len(tmpl) > 0 {
out, err := Format(json, tmpl, job)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error getting formatter: %s", err))
c.Ui.Error(err.Error())
return 1
}
out, err := f.TransformData(job)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error formatting the data: %s", err))
return 1
}
c.Ui.Output(out)
return 0
}

View File

@ -7,7 +7,6 @@ import (
type JobDeploymentsCommand struct {
Meta
formatter DataFormatter
}
func (c *JobDeploymentsCommand) Help() string {
@ -22,6 +21,12 @@ General Options:
Deployments Options:
-json
Output the deployments in a JSON format.
-t
Format and display deployments using a Go template.
-latest
Display the latest deployment only.
@ -36,12 +41,15 @@ func (c *JobDeploymentsCommand) Synopsis() string {
}
func (c *JobDeploymentsCommand) Run(args []string) int {
var latest, verbose bool
var json, latest, verbose bool
var tmpl string
flags := c.Meta.FlagSet("job deployments", FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.BoolVar(&latest, "latest", false, "")
flags.BoolVar(&verbose, "verbose", false, "")
flags.BoolVar(&json, "json", false, "")
flags.StringVar(&tmpl, "t", "", "")
if err := flags.Parse(args); err != nil {
return 1

View File

@ -38,6 +38,12 @@ History Options:
-job-version <job version>
Display only the history for the given job version.
-json
Output the job versions in a JSON format.
-t
Format and display the job versions using a Go template.
`
return strings.TrimSpace(helpText)
}
@ -47,14 +53,16 @@ func (c *JobHistoryCommand) Synopsis() string {
}
func (c *JobHistoryCommand) Run(args []string) int {
var diff, full bool
var versionStr string
var json, diff, full bool
var tmpl, versionStr string
flags := c.Meta.FlagSet("job history", FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.BoolVar(&diff, "p", false, "")
flags.BoolVar(&full, "full", false, "")
flags.BoolVar(&json, "json", false, "")
flags.StringVar(&versionStr, "job-version", "", "")
flags.StringVar(&tmpl, "t", "", "")
if err := flags.Parse(args); err != nil {
return 1
@ -67,6 +75,11 @@ func (c *JobHistoryCommand) Run(args []string) int {
return 1
}
if (json || len(tmpl) != 0) && (diff || full) {
c.Ui.Error("-json and -t are exclusive with -p and -full")
return 1
}
// Get the HTTP client
client, err := c.Meta.Client()
if err != nil {
@ -127,12 +140,34 @@ func (c *JobHistoryCommand) Run(args []string) int {
}
}
if json || len(tmpl) > 0 {
out, err := Format(json, tmpl, job)
if err != nil {
c.Ui.Error(err.Error())
return 1
}
c.Ui.Output(out)
return 0
}
if err := c.formatJobVersion(job, diff, nextVersion, full); err != nil {
c.Ui.Error(err.Error())
return 1
}
} else {
if json || len(tmpl) > 0 {
out, err := Format(json, tmpl, versions)
if err != nil {
c.Ui.Error(err.Error())
return 1
}
c.Ui.Output(out)
return 0
}
if err := c.formatJobVersions(versions, diffs, full); err != nil {
c.Ui.Error(err.Error())
return 1

View File

@ -7,7 +7,6 @@ import (
type JobRevertCommand struct {
Meta
formatter DataFormatter
}
func (c *JobRevertCommand) Help() string {

View File

@ -121,16 +121,6 @@ func (c *NodeStatusCommand) Run(args []string) int {
// Use list mode if no node name was provided
if len(args) == 0 && !c.self {
// If output format is specified, format and output the node data list
var format string
if c.json && len(c.tmpl) > 0 {
c.Ui.Error("Both -json and -t are not allowed")
return 1
} else if c.json {
format = "json"
} else if len(c.tmpl) > 0 {
format = "template"
}
// Query the node info
nodes, _, err := client.Nodes().List(nil)
@ -139,24 +129,20 @@ func (c *NodeStatusCommand) Run(args []string) int {
return 1
}
// Return nothing if no nodes found
if len(nodes) == 0 {
// If output format is specified, format and output the node data list
if c.json || len(c.tmpl) > 0 {
out, err := Format(c.json, c.tmpl, nodes)
if err != nil {
c.Ui.Error(err.Error())
return 1
}
c.Ui.Output(out)
return 0
}
if len(format) > 0 {
f, err := DataFormat(format, c.tmpl)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error getting formatter: %s", err))
return 1
}
out, err := f.TransformData(nodes)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error formatting the data: %s", err))
return 1
}
c.Ui.Output(out)
// Return nothing if no nodes found
if len(nodes) == 0 {
return 0
}
@ -256,27 +242,13 @@ func (c *NodeStatusCommand) Run(args []string) int {
}
// If output format is specified, format and output the data
var format string
if c.json && len(c.tmpl) > 0 {
c.Ui.Error("Both -json and -t are not allowed")
return 1
} else if c.json {
format = "json"
} else if len(c.tmpl) > 0 {
format = "template"
}
if len(format) > 0 {
f, err := DataFormat(format, c.tmpl)
if c.json || len(c.tmpl) > 0 {
out, err := Format(c.json, c.tmpl, node)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error getting formatter: %s", err))
c.Ui.Error(err.Error())
return 1
}
out, err := f.TransformData(node)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error formatting the data: %s", err))
return 1
}
c.Ui.Output(out)
return 0
}