forked from hashicorp/nomad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployment_list.go
130 lines (103 loc) · 2.71 KB
/
deployment_list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package command
import (
"fmt"
"strings"
"github.com/hashicorp/nomad/api"
"github.com/posener/complete"
)
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:
-json
Output the deployments in a JSON format.
-t
Format and display the deployments using a Go template.
-verbose
Display full information.
`
return strings.TrimSpace(helpText)
}
func (c *DeploymentListCommand) AutocompleteFlags() complete.Flags {
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
complete.Flags{
"-json": complete.PredictNothing,
"-t": complete.PredictAnything,
"-verbose": complete.PredictNothing,
})
}
func (c *DeploymentListCommand) AutocompleteArgs() complete.Predictor {
return complete.PredictNothing
}
func (c *DeploymentListCommand) Synopsis() string {
return "List all deployments"
}
func (c *DeploymentListCommand) Name() string { return "deployment list" }
func (c *DeploymentListCommand) Run(args []string) int {
var json, verbose bool
var tmpl string
flags := c.Meta.FlagSet(c.Name(), 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
}
// Check that we got no arguments
args = flags.Args()
if l := len(args); l != 0 {
c.Ui.Error("This command takes no arguments")
c.Ui.Error(commandErrorText(c))
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
}
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
}
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)
}