diff --git a/command/node_status.go b/command/node_status.go index 5f68b49eba6..546d64ba887 100644 --- a/command/node_status.go +++ b/command/node_status.go @@ -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|" diff --git a/command/node_status_test.go b/command/node_status_test.go index eccd9f7737b..b58a7e73170 100644 --- a/command/node_status_test.go +++ b/command/node_status_test.go @@ -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) { diff --git a/website/content/docs/commands/node/status.mdx b/website/content/docs/commands/node/status.mdx index 2a715ca5cac..c1a2df0c63f 100644 --- a/website/content/docs/commands/node/status.mdx +++ b/website/content/docs/commands/node/status.mdx @@ -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 false eligible ready 1f3f03ea dc1 node2 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 ubuntu false eligible ready f73e3993 dc1 node2 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