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