-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcli
190 lines (157 loc) · 5.52 KB
/
cli
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
#!/usr/bin/env node
process.argv.push('--color')
const fero = require('./')
, { stream } = require('./utils')
, { debounce, parse, keys, values, az, za, key, extend, by, not, is, str, lo } = require('utilise/pure')
, { green, magenta, yellow, red, bold } = require('chalk')
, { cursorTo } = require('readline')
, discover = require('./discovery/multicast')
, argv = require('minimist')(process.argv.slice(2))
, [command, name, k, v] = argv._
, port = argv.p || argv.port || 5000
, json = argv.j || argv.json || false
, table = argv.t || argv.table || false
, sort = argv.s || argv.sort
, where = argv.w || argv.where
, commands = { help, list, ls: list, get, set }
, TIMEOUT = 60*5*1000
, block = debounce(TIMEOUT)(() => console.error('timeout!'))
, Table = require('cli-table')
commands[command in commands ? command : 'help']()
function help(){
console.error(`
commands:
* ${green('list, ls')}
${bold('gives a live summary (name, id, partitions, commits, hash) across all services')}
examples:
${magenta('fero list')}
* ${green('list, ls <name>')}
${bold('gives a live summary (name, id, partitions, commits, hash) for a particular service (<name>)')}
examples:
${magenta('fero list users')}
* ${green('get <name> <key>')}
${bold('gets the value of the <key> in the resource <name>')}
key can be empty which will return the state of the entire resource
key can be a deep key to get an arbitrary part of the resource (e.g. a.b.c)
-j, --json: dump output as json
-t, --table: pretty print output as table,
* optionally list fields to show (defaults to fields of first row)
* or specify a number to truncate the number of columns
-s, --sort: comma-delimited list of columns to sort on when displaying as table (default none)
-w, --where:
* comma-delimited list of "key=val" to filter rows on
* the key can be a deep key
* matching is case-insentive, partial, on stringified value of that key (i.e. works with objects)
examples:
${magenta('fero get users 28')}
${magenta('fero get users --table id,username --sort last_updated,firstname')}
${magenta('fero get users --table --where name=david')}
* ${green('set <name> <key> <value>')}
${bold('sets the value of the <key> in the resource <name> to <value>')}
the key can be empty which will set the state of the entire resource
the key can be a deep key to set an arbitrarily deep property of the resource (e.g. a.b.c)
the value will be attempted to be parsed as JSON, before falling back to string
examples:
${magenta('fero set users 28.email [email protected]')}
* ${green('help')}
${bold('prints this message')}
options
-p, --port: change web server port, 0 for random, -1 to disable, defaults to 5000
`)
process.exit(1)
}
async function list(){
block()
clear()
const udp = discover()
, details = {}
await udp.once('listen')
udp
.on('list')
.filter(([rname, id]) => id)
.filter(([rname, id]) => (!name || rname == name))
.map(([rname, id, _uuid, _hash, partitions, commits]) => {
block()
details[_uuid] = {
name: rname
, id
, partitions
, commits
, _hash
, _uuid
}
const rows = values(details)
, ranked = Object.assign([{}, {}], values(rows.reduce((p, { _hash }) => {
p[_hash] = p[_hash] || { _hash, count: 0 }
p[_hash].count++
return p
}, {})).sort(za('count', '_hash')))
reset()
print(rows.map(d => {
d.hash = ranked[0] && d._hash == ranked[0]._hash ? green(d._hash)
: ranked[1] && d._hash == ranked[1]._hash ? yellow(d._hash)
: red(d._hash)
return d
}), ['name', 'id', '_uuid'])
})
udp
.on('stop')
.map(([uuid]) => {
delete details[uuid]
clear()
reset()
print(values(details), ['name', 'id', '_uuid'])
})
udp.multicast(`list ${name || ''}`)
}
function print(
rows
, sort = []
, head = keys(rows[0]).filter(by(0, not(is('_')))).slice(0, is.num(table) ? table : Infinity)
){
const table = new Table({ head })
table.push(...rows
.sort(az(...sort))
.map(key(head, 'NULL'))
.map(values)
.map(d => d.map(str))
)
console.log(table.toString())
}
async function get(){
const client = await fero(name, { client: 'monitor' })
, conditions = (
is.arr(where) ? where
: is.def(where) ? [where]
: []
).map(d => d.split('='))
await client.once('connected')
stream(client)
.each(() => {
clear()
reset()
const output = key(k)(client)
json === true ? console.log(JSON.stringify(output))
: table === false ? console.log(output)
: print(
values(output).filter(row => conditions.every(([k, v]) => lo(str(key(k)(row))).includes(lo(v))))
, is.str(sort) ? sort.split(',') : undefined
, is.str(table) ? table.split(',') : undefined
)
})
.source
.emit('start')
}
async function set(){
const client = await fero(name, { client: 'monitor' })
await client.once('connected.init')
await client.update(k, v).on('reply')
client.destroy()
}
function clear(){
process.stdout.write('\033[2J');
process.stdout.write('\033[0f');
}
function reset(){
cursorTo(process.stdout, 0, 0)
}