github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/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) Name() string { return "quota list" } 54 func (c *QuotaListCommand) Run(args []string) int { 55 var json bool 56 var tmpl string 57 58 flags := c.Meta.FlagSet(c.Name(), FlagSetClient) 59 flags.Usage = func() { c.Ui.Output(c.Help()) } 60 flags.BoolVar(&json, "json", false, "") 61 flags.StringVar(&tmpl, "t", "", "") 62 63 if err := flags.Parse(args); err != nil { 64 return 1 65 } 66 67 // Check that we got no arguments 68 args = flags.Args() 69 if l := len(args); l != 0 { 70 c.Ui.Error("This command takes no arguments") 71 c.Ui.Error(commandErrorText(c)) 72 return 1 73 } 74 75 // Get the HTTP client 76 client, err := c.Meta.Client() 77 if err != nil { 78 c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) 79 return 1 80 } 81 82 quotas, _, err := client.Quotas().List(nil) 83 if err != nil { 84 c.Ui.Error(fmt.Sprintf("Error retrieving quotas: %s", err)) 85 return 1 86 } 87 88 if json || len(tmpl) > 0 { 89 out, err := Format(json, tmpl, quotas) 90 if err != nil { 91 c.Ui.Error(err.Error()) 92 return 1 93 } 94 95 c.Ui.Output(out) 96 return 0 97 } 98 99 c.Ui.Output(formatQuotaSpecs(quotas)) 100 return 0 101 } 102 103 func formatQuotaSpecs(quotas []*api.QuotaSpec) string { 104 if len(quotas) == 0 { 105 return "No quotas found" 106 } 107 108 // Sort the output by quota name 109 sort.Slice(quotas, func(i, j int) bool { return quotas[i].Name < quotas[j].Name }) 110 111 rows := make([]string, len(quotas)+1) 112 rows[0] = "Name|Description" 113 for i, qs := range quotas { 114 rows[i+1] = fmt.Sprintf("%s|%s", 115 qs.Name, 116 qs.Description) 117 } 118 return formatList(rows) 119 }