github.com/nir0s/nomad@v0.8.7-rc1/command/quota_inspect.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 QuotaInspectCommand struct {
    12  	Meta
    13  }
    14  
    15  type inspectedQuota struct {
    16  	Spec     *api.QuotaSpec
    17  	Usages   map[string]*api.QuotaUsage
    18  	Failures map[string]string `json:"UsageLookupErrors"`
    19  }
    20  
    21  func (c *QuotaInspectCommand) Help() string {
    22  	helpText := `
    23  Usage: nomad quota inspect [options] <quota>
    24  
    25    Inspect is used to view raw information about a particular quota.
    26  
    27  General Options:
    28  
    29    ` + generalOptionsUsage() + `
    30  
    31  Inspect Options:
    32  
    33    -t
    34      Format and display the namespaces using a Go template.
    35  `
    36  
    37  	return strings.TrimSpace(helpText)
    38  }
    39  
    40  func (c *QuotaInspectCommand) AutocompleteFlags() complete.Flags {
    41  	return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
    42  		complete.Flags{
    43  			"-t": complete.PredictAnything,
    44  		})
    45  }
    46  
    47  func (c *QuotaInspectCommand) AutocompleteArgs() complete.Predictor {
    48  	return QuotaPredictor(c.Meta.Client)
    49  }
    50  
    51  func (c *QuotaInspectCommand) Synopsis() string {
    52  	return "Inspect a quota specification"
    53  }
    54  
    55  func (c *QuotaInspectCommand) Name() string { return "quota inspect" }
    56  
    57  func (c *QuotaInspectCommand) Run(args []string) int {
    58  	var tmpl string
    59  	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
    60  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    61  	flags.StringVar(&tmpl, "t", "", "")
    62  
    63  	if err := flags.Parse(args); err != nil {
    64  		return 1
    65  	}
    66  
    67  	// Check that we got one arguments
    68  	args = flags.Args()
    69  	if l := len(args); l != 1 {
    70  		c.Ui.Error("This command takes one argument: <quota>")
    71  		c.Ui.Error(commandErrorText(c))
    72  		return 1
    73  	}
    74  
    75  	name := args[0]
    76  
    77  	// Get the HTTP client
    78  	client, err := c.Meta.Client()
    79  	if err != nil {
    80  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    81  		return 1
    82  	}
    83  
    84  	// Do a prefix lookup
    85  	quotas := client.Quotas()
    86  	spec, possible, err := getQuota(quotas, name)
    87  	if err != nil {
    88  		c.Ui.Error(fmt.Sprintf("Error retrieving quota: %s", err))
    89  		return 1
    90  	}
    91  
    92  	if len(possible) != 0 {
    93  		c.Ui.Error(fmt.Sprintf("Prefix matched multiple quotas\n\n%s", formatQuotaSpecs(possible)))
    94  		return 1
    95  	}
    96  
    97  	// Get the quota usages
    98  	usages, failures := quotaUsages(spec, quotas)
    99  
   100  	failuresConverted := make(map[string]string, len(failures))
   101  	for r, e := range failures {
   102  		failuresConverted[r] = e.Error()
   103  	}
   104  
   105  	data := &inspectedQuota{
   106  		Spec:     spec,
   107  		Usages:   usages,
   108  		Failures: failuresConverted,
   109  	}
   110  
   111  	out, err := Format(len(tmpl) == 0, tmpl, data)
   112  	if err != nil {
   113  		c.Ui.Error(err.Error())
   114  		return 1
   115  	}
   116  
   117  	c.Ui.Output(out)
   118  	return 0
   119  }