github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/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 General Options: 23 24 ` + generalOptionsUsage() + ` 25 26 List Options: 27 28 -json 29 Output the quota specifications in a JSON format. 30 31 -t 32 Format and display the quota specifications using a Go template. 33 ` 34 return strings.TrimSpace(helpText) 35 } 36 37 func (c *QuotaListCommand) AutocompleteFlags() complete.Flags { 38 return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient), 39 complete.Flags{ 40 "-json": complete.PredictNothing, 41 "-t": complete.PredictAnything, 42 }) 43 } 44 45 func (c *QuotaListCommand) AutocompleteArgs() complete.Predictor { 46 return complete.PredictNothing 47 } 48 49 func (c *QuotaListCommand) Synopsis() string { 50 return "List quota specifications" 51 } 52 53 func (c *QuotaListCommand) Run(args []string) int { 54 var json bool 55 var tmpl string 56 57 flags := c.Meta.FlagSet("quota list", FlagSetClient) 58 flags.Usage = func() { c.Ui.Output(c.Help()) } 59 flags.BoolVar(&json, "json", false, "") 60 flags.StringVar(&tmpl, "t", "", "") 61 62 if err := flags.Parse(args); err != nil { 63 return 1 64 } 65 66 // Check that we got no arguments 67 args = flags.Args() 68 if l := len(args); l != 0 { 69 c.Ui.Error(c.Help()) 70 return 1 71 } 72 73 // Get the HTTP client 74 client, err := c.Meta.Client() 75 if err != nil { 76 c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) 77 return 1 78 } 79 80 quotas, _, err := client.Quotas().List(nil) 81 if err != nil { 82 c.Ui.Error(fmt.Sprintf("Error retrieving quotas: %s", err)) 83 return 1 84 } 85 86 if json || len(tmpl) > 0 { 87 out, err := Format(json, tmpl, quotas) 88 if err != nil { 89 c.Ui.Error(err.Error()) 90 return 1 91 } 92 93 c.Ui.Output(out) 94 return 0 95 } 96 97 c.Ui.Output(formatQuotaSpecs(quotas)) 98 return 0 99 } 100 101 func formatQuotaSpecs(quotas []*api.QuotaSpec) string { 102 if len(quotas) == 0 { 103 return "No quotas found" 104 } 105 106 // Sort the output by quota name 107 sort.Slice(quotas, func(i, j int) bool { return quotas[i].Name < quotas[j].Name }) 108 109 rows := make([]string, len(quotas)+1) 110 rows[0] = "Name|Description" 111 for i, qs := range quotas { 112 rows[i+1] = fmt.Sprintf("%s|%s", 113 qs.Name, 114 qs.Description) 115 } 116 return formatList(rows) 117 }