Files: b3923852a5471785a61c33094414b62fa845b7b5 / bin.js
4301 bytesRaw
1 | |
2 | |
3 | var fs = require('fs') |
4 | var path = require('path') |
5 | var pull = require('pull-stream') |
6 | var toPull = require('stream-to-pull-stream') |
7 | var File = require('pull-file') |
8 | var explain = require('explain-error') |
9 | var ssbKeys = require('ssb-keys') |
10 | var stringify = require('pull-stringify') |
11 | var createHash = require('multiblob/util').createHash |
12 | var minimist = require('minimist') |
13 | var muxrpcli = require('muxrpcli') |
14 | var cmdAliases = require('./lib/cli-cmd-aliases') |
15 | |
16 | //get config as cli options after --, options before that are |
17 | //options to the command. |
18 | var argv = process.argv.slice(2) |
19 | var i = argv.indexOf('--') |
20 | var conf = argv.slice(i+1) |
21 | argv = ~i ? argv.slice(0, i) : argv |
22 | |
23 | var config = require('ssb-config/inject')(process.env.ssb_appname, minimist(conf)) |
24 | |
25 | var keys = ssbKeys.loadOrCreateSync(path.join(config.path, 'secret')) |
26 | if(keys.curve === 'k256') |
27 | throw new Error('k256 curves are no longer supported,'+ |
28 | 'please delete' + path.join(config.path, 'secret')) |
29 | |
30 | var manifestFile = path.join(config.path, 'manifest.json') |
31 | |
32 | if (argv[0] == 'server') { |
33 | |
34 | // special server command: |
35 | // import sbot and start the server |
36 | |
37 | var createSbot = require('./') |
38 | .use(require('./plugins/plugins')) |
39 | .use(require('./plugins/master')) |
40 | .use(require('./plugins/gossip')) |
41 | .use(require('./plugins/friends')) |
42 | .use(require('./plugins/replicate')) |
43 | .use(require('ssb-blobs')) |
44 | .use(require('./plugins/invite')) |
45 | // .use(require('./plugins/block')) |
46 | .use(require('./plugins/local')) |
47 | .use(require('./plugins/logging')) |
48 | .use(require('./plugins/private')) |
49 | .use(require('ssb-query')) |
50 | .use(require('ssb-links')) |
51 | .use(require('ssb-ws')) |
52 | .use(require('ssb-ebt')) |
53 | |
54 | // add third-party plugins |
55 | // require('./plugins/plugins').loadUserPlugins(createSbot, config) |
56 | |
57 | // start server |
58 | |
59 | config.keys = keys |
60 | var server = createSbot(config) |
61 | |
62 | // write RPC manifest to ~/.ssb/manifest.json |
63 | fs.writeFileSync(manifestFile, JSON.stringify(server.getManifest(), null, 2)) |
64 | } else { |
65 | |
66 | // normal command: |
67 | // create a client connection to the server |
68 | |
69 | // read manifest.json |
70 | var manifest |
71 | try { |
72 | manifest = JSON.parse(fs.readFileSync(manifestFile)) |
73 | } catch (err) { |
74 | throw explain(err, |
75 | 'no manifest file' |
76 | + '- should be generated first time server is run' |
77 | ) |
78 | } |
79 | |
80 | // connect |
81 | require('ssb-client')(keys, { |
82 | manifest: manifest, |
83 | port: config.port, |
84 | host: config.host||'localhost', |
85 | caps: config.caps, |
86 | key: config.key || keys.id |
87 | }, function (err, rpc) { |
88 | if(err) { |
89 | if (/could not connect/.test(err.message)) { |
90 | console.log('Error: Could not connect to the scuttlebot server.') |
91 | console.log('Use the "server" command to start it.') |
92 | if(config.verbose) throw err |
93 | process.exit(1) |
94 | } |
95 | throw err |
96 | } |
97 | |
98 | // add aliases |
99 | for (var k in cmdAliases) { |
100 | rpc[k] = rpc[cmdAliases[k]] |
101 | manifest[k] = manifest[cmdAliases[k]] |
102 | } |
103 | |
104 | // add some extra commands |
105 | manifest.version = 'async' |
106 | manifest.config = 'sync' |
107 | rpc.version = function (cb) { |
108 | console.log(require('./package.json').version) |
109 | cb() |
110 | } |
111 | rpc.config = function (cb) { |
112 | console.log(JSON.stringify(config, null, 2)) |
113 | cb() |
114 | } |
115 | |
116 | // HACK |
117 | // we need to output the hash of blobs that are added via blobs.add |
118 | // because muxrpc doesnt support the `sink` callback yet, we need this manual override |
119 | // -prf |
120 | if (process.argv[2] === 'blobs.add') { |
121 | var filename = process.argv[3] |
122 | var source = |
123 | filename ? File(process.argv[3]) |
124 | : !process.stdin.isTTY ? toPull.source(process.stdin) |
125 | : (function () { |
126 | console.error('USAGE:') |
127 | console.error(' blobs.add <filename> # add a file') |
128 | console.error(' source | blobs.add # read from stdin') |
129 | process.exit(1) |
130 | })() |
131 | var hasher = createHash('sha256') |
132 | pull( |
133 | source, |
134 | hasher, |
135 | rpc.blobs.add(function (err) { |
136 | if (err) |
137 | throw err |
138 | console.log('&'+hasher.digest) |
139 | process.exit() |
140 | }) |
141 | ) |
142 | return |
143 | } |
144 | |
145 | // run commandline flow |
146 | muxrpcli(argv, manifest, rpc, config.verbose) |
147 | }) |
148 | } |
149 | |
150 | |
151 | |
152 |
Built with git-ssb-web