git ssb

4+

Dominic / scuttlebot



Tree: d06669ad6f6bfbbd8fef97b6e36fec7f5e505483

Files: d06669ad6f6bfbbd8fef97b6e36fec7f5e505483 / bin.js

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

Built with git-ssb-web