git ssb

4+

Dominic / scuttlebot



Tree: 5653a26d6cfcd9a9cff414d2c291fc24e7a294aa

Files: 5653a26d6cfcd9a9cff414d2c291fc24e7a294aa / bin.js

4258 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/replicate'))
42 .use(require('ssb-friends'))
43 .use(require('ssb-blobs'))
44 .use(require('./plugins/invite'))
45 .use(require('./plugins/local'))
46 .use(require('./plugins/logging'))
47 .use(require('./plugins/private'))
48 .use(require('ssb-query'))
49 .use(require('ssb-links'))
50 .use(require('ssb-ws'))
51// .use(require('ssb-ebt'))
52
53 // add third-party plugins
54 require('./plugins/plugins').loadUserPlugins(createSbot, config)
55
56 // start server
57
58 config.keys = keys
59 var server = createSbot(config)
60
61 // write RPC manifest to ~/.ssb/manifest.json
62 fs.writeFileSync(manifestFile, JSON.stringify(server.getManifest(), null, 2))
63} else {
64
65 // normal command:
66 // create a client connection to the server
67
68 // read manifest.json
69 var manifest
70 try {
71 manifest = JSON.parse(fs.readFileSync(manifestFile))
72 } catch (err) {
73 throw explain(err,
74 'no manifest file'
75 + '- should be generated first time server is run'
76 )
77 }
78
79 // connect
80 require('ssb-client')(keys, {
81 manifest: manifest,
82 port: config.port,
83 host: config.host||'localhost',
84 caps: config.caps,
85 key: config.key || keys.id
86 }, function (err, rpc) {
87 if(err) {
88 if (/could not connect/.test(err.message)) {
89 console.log('Error: Could not connect to the scuttlebot server.')
90 console.log('Use the "server" command to start it.')
91 if(config.verbose) throw err
92 process.exit(1)
93 }
94 throw err
95 }
96
97 // add aliases
98 for (var k in cmdAliases) {
99 rpc[k] = rpc[cmdAliases[k]]
100 manifest[k] = manifest[cmdAliases[k]]
101 }
102
103 // add some extra commands
104 manifest.version = 'async'
105 manifest.config = 'sync'
106 rpc.version = function (cb) {
107 console.log(require('./package.json').version)
108 cb()
109 }
110 rpc.config = function (cb) {
111 console.log(JSON.stringify(config, null, 2))
112 cb()
113 }
114
115 // HACK
116 // we need to output the hash of blobs that are added via blobs.add
117 // because muxrpc doesnt support the `sink` callback yet, we need this manual override
118 // -prf
119 if (process.argv[2] === 'blobs.add') {
120 var filename = process.argv[3]
121 var source =
122 filename ? File(process.argv[3])
123 : !process.stdin.isTTY ? toPull.source(process.stdin)
124 : (function () {
125 console.error('USAGE:')
126 console.error(' blobs.add <filename> # add a file')
127 console.error(' source | blobs.add # read from stdin')
128 process.exit(1)
129 })()
130 var hasher = createHash('sha256')
131 pull(
132 source,
133 hasher,
134 rpc.blobs.add(function (err) {
135 if (err)
136 throw err
137 console.log('&'+hasher.digest)
138 process.exit()
139 })
140 )
141 return
142 }
143
144 // run commandline flow
145 muxrpcli(argv, manifest, rpc, config.verbose)
146 })
147}
148
149
150
151
152
153

Built with git-ssb-web