1package command
2
3import (
4	"fmt"
5	"strings"
6
7	"github.com/hashicorp/nomad/api"
8	"github.com/posener/complete"
9)
10
11type DeploymentListCommand struct {
12	Meta
13}
14
15func (c *DeploymentListCommand) Help() string {
16	helpText := `
17Usage: nomad deployment list [options]
18
19  List is used to list the set of deployments tracked by Nomad.
20
21General Options:
22
23  ` + generalOptionsUsage() + `
24
25List Options:
26
27  -json
28    Output the deployments in a JSON format.
29
30  -t
31    Format and display the deployments using a Go template.
32
33  -verbose
34    Display full information.
35`
36	return strings.TrimSpace(helpText)
37}
38
39func (c *DeploymentListCommand) AutocompleteFlags() complete.Flags {
40	return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
41		complete.Flags{
42			"-json":    complete.PredictNothing,
43			"-t":       complete.PredictAnything,
44			"-verbose": complete.PredictNothing,
45		})
46}
47
48func (c *DeploymentListCommand) AutocompleteArgs() complete.Predictor {
49	return complete.PredictNothing
50}
51
52func (c *DeploymentListCommand) Synopsis() string {
53	return "List all deployments"
54}
55
56func (c *DeploymentListCommand) Name() string { return "deployment list" }
57
58func (c *DeploymentListCommand) Run(args []string) int {
59	var json, verbose bool
60	var tmpl string
61
62	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
63	flags.Usage = func() { c.Ui.Output(c.Help()) }
64	flags.BoolVar(&verbose, "verbose", false, "")
65	flags.BoolVar(&json, "json", false, "")
66	flags.StringVar(&tmpl, "t", "", "")
67
68	if err := flags.Parse(args); err != nil {
69		return 1
70	}
71
72	// Check that we got no arguments
73	args = flags.Args()
74	if l := len(args); l != 0 {
75		c.Ui.Error("This command takes no arguments")
76		c.Ui.Error(commandErrorText(c))
77		return 1
78	}
79
80	// Truncate the id unless full length is requested
81	length := shortId
82	if verbose {
83		length = fullId
84	}
85
86	// Get the HTTP client
87	client, err := c.Meta.Client()
88	if err != nil {
89		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
90		return 1
91	}
92
93	deploys, _, err := client.Deployments().List(nil)
94	if err != nil {
95		c.Ui.Error(fmt.Sprintf("Error retrieving deployments: %s", err))
96		return 1
97	}
98
99	if json || len(tmpl) > 0 {
100		out, err := Format(json, tmpl, deploys)
101		if err != nil {
102			c.Ui.Error(err.Error())
103			return 1
104		}
105
106		c.Ui.Output(out)
107		return 0
108	}
109
110	c.Ui.Output(formatDeployments(deploys, length))
111	return 0
112}
113
114func formatDeployments(deploys []*api.Deployment, uuidLength int) string {
115	if len(deploys) == 0 {
116		return "No deployments found"
117	}
118
119	rows := make([]string, len(deploys)+1)
120	rows[0] = "ID|Job ID|Job Version|Status|Description"
121	for i, d := range deploys {
122		rows[i+1] = fmt.Sprintf("%s|%s|%d|%s|%s",
123			limit(d.ID, uuidLength),
124			d.JobID,
125			d.JobVersion,
126			d.Status,
127			d.StatusDescription)
128	}
129	return formatList(rows)
130}
131