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