-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlist_units.go
More file actions
116 lines (105 loc) · 2.63 KB
/
list_units.go
File metadata and controls
116 lines (105 loc) · 2.63 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package fleet
import (
"fmt"
"strings"
"github.com/coreos/fleet/machine"
"github.com/coreos/fleet/schema"
"github.com/deis/deis/deisctl/units"
)
const (
//defaultListUnitFields = "unit,state,load,active,sub,machine"
defaultListUnitsFields = "unit,machine,load,active,sub"
)
type usToField func(c *FleetClient, us *schema.UnitState, full bool) string
var (
listUnitsFields = map[string]usToField{
"unit": func(c *FleetClient, us *schema.UnitState, full bool) string {
if us == nil {
return "-"
}
return us.Name
},
"load": func(c *FleetClient, us *schema.UnitState, full bool) string {
if us == nil {
return "-"
}
return us.SystemdLoadState
},
"active": func(c *FleetClient, us *schema.UnitState, full bool) string {
if us == nil {
return "-"
}
return us.SystemdActiveState
},
"sub": func(c *FleetClient, us *schema.UnitState, full bool) string {
if us == nil {
return "-"
}
return us.SystemdSubState
},
"machine": func(c *FleetClient, us *schema.UnitState, full bool) string {
if us == nil || us.MachineID == "" {
return "-"
}
ms := c.cachedMachineState(us.MachineID)
if ms == nil {
ms = &machine.MachineState{ID: us.MachineID}
}
return machineFullLegend(*ms, full)
},
"hash": func(c *FleetClient, us *schema.UnitState, full bool) string {
if us == nil || us.Hash == "" {
return "-"
}
if !full {
return us.Hash[:7]
}
return us.Hash
},
}
)
// ListUnits prints all Deis-related units to Stdout
func (c *FleetClient) ListUnits() (err error) {
var states []*schema.UnitState
unitStates, err := c.Fleet.UnitStates()
if err != nil {
return err
}
for _, us := range unitStates {
for _, prefix := range units.Names {
if strings.HasPrefix(us.Name, prefix) {
states = append(states, us)
break
}
}
}
c.printUnits(states)
return
}
// printUnits writes units to stdout using a tabwriter
func (c *FleetClient) printUnits(states []*schema.UnitState) {
cols := strings.Split(defaultListUnitsFields, ",")
fmt.Fprintln(c.out, strings.ToUpper(strings.Join(cols, "\t")))
for _, us := range states {
var f []string
for _, col := range cols {
f = append(f, listUnitsFields[col](c, us, false))
}
fmt.Fprintln(c.out, strings.Join(f, "\t"))
}
c.out.Flush()
}
func machineIDLegend(ms machine.MachineState, full bool) string {
legend := ms.ID
if !full {
legend = fmt.Sprintf("%s...", ms.ShortID())
}
return legend
}
func machineFullLegend(ms machine.MachineState, full bool) string {
legend := machineIDLegend(ms, full)
if len(ms.PublicIP) > 0 {
legend = fmt.Sprintf("%s/%s", legend, ms.PublicIP)
}
return legend
}