open-nomad/command/deployment_list.go

128 lines
2.6 KiB
Go
Raw Normal View History

2017-06-30 17:59:19 +00:00
package command
import (
"fmt"
"strings"
"github.com/hashicorp/nomad/api"
2017-08-18 15:09:52 +00:00
"github.com/posener/complete"
2017-06-30 17:59:19 +00:00
)
type DeploymentListCommand struct {
Meta
}
func (c *DeploymentListCommand) Help() string {
helpText := `
Usage: nomad deployment list [options]
List is used to list the set of deployments tracked by Nomad.
General Options:
` + generalOptionsUsage() + `
List Options:
2017-07-01 01:10:19 +00:00
-json
Output the deployments in a JSON format.
-t
Format and display the deployments using a Go template.
2017-06-30 17:59:19 +00:00
-verbose
Display full information.
`
return strings.TrimSpace(helpText)
}
2017-08-18 15:09:52 +00:00
func (c *DeploymentListCommand) AutocompleteFlags() complete.Flags {
2017-08-23 21:56:21 +00:00
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
complete.Flags{
"-json": complete.PredictNothing,
"-t": complete.PredictAnything,
"-verbose": complete.PredictNothing,
})
2017-08-18 15:09:52 +00:00
}
func (c *DeploymentListCommand) AutocompleteArgs() complete.Predictor {
return complete.PredictNothing
}
2017-06-30 17:59:19 +00:00
func (c *DeploymentListCommand) Synopsis() string {
return "List all deployments"
}
func (c *DeploymentListCommand) Run(args []string) int {
2017-07-01 01:10:19 +00:00
var json, verbose bool
var tmpl string
2017-06-30 17:59:19 +00:00
2017-06-30 19:35:59 +00:00
flags := c.Meta.FlagSet("deployment list", FlagSetClient)
2017-06-30 17:59:19 +00:00
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.BoolVar(&verbose, "verbose", false, "")
2017-07-01 01:10:19 +00:00
flags.BoolVar(&json, "json", false, "")
flags.StringVar(&tmpl, "t", "", "")
2017-06-30 17:59:19 +00:00
if err := flags.Parse(args); err != nil {
return 1
}
// Check that we got no arguments
args = flags.Args()
if l := len(args); l != 0 {
c.Ui.Error(c.Help())
return 1
}
// Truncate the id unless full length is requested
length := shortId
if verbose {
length = fullId
}
// Get the HTTP client
client, err := c.Meta.Client()
if err != nil {
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
return 1
}
deploys, _, err := client.Deployments().List(nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error retrieving deployments: %s", err))
return 1
}
2017-07-01 01:10:19 +00:00
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
}
2017-06-30 17:59:19 +00:00
c.Ui.Output(formatDeployments(deploys, length))
return 0
}
func formatDeployments(deploys []*api.Deployment, uuidLength int) string {
if len(deploys) == 0 {
return "No deployments found"
}
rows := make([]string, len(deploys)+1)
rows[0] = "ID|Job ID|Job Version|Status|Description"
for i, d := range deploys {
rows[i+1] = fmt.Sprintf("%s|%s|%d|%s|%s",
limit(d.ID, uuidLength),
d.JobID,
d.JobVersion,
d.Status,
d.StatusDescription)
}
return formatList(rows)
}