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