github.com/djenriquez/nomad-1@v0.8.1/command/deployment_list.go (about)

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