internal/cli/members.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package cli import ( "bytes" "flag" "fmt" "os" "strings" "text/tabwriter" "git.j3s.sh/cascade/api" ) type membersCommand struct { usage string flagAPIAddr string flagDetails bool } func (c membersCommand) Usage() { fmt.Printf(`usage: cascade members [flags] list the members of the cascade serf cluster flags: -api address of the cascade http api to target (default = 127.0.0.1:8500) -details | -l show more details (default = nil) `) } func (c *membersCommand) Init(args []string) { flags := flag.NewFlagSet("", flag.ContinueOnError) flags.Usage = c.Usage flags.StringVar(&c.flagAPIAddr, "api", "", "") flags.BoolVar(&c.flagDetails, "details", false, "") flags.BoolVar(&c.flagDetails, "l", false, "") if err := flags.Parse(args); err != nil { fmt.Println(err) os.Exit(1) } } func RunMembers(args []string) { c := membersCommand{} c.Init(args) cfg := api.DefaultConfig() client, err := api.NewClient(cfg) if err != nil { fmt.Println(err) os.Exit(1) } members, err := client.Agent().Members() if err != nil { fmt.Println(err) os.Exit(1) } if c.flagDetails { var b bytes.Buffer tw := tabwriter.NewWriter(&b, 0, 2, 2, ' ', 0) fmt.Fprintf(tw, "node\taddr\tstatus\ttags\n") for _, m := range members { fmt.Fprintf(tw, "%s\t", m.Name) fmt.Fprintf(tw, "%s:%d\t", m.Addr, m.Port) fmt.Fprintf(tw, "%s\t", m.StatusPretty()) fmt.Fprintf(tw, "%s\t", c.printTags(m.Tags)) fmt.Fprintln(tw) } if err := tw.Flush(); err != nil { fmt.Printf("error flushing tabwriter: %s", err) os.Exit(1) } fmt.Print(b.String()) } else { for _, m := range members { fmt.Println(m.Name) } } } func (c membersCommand) printTags(tags map[string]string) string { var results []string for k, v := range tags { results = append(results, fmt.Sprintf("%s=%s", k, v)) } return strings.Join(results, ",") }