Skip to content

Commit

Permalink
cli: add -quiet to nomad node status command. (#12426)
Browse files Browse the repository at this point in the history
  • Loading branch information
shishir-a412ed authored Apr 5, 2022
1 parent 1d28553 commit 4042c28
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
27 changes: 26 additions & 1 deletion command/node_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type NodeStatusCommand struct {
length int
short bool
os bool
quiet bool
verbose bool
list_allocs bool
self bool
Expand Down Expand Up @@ -78,6 +79,9 @@ Node Status Options:
-os
Display operating system name.
-quiet
Display only node IDs.
-json
Output the node in its JSON format.
Expand All @@ -101,6 +105,7 @@ func (c *NodeStatusCommand) AutocompleteFlags() complete.Flags {
"-stats": complete.PredictNothing,
"-t": complete.PredictAnything,
"-os": complete.PredictAnything,
"-quiet": complete.PredictAnything,
"-verbose": complete.PredictNothing,
})
}
Expand Down Expand Up @@ -128,6 +133,7 @@ func (c *NodeStatusCommand) Run(args []string) int {
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.BoolVar(&c.short, "short", false, "")
flags.BoolVar(&c.os, "os", false, "")
flags.BoolVar(&c.quiet, "quiet", false, "")
flags.BoolVar(&c.verbose, "verbose", false, "")
flags.BoolVar(&c.list_allocs, "allocs", false, "")
flags.BoolVar(&c.self, "self", false, "")
Expand Down Expand Up @@ -162,6 +168,10 @@ func (c *NodeStatusCommand) Run(args []string) int {

// Use list mode if no node name was provided
if len(args) == 0 && !c.self {
if c.quiet && (c.verbose || c.json) {
c.Ui.Error("-quiet cannot be used with -verbose or -json")
return 1
}

// Query the node info
nodes, _, err := client.Nodes().List(nil)
Expand All @@ -187,8 +197,23 @@ func (c *NodeStatusCommand) Run(args []string) int {
return 0
}

var size int
if c.quiet {
size = len(nodes)
} else {
size = len(nodes) + 1
}

// Format the nodes list
out := make([]string, len(nodes)+1)
out := make([]string, size)

if c.quiet {
for i, node := range nodes {
out[i] = node.ID
}
c.Ui.Output(formatList(out))
return 0
}

out[0] = "ID|DC|Name|Class|"

Expand Down
20 changes: 20 additions & 0 deletions command/node_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,26 @@ func TestNodeStatusCommand_Fails(t *testing.T) {
if out := ui.ErrorWriter.String(); !strings.Contains(out, "Both json and template formatting are not allowed") {
t.Fatalf("expected getting formatter error, got: %s", out)
}
ui.ErrorWriter.Reset()

// Fail if -quiet is passed with -verbose
if code := cmd.Run([]string{"-address=" + url, "-quiet", "-verbose"}); code != 1 {
t.Fatalf("expected exit 1, got: %d", code)
}

if out := ui.ErrorWriter.String(); !strings.Contains(out, "-quiet cannot be used with -verbose or -json") {
t.Fatalf("expected getting formatter error, got: %s", out)
}
ui.ErrorWriter.Reset()

// Fail if -quiet is passed with -json
if code := cmd.Run([]string{"-address=" + url, "-quiet", "-json"}); code != 1 {
t.Fatalf("expected exit 1, got: %d", code)
}

if out := ui.ErrorWriter.String(); !strings.Contains(out, "-quiet cannot be used with -verbose or -json") {
t.Fatalf("expected getting formatter error, got: %s", out)
}
}

func TestNodeStatusCommand_AutocompleteArgs(t *testing.T) {
Expand Down
16 changes: 16 additions & 0 deletions website/content/docs/commands/node/status.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ capability.

- `-os`: Display operating system name.

- `-quiet`: Display only node IDs.

- `-json` : Output the node in its JSON format.

- `-t` : Format and display node using a Go template.
Expand All @@ -64,13 +66,27 @@ a72dfba2 dc1 node1 <none> false eligible ready
1f3f03ea dc1 node2 <none> false eligible ready
```

List view, with operating system name:

```shell-session
$ nomad node status -os
ID DC Name Class OS Drain Eligibility Status
a72dfba2 dc1 node1 <none> ubuntu false eligible ready
f73e3993 dc1 node2 <none> centos false eligible ready
```

List view, with quiet:

```shell-session
$ nomad node status -quiet
820561cf-830d-b089-cg03-f2cfc4cecff7
07949e3a-4c25-e1bf-3dbh-984c2e28fec2
8bc2c581-37ba-dj08-9f67-53bc217f36f8
f35be281-85a5-d1e6-d268-6e8a6f0684df
```

**NOTE**: `-quiet` cannot be used in conjuction with `-verbose` or `-json`.

List view, with running allocations:

```shell-session
Expand Down

0 comments on commit 4042c28

Please sign in to comment.