From 07fbee0708b9fac11bbb0a79fb762a687c803eb7 Mon Sep 17 00:00:00 2001 From: Andy Dunstall Date: Sat, 8 Feb 2025 13:50:56 +0000 Subject: [PATCH] status: add --status filter to cluster nodes status (#222) Adds support for filtering the nodes to show in `status cluster nodes` by status with a `--status` flag. --- cli/server/status/cluster.go | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/cli/server/status/cluster.go b/cli/server/status/cluster.go index 537746d7..068bc90d 100644 --- a/cli/server/status/cluster.go +++ b/cli/server/status/cluster.go @@ -34,12 +34,25 @@ Queries the server for the set of nodes the cluster that this node knows about. The output contains the state of each known node. Examples: + # Inspect all nodes. piko server status cluster nodes + + # Inspect only active nodes. + piko server status cluster nodes --status active `, } + var status string + cmd.Flags().StringVar( + &status, + "status", + "", + ` +Filter by node status.`, + ) + cmd.Run = func(_ *cobra.Command, _ []string) { - showClusterNodes(c) + showClusterNodes(c, cluster.NodeStatus(status)) } return cmd @@ -49,22 +62,30 @@ type clusterNodesOutput struct { Nodes []*cluster.NodeMetadata `json:"nodes"` } -func showClusterNodes(c *client.Client) { - cluster := client.NewCluster(c) +func showClusterNodes(c *client.Client, status cluster.NodeStatus) { + client := client.NewCluster(c) - nodes, err := cluster.Nodes() + nodes, err := client.Nodes() if err != nil { fmt.Printf("failed to get cluster nodes: %s\n", err.Error()) os.Exit(1) } + var filtered []*cluster.NodeMetadata + for _, node := range nodes { + if status != "" && status != node.Status { + continue + } + filtered = append(filtered, node) + } + // Sort by ID. - sort.Slice(nodes, func(i, j int) bool { - return nodes[i].ID < nodes[j].ID + sort.Slice(filtered, func(i, j int) bool { + return filtered[i].ID < filtered[j].ID }) output := clusterNodesOutput{ - Nodes: nodes, + Nodes: filtered, } b, _ := yaml.Marshal(output) fmt.Print(string(b))