Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: Add -json and -t flags to namespace status command #16442

Merged
merged 3 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions command/namespace_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,26 @@ Usage: nomad namespace status [options] <namespace>

General Options:

` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace)
` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + `

Status Specific Options:

-json
Output the latest namespace status information in a JSON format.

-t
Format and display namespace status information using a Go template.
`
Juanadelacuesta marked this conversation as resolved.
Show resolved Hide resolved

return strings.TrimSpace(helpText)
}

func (c *NamespaceStatusCommand) AutocompleteFlags() complete.Flags {
return c.Meta.AutocompleteFlags(FlagSetClient)
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
complete.Flags{
"-json": complete.PredictNothing,
"-t": complete.PredictAnything,
})
}

func (c *NamespaceStatusCommand) AutocompleteArgs() complete.Predictor {
Expand All @@ -44,7 +57,12 @@ func (c *NamespaceStatusCommand) Synopsis() string {
func (c *NamespaceStatusCommand) Name() string { return "namespace status" }

func (c *NamespaceStatusCommand) Run(args []string) int {
var json bool
var tmpl string

flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
flags.BoolVar(&json, "json", false, "")
flags.StringVar(&tmpl, "t", "", "")
flags.Usage = func() { c.Ui.Output(c.Help()) }

if err := flags.Parse(args); err != nil {
Expand Down Expand Up @@ -80,6 +98,17 @@ func (c *NamespaceStatusCommand) Run(args []string) int {
return 1
}

if json || len(tmpl) > 0 {
out, err := Format(json, tmpl, ns)
if err != nil {
c.Ui.Error(err.Error())
return 1
}

c.Ui.Output(out)
return 0
}

c.Ui.Output(formatNamespaceBasics(ns))

if len(ns.Meta) > 0 {
Expand Down
27 changes: 27 additions & 0 deletions command/namespace_status_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package command

import (
"encoding/json"
"strings"
"testing"

"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/ci"
"github.com/mitchellh/cli"
"github.com/posener/complete"
"github.com/shoenig/test/must"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -52,6 +54,7 @@ func TestNamespaceStatusCommand_Good(t *testing.T) {
// Create a namespace
ns := &api.Namespace{
Name: "foo",
//Description: "Test namespace",
jrasell marked this conversation as resolved.
Show resolved Hide resolved
}
_, err := client.Namespaces().Register(ns, nil)
assert.Nil(t, err)
Expand All @@ -66,6 +69,30 @@ func TestNamespaceStatusCommand_Good(t *testing.T) {
if !strings.Contains(out, "= foo") {
t.Fatalf("expected quota, got: %s", out)
}

ui.OutputWriter.Reset()

// List json
if code := cmd.Run([]string{"-address=" + url, "-json", ns.Name}); code != 0 {
t.Fatalf("expected exit 0, got: %d; %v", code, ui.ErrorWriter.String())
}

outJson := api.Namespace{}
err = json.Unmarshal(ui.OutputWriter.Bytes(), &outJson)
must.NoError(t, err)

ui.OutputWriter.Reset()

// Go template to format the output
if code := cmd.Run([]string{"-address=" + url, "-t", "{{.Name}}", ns.Name}); code != 0 {
t.Fatalf("expected exit 0, got: %d; %v", code, ui.ErrorWriter.String())
}

out = ui.OutputWriter.String()
must.StrContains(t, out, "foo")

ui.OutputWriter.Reset()
ui.ErrorWriter.Reset()
}

func TestNamespaceStatusCommand_Good_Quota(t *testing.T) {
Expand Down
26 changes: 26 additions & 0 deletions website/content/docs/commands/namespace/status.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ that has a capability associated with the namespace.

@include 'general_options_no_namespace.mdx'

## Status Options

- `-json` : Output the namespace status in its JSON format.
- `-t` : Format and display the namespace status using a Go template.

## Examples

View the status of a namespace:
Expand All @@ -46,3 +51,24 @@ Quota Limits
Region CPU Usage Memory Usage
global 500 / 2500 256 / 2000
```

The `-json` flag can be used to get the namespace status in json format:

```shell-session
$ nomad namespace status -json default
{
"Capabilities": null,
"CreateIndex": 1,
"Description": "Default shared namespace",
"Meta": null,
"ModifyIndex": 1,
"Name": "default",
"Quota": ""
}

Or use the `-t` flag to format and display the status using a Go template:

```shell-session
$ nomad namespace status -t {{.Description}} default
Default shared namespace
```