/
list.go
236 lines (216 loc) · 7.68 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
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 (
deviceMine bool
deviceByTag string
deviceByTarget 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 !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.ChangeMeta.CreatedAt }},
"created-by": {func(d *client.Device) string { return d.ChangeMeta.CreatedBy }},
"updated-at": {func(d *client.Device) string { return d.ChangeMeta.UpdatedAt }},
"updated-by": {func(d *client.Device) string { return d.ChangeMeta.UpdatedBy }},
"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 addPaginationFlags(cmd *cobra.Command) {
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)
}
cmd.Flags().IntVarP(&showPage, "page", "p", 1, "Page of devices to display when pagination is needed")
cmd.Flags().IntVarP(&paginationLimit, "limit", "n", 500, "Number of devices to paginate by. Allowed values: "+limitsStr)
}
func addSortFlag(cmd *cobra.Command, flag, short, help string) {
// Allows `--flag` to specify ascending sort order or a regular `--flag=asc`, `--flag=desc`.
cmd.Flags().StringP(flag, short, "", help)
cmd.Flags().Lookup(flag).NoOptDefVal = "asc"
}
func appendSortFlagValue(sortBy []string, cmd *cobra.Command, flag string, field string) []string {
val, _ := cmd.Flags().GetString(flag)
switch val {
case "":
break
case "asc":
sortBy = append(sortBy, field)
case "desc":
sortBy = append(sortBy, "-"+field)
default:
subcommands.DieNotNil(fmt.Errorf("Only 'asc, desc' values are allowed for %s but received: %s", flag, val))
}
return sortBy
}
func init() {
var defCols = []string{
"name", "target", "status", "apps", "up-to-date", "is-prod",
}
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(&deviceMine, "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(&deviceByTarget, "by-target", "", "", "Only list devices updated to the given target name")
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")
addPaginationFlags(listCmd)
addSortFlag(listCmd, "sort-by-name", "", "Sort by name (asc, desc); default sort is by owner and name")
addSortFlag(listCmd, "sort-by-last-seen", "", "Sort by last-seen (asc, desc); default sort is by owner and name")
listCmd.MarkFlagsMutuallyExclusive("sort-by-name", "sort-by-last-seen")
}
// 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 showDeviceList(dl *client.DeviceList, showColumns []string) {
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...)
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()
subcommands.ShowPages(showPage, dl.Next)
}
func doList(cmd *cobra.Command, args []string) {
factory := viper.GetString("factory")
logrus.Debugf("Listing registered devices for: %s", factory)
assertPagination()
var sortBy []string
sortBy = appendSortFlagValue(sortBy, cmd, "sort-by-last-seen", "last_seen")
sortBy = appendSortFlagValue(sortBy, cmd, "sort-by-name", "name")
name_ilike := ""
if len(args) == 1 {
name_ilike = sqlLikeIfy(args[0])
}
dl, err := api.DeviceList(
deviceMine,
deviceByTag,
factory,
deviceByGroup,
name_ilike,
deviceUuid,
deviceByTarget,
strings.Join(sortBy, ","),
showPage,
paginationLimit,
)
subcommands.DieNotNil(err)
showDeviceList(dl, showColumns)
}