-
Notifications
You must be signed in to change notification settings - Fork 17
/
list.go
214 lines (198 loc) · 6.77 KB
/
list.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package devices
import (
"fmt"
"os"
"sort"
"strconv"
"strings"
"github.com/cheynewallace/tabby"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/foundriesio/fioctl/client"
"github.com/foundriesio/fioctl/subcommands"
)
var (
deviceNoShared bool
deviceByTag string
deviceByFactory string
deviceByGroup string
deviceInactiveHours int
deviceUuid string
showColumns []string
showPage int
paginationLimit int
paginationLimits []int
)
type column struct {
Formatter func(d *client.Device) string
}
func statusFormatter(d *client.Device) string {
status := "OK"
if len(d.Status) > 0 {
status = d.Status
}
if len(d.LastSeen) > 0 && !d.Online(deviceInactiveHours) {
status = "OFFLINE"
}
return status
}
var ownerCache = make(map[string]string)
func ownerFormatter(d *client.Device) string {
name, ok := ownerCache[d.Owner]
if ok {
return name
}
logrus.Debugf("Looking up user %s in factory %s", d.Owner, d.Factory)
users, err := api.UsersList(d.Factory)
if err != nil {
logrus.Errorf("Unable to look up users: %s", err)
return "???"
}
id := "<not in factory: " + d.Factory + ">"
for _, user := range users {
ownerCache[user.PolisId] = user.Name
if user.PolisId == d.Owner {
id = user.Name
}
}
return id
}
var Columns = map[string]column{
"name": {func(d *client.Device) string { return d.Name }},
"uuid": {func(d *client.Device) string { return d.Uuid }},
"factory": {func(d *client.Device) string { return d.Factory }},
"owner": {ownerFormatter},
"device-group": {func(d *client.Device) string { return d.GroupName }},
"target": {func(d *client.Device) string { return d.TargetName }},
"status": {statusFormatter},
"apps": {func(d *client.Device) string { return strings.Join(d.DockerApps, ",") }},
"up-to-date": {func(d *client.Device) string { return fmt.Sprintf("%v", d.UpToDate) }},
"tag": {func(d *client.Device) string { return d.Tag }},
"created-at": {func(d *client.Device) string { return d.CreatedAt }},
"last-seen": {func(d *client.Device) string { return d.LastSeen }},
"ostree-hash": {func(d *client.Device) string { return d.OstreeHash }},
"curent-update": {func(d *client.Device) string { return d.CurrentUpdate }},
"is-prod": {func(d *client.Device) string { return fmt.Sprintf("%v", d.IsProd) }},
"is-wave": {func(d *client.Device) string { return fmt.Sprintf("%v", d.IsWave) }},
}
func init() {
var defCols = []string{
"name", "factory", "target", "status", "apps", "up-to-date", "is-prod",
}
paginationLimits = []int{10, 20, 30, 40, 50, 100, 200, 500, 1000}
limitsStr := ""
for i, limit := range paginationLimits {
if i > 0 {
limitsStr += ","
}
limitsStr += strconv.Itoa(limit)
}
allCols := make([]string, 0, len(Columns))
for k := range Columns {
allCols = append(allCols, k)
}
sort.Strings(allCols)
listCmd := &cobra.Command{
Use: "list [pattern]",
Short: "List devices registered to factories. Optionally include filepath style patterns to limit to device names. eg device-*",
Run: doList,
Args: cobra.MaximumNArgs(1),
Long: "Available columns for display:\n\n * " + strings.Join(allCols, "\n * "),
}
cmd.AddCommand(listCmd)
listCmd.Flags().BoolVarP(&deviceNoShared, "just-mine", "", false, "Only include devices owned by you")
listCmd.Flags().StringVarP(&deviceByTag, "by-tag", "", "", "Only list devices configured with the given tag")
listCmd.Flags().StringVarP(&deviceByFactory, "by-factory", "f", "", "Only list devices belonging to this factory")
listCmd.Flags().StringVarP(&deviceByGroup, "by-group", "g", "", "Only list devices belonging to this group (factory is mandatory)")
listCmd.Flags().IntVarP(&deviceInactiveHours, "offline-threshold", "", 4, "List the device as 'OFFLINE' if not seen in the last X hours")
listCmd.Flags().StringVarP(&deviceUuid, "uuid", "", "", "Find device with the given UUID")
listCmd.Flags().StringSliceVarP(&showColumns, "columns", "", defCols, "Specify which columns to display")
listCmd.Flags().IntVarP(&showPage, "page", "p", 1, "Page of devices to display when pagination is needed")
listCmd.Flags().IntVarP(&paginationLimit, "limit", "n", 500, "Number of devices to paginate by. Allowed values: "+limitsStr)
}
// We allow pattern matching using filepath.Match type * and ?
// ie * matches everything and ? matches a single character.
// In sql we need * and ? to maps to % and _ respectively
// Since _ is a special character we need to escape that. And
//
// Soo... a pattern like: H?st_b* would become: H_st\_b%
// and would match stuff like host_b and hast_FOOO
func sqlLikeIfy(filePathLike string) string {
// %25 = urlencode("%")
sql := strings.Replace(filePathLike, "*", "%25", -1)
sql = strings.Replace(sql, "_", "\\_", -1)
sql = strings.Replace(sql, "?", "_", -1)
logrus.Debugf("Converted query(%s) -> %s", filePathLike, sql)
return sql
}
func assertPagination() {
// hack until: https://github.com/spf13/pflag/issues/236
for _, x := range paginationLimits {
if x == paginationLimit {
return
}
}
subcommands.DieNotNil(fmt.Errorf("Invalid limit: %d", paginationLimit))
}
func doList(cmd *cobra.Command, args []string) {
logrus.Debug("Listing registered devices")
assertPagination()
t := tabby.New()
var cols = make([]interface{}, len(showColumns))
for idx, c := range showColumns {
if _, ok := Columns[c]; !ok {
fmt.Println("ERROR: Invalid column name:", c)
os.Exit(1)
}
cols[idx] = strings.ToUpper(c)
}
t.AddHeader(cols...)
name_ilike := ""
if len(args) == 1 {
name_ilike = sqlLikeIfy(args[0])
}
if len(deviceByFactory) > 0 {
deviceNoShared = true
} else if len(deviceByGroup) > 0 {
deviceByFactory = viper.GetString("factory")
if len(deviceByFactory) == 0 {
subcommands.DieNotNil(fmt.Errorf("A factory is mandatory to filter by group"))
}
}
dl, err := api.DeviceList(!deviceNoShared, deviceByTag, deviceByFactory, deviceByGroup, name_ilike, deviceUuid, showPage, paginationLimit)
subcommands.DieNotNil(err)
row := make([]interface{}, len(showColumns))
for _, device := range dl.Devices {
if len(device.TargetName) == 0 {
device.TargetName = "???"
}
for idx, col := range showColumns {
col := Columns[col]
row[idx] = col.Formatter(&device)
}
t.AddLine(row...)
}
t.Print()
if dl.Next != nil {
fmt.Print("\nNext page of devices can be viewed with: ")
found := false
for i := 0; i < len(os.Args); i++ {
arg := os.Args[i]
if len(arg) > 2 && arg[:2] == "-p" {
fmt.Printf("-p%d ", showPage+1)
found = true
} else if len(arg) == 6 && arg[:6] == "--page" {
fmt.Printf("-p%d ", showPage+1)
found = true
i++
} else {
fmt.Print(os.Args[i], " ")
}
}
if !found {
fmt.Print("-p", showPage+1)
}
fmt.Println()
}
}