forked from coreos/fleet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
list_machines.go
62 lines (50 loc) · 1.49 KB
/
list_machines.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
package main
import (
"fmt"
"strings"
)
var cmdListMachines = &Command{
Name: "list-machines",
Summary: "Enumerate the current hosts in the cluster",
Usage: "[-l|--full] [--no-legend]",
Description: `Lists all active machines within the cluster. Previously active machines will
not appear in this list.
For easily parsable output, you can remove the column headers:
fleetctl list-machines --no-legend
Output the list without truncation:
fleetctl list-machines --full`,
Run: runListMachines,
}
func init() {
cmdListMachines.Flags.BoolVar(&sharedFlags.Full, "full", false, "Do not ellipsize fields on output")
cmdListMachines.Flags.BoolVar(&sharedFlags.Full, "l", false, "Shorthand for --full")
cmdListMachines.Flags.BoolVar(&sharedFlags.NoLegend, "no-legend", false, "Do not print a legend (column headers)")
}
func runListMachines(args []string) (exit int) {
if !sharedFlags.NoLegend {
fmt.Fprintln(out, "MACHINE\tIP\tMETADATA")
}
for _, m := range registryCtl.GetActiveMachines() {
mach := machineIDLegend(m, sharedFlags.Full)
ip := m.PublicIP
if len(ip) == 0 {
ip = "-"
}
metadata := "-"
if len(m.Metadata) != 0 {
metadata = formatMetadata(m.Metadata)
}
fmt.Fprintf(out, "%s\t%s\t%s\n", mach, ip, metadata)
}
out.Flush()
return
}
func formatMetadata(metadata map[string]string) string {
pairs := make([]string, len(metadata))
idx := 0
for key, value := range metadata {
pairs[idx] = fmt.Sprintf("%s=%s", key, value)
idx++
}
return strings.Join(pairs, ", ")
}