git ssb

4+

Dominic / scuttlebot



Tree: ff951035989067cd41a4127422e8cfb00361ebeb

Files: ff951035989067cd41a4127422e8cfb00361ebeb / bin.js

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

Built with git-ssb-web