github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/quota_list.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/nomad/api"
     9  	"github.com/posener/complete"
    10  )
    11  
    12  type QuotaListCommand struct {
    13  	Meta
    14  }
    15  
    16  func (c *QuotaListCommand) Help() string {
    17  	helpText := `
    18  Usage: nomad quota list [options]
    19  
    20    List is used to list available quota specifications.
    21  
    22    If ACLs are enabled, this command requires a token with the 'quota:read'
    23    capability. Any quotas applied to namespaces that the token does not have
    24    access to will be filtered from the results.
    25  
    26  General Options:
    27  
    28    ` + generalOptionsUsage(usageOptsDefault) + `
    29  
    30  List Options:
    31  
    32    -json
    33      Output the quota specifications in a JSON format.
    34  
    35    -t
    36      Format and display the quota specifications using a Go template.
    37  `
    38  	return strings.TrimSpace(helpText)
    39  }
    40  
    41  func (c *QuotaListCommand) AutocompleteFlags() complete.Flags {
    42  	return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
    43  		complete.Flags{
    44  			"-json": complete.PredictNothing,
    45  			"-t":    complete.PredictAnything,
    46  		})
    47  }
    48  
    49  func (c *QuotaListCommand) AutocompleteArgs() complete.Predictor {
    50  	return complete.PredictNothing
    51  }
    52  
    53  func (c *QuotaListCommand) Synopsis() string {
    54  	return "List quota specifications"
    55  }
    56  
    57  func (c *QuotaListCommand) Name() string { return "quota list" }
    58  func (c *QuotaListCommand) Run(args []string) int {
    59  	var json 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(&json, "json", false, "")
    65  	flags.StringVar(&tmpl, "t", "", "")
    66  
    67  	if err := flags.Parse(args); err != nil {
    68  		return 1
    69  	}
    70  
    71  	// Check that we got no arguments
    72  	args = flags.Args()
    73  	if l := len(args); l != 0 {
    74  		c.Ui.Error("This command takes no arguments")
    75  		c.Ui.Error(commandErrorText(c))
    76  		return 1
    77  	}
    78  
    79  	// Get the HTTP client
    80  	client, err := c.Meta.Client()
    81  	if err != nil {
    82  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    83  		return 1
    84  	}
    85  
    86  	quotas, _, err := client.Quotas().List(nil)
    87  	if err != nil {
    88  		c.Ui.Error(fmt.Sprintf("Error retrieving quotas: %s", err))
    89  		return 1
    90  	}
    91  
    92  	if json || len(tmpl) > 0 {
    93  		out, err := Format(json, tmpl, quotas)
    94  		if err != nil {
    95  			c.Ui.Error(err.Error())
    96  			return 1
    97  		}
    98  
    99  		c.Ui.Output(out)
   100  		return 0
   101  	}
   102  
   103  	c.Ui.Output(formatQuotaSpecs(quotas))
   104  	return 0
   105  }
   106  
   107  func formatQuotaSpecs(quotas []*api.QuotaSpec) string {
   108  	if len(quotas) == 0 {
   109  		return "No quotas found"
   110  	}
   111  
   112  	// Sort the output by quota name
   113  	sort.Slice(quotas, func(i, j int) bool { return quotas[i].Name < quotas[j].Name })
   114  
   115  	rows := make([]string, len(quotas)+1)
   116  	rows[0] = "Name|Description"
   117  	for i, qs := range quotas {
   118  		rows[i+1] = fmt.Sprintf("%s|%s",
   119  			qs.Name,
   120  			qs.Description)
   121  	}
   122  	return formatList(rows)
   123  }