github.com/djenriquez/nomad-1@v0.8.1/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) Run(args []string) int {
    56  	var tmpl string
    57  	flags := c.Meta.FlagSet("quota inspect", FlagSetClient)
    58  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    59  	flags.StringVar(&tmpl, "t", "", "")
    60  
    61  	if err := flags.Parse(args); err != nil {
    62  		return 1
    63  	}
    64  
    65  	// Check that we got one arguments
    66  	args = flags.Args()
    67  	if l := len(args); l != 1 {
    68  		c.Ui.Error(c.Help())
    69  		return 1
    70  	}
    71  
    72  	name := args[0]
    73  
    74  	// Get the HTTP client
    75  	client, err := c.Meta.Client()
    76  	if err != nil {
    77  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    78  		return 1
    79  	}
    80  
    81  	// Do a prefix lookup
    82  	quotas := client.Quotas()
    83  	spec, possible, err := getQuota(quotas, name)
    84  	if err != nil {
    85  		c.Ui.Error(fmt.Sprintf("Error retrieving quota: %s", err))
    86  		return 1
    87  	}
    88  
    89  	if len(possible) != 0 {
    90  		c.Ui.Error(fmt.Sprintf("Prefix matched multiple quotas\n\n%s", formatQuotaSpecs(possible)))
    91  		return 1
    92  	}
    93  
    94  	// Get the quota usages
    95  	usages, failures := quotaUsages(spec, quotas)
    96  
    97  	failuresConverted := make(map[string]string, len(failures))
    98  	for r, e := range failures {
    99  		failuresConverted[r] = e.Error()
   100  	}
   101  
   102  	data := &inspectedQuota{
   103  		Spec:     spec,
   104  		Usages:   usages,
   105  		Failures: failuresConverted,
   106  	}
   107  
   108  	out, err := Format(len(tmpl) == 0, tmpl, data)
   109  	if err != nil {
   110  		c.Ui.Error(err.Error())
   111  		return 1
   112  	}
   113  
   114  	c.Ui.Output(out)
   115  	return 0
   116  }