-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample.js
More file actions
executable file
·58 lines (48 loc) · 1.4 KB
/
example.js
File metadata and controls
executable file
·58 lines (48 loc) · 1.4 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
#! /usr/bin/env node
var muxrpcli = require('.')
var zerr = require('zerr')
var MissingArgError = zerr('BadArg', '"%" is required')
var BadTypeError = zerr('BadArg', '"%" must be a valid %')
function isAddress (v) {
return v && typeof v == 'string' && v.split('.').length == 4
}
var manifest = {
usage: 'async',
whoami: 'async',
ping: 'async',
help: 'sync'
}
var api = {
usage: function (cmd, cb) {
switch (cmd) {
case 'whoami':
return cb(null, 'whoami. get your profile info.')
case 'ping':
return cb(null, 'ping {target} [-n times]. send `n` pings to `target`, defaults to 1')
}
cb(null, [
'myexample usage:',
' - whoami. get your profile info.',
' - ping {target} [-n times]. send `n` pings to `target`, defaults to 1'
].join('\n'))
},
whoami: function(cb) {
cb(null, 'bob, obviously')
},
ping: function(target, opts, cb) {
if (typeof target == 'function') cb = target, target = null
if (typeof opts == 'function') cb = opts, opts = null
if (!target) return cb(MissingArgError('target'))
if (!isAddress(target)) return cb(BadTypeError('target', 'address'))
var n = 1
if (opts && opts.n) {
n = +opts.n
if (isNaN(n)) return cb(BadTypeError('n', 'number'))
}
for (var i=0; i < n; i++) {
console.log('ping', target)
}
cb()
}
}
muxrpcli(process.argv.slice(2), manifest, api)