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