mirror of
https://github.com/kemko/nomad.git
synced 2026-01-06 10:25:42 +03:00
Add -json flag to quota inspect command (#16478)
* Added and flag to command * cli[style]: small refactor to avoid confussion with tmpl variable * Update inspect.mdx * cli: add changelog entry * Update .changelog/16478.txt Co-authored-by: James Rasell <jrasell@users.noreply.github.com> * Update command/quota_inspect.go Co-authored-by: James Rasell <jrasell@users.noreply.github.com> --------- Co-authored-by: James Rasell <jrasell@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
26b4fcc2c9
commit
cc110f4cc7
@@ -33,8 +33,11 @@ General Options:
|
||||
|
||||
Inspect Options:
|
||||
|
||||
-json
|
||||
Output the latest quota information in a JSON format.
|
||||
|
||||
-t
|
||||
Format and display the namespaces using a Go template.
|
||||
Format and display quota information using a Go template.
|
||||
`
|
||||
|
||||
return strings.TrimSpace(helpText)
|
||||
@@ -43,7 +46,8 @@ Inspect Options:
|
||||
func (c *QuotaInspectCommand) AutocompleteFlags() complete.Flags {
|
||||
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
|
||||
complete.Flags{
|
||||
"-t": complete.PredictAnything,
|
||||
"-t": complete.PredictAnything,
|
||||
"-json": complete.PredictNothing,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -58,9 +62,11 @@ func (c *QuotaInspectCommand) Synopsis() string {
|
||||
func (c *QuotaInspectCommand) Name() string { return "quota inspect" }
|
||||
|
||||
func (c *QuotaInspectCommand) Run(args []string) int {
|
||||
var json bool
|
||||
var tmpl string
|
||||
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
|
||||
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
||||
flags.BoolVar(&json, "json", false, "")
|
||||
flags.StringVar(&tmpl, "t", "", "")
|
||||
|
||||
if err := flags.Parse(args); err != nil {
|
||||
@@ -97,6 +103,17 @@ func (c *QuotaInspectCommand) Run(args []string) int {
|
||||
return 1
|
||||
}
|
||||
|
||||
if json || len(tmpl) > 0 {
|
||||
out, err := Format(json, tmpl, spec)
|
||||
if err != nil {
|
||||
c.Ui.Error(err.Error())
|
||||
return 1
|
||||
}
|
||||
|
||||
c.Ui.Output(out)
|
||||
return 0
|
||||
}
|
||||
|
||||
// Get the quota usages
|
||||
usages, failures := quotaUsages(spec, quotas)
|
||||
|
||||
@@ -111,7 +128,8 @@ func (c *QuotaInspectCommand) Run(args []string) int {
|
||||
Failures: failuresConverted,
|
||||
}
|
||||
|
||||
out, err := Format(len(tmpl) == 0, tmpl, data)
|
||||
ftr := JSONFormat{}
|
||||
out, err := ftr.TransformData(data)
|
||||
if err != nil {
|
||||
c.Ui.Error(err.Error())
|
||||
return 1
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
"github.com/hashicorp/nomad/ci"
|
||||
"github.com/mitchellh/cli"
|
||||
"github.com/posener/complete"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/shoenig/test/must"
|
||||
)
|
||||
|
||||
func TestQuotaInspectCommand_Implements(t *testing.T) {
|
||||
@@ -24,24 +24,20 @@ func TestQuotaInspectCommand_Fails(t *testing.T) {
|
||||
cmd := &QuotaInspectCommand{Meta: Meta{Ui: ui}}
|
||||
|
||||
// Fails on misuse
|
||||
if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 {
|
||||
t.Fatalf("expected exit code 1, got: %d", code)
|
||||
}
|
||||
if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
|
||||
t.Fatalf("expected help output, got: %s", out)
|
||||
}
|
||||
code = cmd.Run([]string{"some", "bad", "args"})
|
||||
must.One(t, code)
|
||||
|
||||
must.StrContains(t, ui.ErrorWriter.String(), commandErrorText(cmd))
|
||||
ui.ErrorWriter.Reset()
|
||||
|
||||
if code := cmd.Run([]string{"-address=nope", "foo"}); code != 1 {
|
||||
t.Fatalf("expected exit code 1, got: %d", code)
|
||||
}
|
||||
if out := ui.ErrorWriter.String(); !strings.Contains(out, "retrieving quota") {
|
||||
t.Fatalf("connection error, got: %s", out)
|
||||
}
|
||||
code = cmd.Run([]string{"-address=nope", "foo"})
|
||||
must.One(t, code)
|
||||
|
||||
must.StrContains(t, ui.ErrorWriter.String(), "retrieving quota")
|
||||
ui.ErrorWriter.Reset()
|
||||
}
|
||||
|
||||
func TestQuotaInspectCommand_Good(t *testing.T) {
|
||||
func TestQuotaInspectCommand_Run(t *testing.T) {
|
||||
ci.Parallel(t)
|
||||
|
||||
// Create a server
|
||||
@@ -54,17 +50,34 @@ func TestQuotaInspectCommand_Good(t *testing.T) {
|
||||
// Create a quota to delete
|
||||
qs := testQuotaSpec()
|
||||
_, err := client.Quotas().Register(qs, nil)
|
||||
assert.Nil(t, err)
|
||||
must.NoError(t, err)
|
||||
|
||||
// Delete a namespace
|
||||
if code := cmd.Run([]string{"-address=" + url, qs.Name}); code != 0 {
|
||||
t.Fatalf("expected exit 0, got: %d; %v", code, ui.ErrorWriter.String())
|
||||
}
|
||||
// Delete a quota
|
||||
code := cmd.Run([]string{"-address=" + url, qs.Name})
|
||||
must.Zero(t, code)
|
||||
|
||||
out := ui.OutputWriter.String()
|
||||
if !strings.Contains(out, "Usages") || !strings.Contains(out, qs.Name) {
|
||||
t.Fatalf("expected quota, got: %s", out)
|
||||
}
|
||||
|
||||
// List json
|
||||
must.Zero(t, cmd.Run([]string{"-address=" + url, "-json", allocID}))
|
||||
|
||||
outJson := *api.Quotas{}
|
||||
err = json.Unmarshal(ui.OutputWriter.Bytes(), &outJson)
|
||||
must.NoError(t, err)
|
||||
|
||||
ui.OutputWriter.Reset()
|
||||
|
||||
// Go template to format the output
|
||||
code = cmd.Run([]string{"-address=" + url, "-t", "{{ .Name }}", allocID})
|
||||
must.Zero(t, code)
|
||||
|
||||
out = ui.OutputWriter.String()
|
||||
must.StrContains(t, out, "test-quota")
|
||||
|
||||
ui.OutputWriter.Reset()
|
||||
}
|
||||
|
||||
func TestQuotaInspectCommand_AutocompleteArgs(t *testing.T) {
|
||||
@@ -80,12 +93,12 @@ func TestQuotaInspectCommand_AutocompleteArgs(t *testing.T) {
|
||||
// Create a quota
|
||||
qs := testQuotaSpec()
|
||||
_, err := client.Quotas().Register(qs, nil)
|
||||
assert.Nil(err)
|
||||
must.NoError(t, err)
|
||||
|
||||
args := complete.Args{Last: "t"}
|
||||
predictor := cmd.AutocompleteArgs()
|
||||
|
||||
res := predictor.Predict(args)
|
||||
assert.Equal(1, len(res))
|
||||
assert.Equal(qs.Name, res[0])
|
||||
must.One(t, len(res))
|
||||
must.StrContains(t, qs.Name, res[0])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user