-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·494 lines (432 loc) · 12 KB
/
cli.js
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#! /usr/bin/env node
'use strict';
var program = require('commander')
, _ = require('lodash')
, colors = require('colors')
, Table = require('cli-table')
, Thunderpush = require('./lib/thunderpush')
, thunderpush;
var stdin = '';
function collect(val, memo) {
memo.push(val);
return memo;
}
function increaseVerbosity(v, total) {
return total + 1;
}
function connect() {
var options = {};
if(program.host) options.hostname = program.host;
if(program.port) options.port = program.port;
if(program.key) options.key = program.key;
if(program.secretKey) options.secret = program.secretKey;
var thunderpush = new Thunderpush(options, program.verbose);
return thunderpush;
}
program
.version('0.0.1')
.option('-h, --host <hostname>', 'Hostname of service', 'localhost')
.option('-p, --port <port>', 'Port of service', '80')
.option('-k, --key <key>', 'Key for service [required]')
.option('-s, --secret-key <secretKey>', 'Secret key for service [required]')
.option('-v, --verbose', 'Add verbosity', increaseVerbosity, 0)
program.on('--help', function(){
console.log(' Examples:');
console.log('');
console.log(' $ thunderpush-cli -vvv -k key -s secret ping');
console.log(' DEBUG: Sending ping');
console.log(' DEBUG: Server took 200ms');
console.log('');
});
/**
* Ping the server
*
* $ thunderpush-cli -k key -s secret ping
* pong
*
*/
program
.command('ping')
.description('Test connection')
.action(function(env, options) {
try {
connect().ping(function(res) {
res.setEncoding('utf8');
res.on('data', function(chunk) {
try {
var data = JSON.parse(chunk);
if(data.match('pong')) {
process.stderr.write('unexpected response from server\n');
}
else {
process.stdout.write('pong\n');
}
}
catch(e) {
process.stderr.write('unexpected response from server\n');
}
});
}).on('error', function(e) {
process.stderr.write(e.message + '\n');
});
}
catch(e) {
process.stderr.write(e.message + '\n');
}
})
.on('--help', function(){
console.log(' Examples:');
console.log('');
console.log(' $ thunderpush-cli -k key -s secret ping');
console.log(' pong');
console.log('');
});
/**
* Get number of open connections
*
* $ thunderpush-cli -k key -s secret server:connections
* 2
*/
program
.command('server:connections')
.description('Get number of connections')
.action(function(env, options) {
try {
connect().user().get(function(res) {
res.setEncoding('utf8');
res.on('data', function(chunk) {
try {
var data = JSON.parse(chunk);
process.stdout.write(data.count + '\n');
}
catch(e) {
process.stderr.write('unexpected response from server\n');
}
})
}).on('error', function(e) {
process.stderr.write(e.message + '\n');
});
}
catch(e) {
process.stderr.write(e.message + '\n')
}
})
.on('--help', function(){
console.log(' Examples:');
console.log('');
console.log(' $ thunderpush-cli -k key -s secret server:connections');
console.log(' 2');
console.log('');
});
/**
* Get number of open connections
*
* $ thunderpush-cli -k key -s secret server:users
* johndoe
* jimsmith
* bob
*
* Show total
*
* $ thunderpush-cli -k key -s secret server:users --count
* total 3
* johndoe
* jimsmith
* bob
*
*/
program
.command('server:users')
.description('Get count of users')
.option('--count', 'Show total')
.action(function(env, options) {
process.stdin.write('oops, not yet implemented in the server'.yellow + '\n');
})
.on('--help', function(){
console.log(' Examples:');
console.log('');
console.log(' $ thunderpush-cli -k key -s secret server:users');
console.log(' johndoe');
console.log(' jimsmith');
console.log(' bob');
console.log('');
});
/**
* All populated channels
*
* Get all populated channel
*
* $ thunderpush-cli -k key -s secret server:channels
* chan1
* chan2
*
* Get total with output
*
* $ thunderpush-cli -k key -s secret server:channels --count
* total 2
* chan1
* chan2
*/
program
.command('server:channels')
.description('Get list of channels')
.action(function() {
process.stdin.write('oops, yet to be implemented in the server'.yellow + '\n');
})
.on('--help', function(){
console.log(' Examples:');
console.log('');
console.log(' $ thunderpush-cli -k key -s secret server:channels');
console.log(' chan1');
console.log(' chan2');
console.log('');
console.log(' Get total with output');
console.log(' $ thunderpush-cli -k key -s secret server:channels --count');
console.log(' total 2');
console.log(' chan1');
console.log(' chan2');
console.log('');
});
/**
* Users subscribed to a channel
*
* Get users of populated channel
*
* $ thunderpush-cli -k key -s secret channel:users chan1
* johndoe
* jimsmith
*
* Get users of **unpopulated** channel
*
* $ thunderpush-cli -k key -s secret channel:users chan1
*
*
* Get total with output
*
* $ thunderpush-cli -k key -s secret channel:users chan1 --count
* total 2
* johndoe
* jimsmith
*/
program
.command('channel:users <channel>')
.description('Get list of users in a channel')
.option('--count', 'Count of users')
.action(function(channel, options) {
try {
connect().channel(channel).get(function(res) {
res.setEncoding('utf8');
res.on('data', function(chunk) {
try {
var data = JSON.parse(chunk);
if(options.count) {
process.stdout.write('total ' + data.users.length + '\n');
}
_.each(data.users, function(user) {
process.stdout.write(user + '\n');
});
}
catch(e) {
process.stderr.write('unexpected response from server\n');
}
});
}).on('error', function(e) {
process.stderr.write(e.message + '\n');
});
}
catch(e) {
process.stderr.write(e.message + '\n')
}
})
.on('--help', function(){
console.log(' Examples:');
console.log('');
console.log(' $ thunderpush-cli -k key -s secret channel:users chan1');
console.log(' johndoe');
console.log(' jimsmith');
console.log('');
console.log(' Get total with output');
console.log(' $ thunderpush-cli -k key -s secret channel:users chan1 --count');
console.log(' total 2');
console.log(' johndoe');
console.log(' jimsmith');
console.log('');
});
/**
* Send message to a channel
*
* Populated channel
*
* $ thunderpush-cli -k key -s secret channel:message chan1 hi!
* 12
* $ echo "123" | thunderpush-cli -k key -s secret channel:message chan2
* 1
*
* Unpopulated channel
*
* $ thunderpush-cli -k key -s secret channel:message chan1 hi!
* 0
*/
program
.command('channel:message <channel> [message]')
.description('Send message to a channel')
.action(function(channel, message, options) {
message = '';
if(stdin) {
message = stdin.toString().trim();
}
try {
connect().channel(channel).message(message, function(res) {
res.setEncoding('utf8');
res.on('data', function(chunk) {
try {
var count = JSON.parse(chunk).count || 0;
process.stdout.write(count + '\n');
}
catch(e) {
process.stderr.write('unexpected response from server\n');
}
})
}).on('error', function(e) {
process.stderr.write(e.message + '\n');
});
}
catch(e) {
process.stderr.write(e.message + '\n')
}
})
.on('--help', function(){
console.log(' Examples:');
console.log('');
console.log(' $ thunderpush-cli -k key -s secret channel:message chan1 hi!');
console.log(' 12');
console.log('');
console.log(' Piping output');
console.log(' $ echo "123" | thunderpush-cli -k key -s secret channel:message chan2');
console.log(' 1');
console.log('');
console.log(' Unpopulated channel');
console.log(' $ thunderpush-cli -k key -s secret channel:message chan3 {"hello":"world"}');
console.log(' 0');
console.log('');
});
/**
* Users online presence
*
* Online user
*
* $ thunderpush-cli -k key -s secret user:presence janedoe
* online
*
* Offline user
*
* $ thunderpush-cli -k key -s secret user:presence johndoe
* offline
*/
program
.command('user:presence <user>')
.description('Get user presence')
.action(function(user, options) {
try {
connect().user(user).get(function(res) {
res.setEncoding('utf8');
res.on('data', function(chunk) {
try {
var data = JSON.parse(chunk);
process.stdout.write((data.online ? 'online' : 'offline') + '\n');
}
catch(e) {
process.stderr.write('unexpected response from server\n');
}
})
}).on('error', function(e) {
process.stderr.write(e.message + '\n');
});
}
catch(e) {
process.stderr.write(e.message + '\n');
}
})
.on('--help', function(){
console.log(' Examples:');
console.log('');
console.log(' $ thunderpush-cli -k key -s secret user:presence janedoe');
console.log(' online');
console.log('');
console.log(' $ thunderpush-cli -k key -s secret user:presence johndoe');
console.log(' offline');
console.log('');
});
/**
* Send message to a user
*
* Online user
*
* $ thunderpush-cli -k key -s secret user:message janedoe hi!
* 1
* $ echo "123" | thunderpush-cli -k key -s secret user:message janedoe
* 1
*
* Offline user
*
* $ thunderpush-cli -k key -s secret user:message janedoe hi!
* 0
*/
program
.command('user:message <user> [message]')
.description('Send message to a user')
.action(function(user, message, options) {
message = '';
if(stdin) {
message = stdin.toString().trim();
}
try {
connect().user(user).message(message, function(res) {
res.setEncoding('utf8');
res.on('data', function(chunk) {
try {
var count = JSON.parse(chunk).count || 0;
process.stdout.write(count + '\n');
}
catch(e) {
process.stderr.write('unexpected response from server\n');
}
})
}).on('error', function(e) {
process.stderr.write(e.message + '\n');
});
}
catch(e) {
process.stderr.write(e.message + '\n')
}
})
.on('--help', function(){
console.log(' Examples:');
console.log('');
console.log(' $ thunderpush-cli -k key -s secret user:message johndoe hi!');
console.log(' 1');
console.log('');
console.log(' Piping output');
console.log(' $ echo "123" | thunderpush-cli -k key -s secret user:message johndoe');
console.log(' 1');
console.log('');
console.log(' Offline user');
console.log(' $ thunderpush-cli -k key -s secret user:message janedoe {"hello":"world"}');
console.log(' 0');
console.log('');
});
// Handle piping
if(process.stdin.isTTY) {
program.parse(process.argv);
}
else {
process.stdin.on('readable', function() {
var chunk = this.read();
if (chunk !== null) {
stdin += chunk;
}
});
process.stdin.on('end', function() {
program.parse(process.argv);
});
}