cli: add -quiet to nomad node status command. (#12426)

This commit is contained in:
Shishir 2022-04-05 12:53:43 -07:00 committed by GitHub
parent 71673f03b2
commit a6801f73d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 1 deletions

View File

@ -29,6 +29,7 @@ type NodeStatusCommand struct {
length int
short bool
os bool
quiet bool
verbose bool
list_allocs bool
self bool
@ -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.
@ -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,
})
}
@ -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, "")
@ -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)
@ -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|"

View File

@ -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) {

View File

@ -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.
@ -64,6 +66,8 @@ 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
@ -71,6 +75,18 @@ 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