-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
cli.js
122 lines (111 loc) · 3.26 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
var path = require('path')
var pull = require('pull-stream')
var stringify = require('pull-stringify')
var toPull = require('stream-to-pull-stream')
var scuttlebot = require('../')
var config = require('../config')
var api = require('./api')
var ssbkeys = require('ssb-keys')
exports.serve = function(opts) {
if (opts.port)
config.port = opts.port
var server = scuttlebot(config)
console.log('Scuttlebot now serving on port', config.port)
function onExit() { /* :TODO: any cleanup? */ process.exit() }
process.on('SIGINT', onExit).on('SIGTERM', onExit)
}
exports.repl = function(opts, rpcclient) {
var repl = require('repl')
var r = repl.start(opts.host + ':' + opts.port + '> ')
// import the rpc api into the top level
var api = scuttlebot.connect(opts)
for (var k in api) {
if (typeof api[k] == 'function')
r.context[k] = api[k].bind(api)
}
api.conn.on('error', function(err) {
console.error(err)
process.exit(1)
})
r.on('exit', function() { api.conn.end() })
}
exports.exec = function(cmd) {
return function(opts) {
delete opts[0]
delete opts._
var keys = require('ssb-keys').loadSync(path.join(config.path, 'secret'))
var rpc = scuttlebot.createClient({port: config.port, host: 'localhost'})
// if there's data coming from stdin, pipe that into our command
if(!process.stdin.isTTY) {
pull(
toPull.source(process.stdin),
pull.collect(function (err, ary) {
var str = Buffer.concat(ary).toString('utf8')
var data = JSON.parse(str)
next(data)
})
)
}
else
next(opts)
function next (data) {
//set $rel as key name if it's missing.
defaultRel(data)
console.log(data)
rpc.auth(ssbkeys.signObj(keys, {
role: 'client',
ts: Date.now(),
public: keys.public
}), function (err) {
if(err) throw err
var isAsyncCmd = contains(cmd, api.manifest.async)
if(isAsyncCmd) {
// massage data as needed
if (cmd == 'getPublicKey' && data && typeof data == 'object')
data = data[1]
if (cmd == 'add' && data && typeof data == 'object')
data = data[1]
// run command
console.log(data)
rpc[cmd](data, function (err, ret) {
if(err) throw err
console.log(JSON.stringify((ret), null, 2))
process.exit()
})
}
else {
// run command
pull(
rpc[cmd](data),
stringify('', '\n', '\n\n', 2, JSON.stringify),
toPull.sink(process.stdout, function (err) {
if(err) throw err
process.exit()
})
)
}
})
}
}
}
function isObject (o) {
return o && 'object' === typeof o && !Buffer.isBuffer(o)
}
// helper, does a contain s?
function contains (s, a) {
if(!a) return false
return !!~a.indexOf(s)
}
// helper, sets a default reltype on links which are missing them
function defaultRel (o, r) {
if(!isObject(o)) return o
for(var k in o) {
if(isObject(o[k]))
defaultRel(o[k], k)
else if(k[0] === '$' && ~['$msg', '$ext', '$feed'].indexOf(k)) {
if(!o.$rel)
o.$rel = r ? r : o.type
}
}
return o
}