blob: 13e44cbb7c8cb4301222af268118f8f423392924 [file] [log] [blame]
Mateusz Zalegadb75e212022-08-04 17:31:34 +02001package main
2
3import (
4 "context"
5 "log"
6
7 "github.com/spf13/cobra"
8
9 "source.monogon.dev/metropolis/cli/metroctl/core"
10 clicontext "source.monogon.dev/metropolis/cli/pkg/context"
Mateusz Zalegab838e052022-08-12 18:08:10 +020011 apb "source.monogon.dev/metropolis/proto/api"
Mateusz Zalegadb75e212022-08-04 17:31:34 +020012)
13
14var listCmd = &cobra.Command{
15 Short: "Lists cluster nodes.",
16 Use: "list [node-id] [--filter] [--output] [--format]",
17 Example: "metroctl node list --filter node.status.external_address==\"10.8.0.2\"",
18 Run: doList,
19 Args: cobra.ArbitraryArgs,
20}
21
22func init() {
23 nodeCmd.AddCommand(listCmd)
24}
25
26func doList(cmd *cobra.Command, args []string) {
27 ctx := clicontext.WithInterrupt(context.Background())
28 cc := dialAuthenticated(ctx)
Mateusz Zalegab838e052022-08-12 18:08:10 +020029 mgmt := apb.NewManagementClient(cc)
Mateusz Zalegadb75e212022-08-04 17:31:34 +020030
31 // Narrow down the output set to supplied node IDs, if any.
32 qids := make(map[string]bool)
33 if len(args) != 0 && args[0] != "all" {
34 for _, a := range args {
35 qids[a] = true
36 }
37 }
38
39 nodes, err := core.GetNodes(ctx, mgmt, flags.filter)
40 if err != nil {
41 log.Fatalf("While calling Management.GetNodes: %v", err)
42 }
43
Mateusz Zalegab838e052022-08-12 18:08:10 +020044 of := func(enc *encoder, n *apb.Node) error {
45 return enc.writeNodeID(n)
Mateusz Zalegadb75e212022-08-04 17:31:34 +020046 }
Mateusz Zalegab838e052022-08-12 18:08:10 +020047 printNodes(of, nodes, args)
Mateusz Zalegadb75e212022-08-04 17:31:34 +020048}