From 0e83adc469280cc9c9611e7e7804661bc66ec233 Mon Sep 17 00:00:00 2001 From: Dominic Tarr Date: Sun, 31 Mar 2019 19:15:13 +1300 Subject: [PATCH 1/2] deps --- package.json | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 229e54d5..a57fba23 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ "multiserver": "^3.3.1", "multiserver-address": "^1.0.1", "muxrpc-validation": "^3", - "muxrpcli": "^1.0.0", + "muxrpcli": "^3.0.0", "mv": "^2.1.1", "osenv": "^0.1.5", "pull-cat": "~1.1.5", @@ -53,7 +53,7 @@ "ssb-ebt": "^5.5.3", "ssb-friends": "^4.0.0", "ssb-gossip": "^1.0.6", - "ssb-invite": "^2.0.3", + "ssb-invite": "^2.0.4", "ssb-keys": "^7.1.1", "ssb-links": "^3.0.2", "ssb-ooo": "^1.2.2", @@ -61,7 +61,7 @@ "ssb-ref": "^2.13.9", "ssb-replicate": "^1.2.1", "ssb-ws": "^5.1.1", - "stream-to-pull-stream": "^1.6.10", + "stream-to-pull-stream": "^1.7.3", "zerr": "^1.0.0" }, "devDependencies": { @@ -98,3 +98,4 @@ "author": "Paul Frazee ", "license": "MIT" } + From 30ba1662c4ca57251481f672052aca00b0eb2652 Mon Sep 17 00:00:00 2001 From: Dominic Tarr Date: Sun, 31 Mar 2019 19:15:44 +1300 Subject: [PATCH 2/2] move out cli stuff, and base that on muxrpc-usage@3 --- bin.js | 96 ++---------------------------------------------- cli.js | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 93 deletions(-) create mode 100755 cli.js diff --git a/bin.js b/bin.js index 7d52eb28..383845a4 100755 --- a/bin.js +++ b/bin.js @@ -2,16 +2,10 @@ var fs = require('fs') var path = require('path') -var pull = require('pull-stream') -var toPull = require('stream-to-pull-stream') -var File = require('pull-file') var explain = require('explain-error') var Config = require('ssb-config/inject') -var Client = require('ssb-client') var createHash = require('multiblob/util').createHash var minimist = require('minimist') -var muxrpcli = require('muxrpcli') -var cmdAliases = require('./lib/cli-cmd-aliases') var ProgressBar = require('./lib/progress') var packageJson = require('./package.json') @@ -46,7 +40,7 @@ if (argv[0] == 'start') { .use(require('./plugins/onion')) .use(require('./plugins/unix-socket')) .use(require('./plugins/no-auth')) - .use(require('./plugins/plugins')) + .use(require('../ssb-plugins2')) .use(require('./plugins/master')) .use(require('ssb-gossip')) .use(require('ssb-replicate')) @@ -60,7 +54,7 @@ if (argv[0] == 'start') { .use(require('ssb-ebt')) .use(require('ssb-ooo')) // add third-party plugins - require('./plugins/plugins').loadUserPlugins(createSsbServer, config) + require('../ssb-plugins2').loadUserPlugins(createSsbServer, config) if (argv[1] != '--disable-ssb-links') { if (!createSsbServer.plugins.find(p => p.name == 'links2')) { @@ -78,90 +72,6 @@ if (argv[0] == 'start') { if(process.stdout.isTTY && (config.logging.level != 'info')) ProgressBar(server.progress) } else { - // normal command: - // create a client connection to the server - - // read manifest.json - var manifest - try { - manifest = JSON.parse(fs.readFileSync(manifestFile)) - } catch (err) { - throw explain(err, - 'no manifest file' - + '- should be generated first time server is run' - ) - } - - var opts = { - manifest: manifest, - port: config.port, - host: config.host || 'localhost', - caps: config.caps, - key: config.key || config.keys.id - } - - // connect - Client(config.keys, opts, function (err, rpc) { - if(err) { - if (/could not connect/.test(err.message)) { - console.error('Error: Could not connect to ssb-server ' + opts.host + ':' + opts.port) - console.error('Use the "start" command to start it.') - console.error('Use --verbose option to see full error') - if(config.verbose) throw err - process.exit(1) - } - throw err - } - - // add aliases - for (var k in cmdAliases) { - rpc[k] = rpc[cmdAliases[k]] - manifest[k] = manifest[cmdAliases[k]] - } - - // add some extra commands -// manifest.version = 'async' - manifest.config = 'sync' -// rpc.version = function (cb) { -// console.log(packageJson.version) -// cb() -// } - rpc.config = function (cb) { - console.log(JSON.stringify(config, null, 2)) - cb() - } - - // HACK - // we need to output the hash of blobs that are added via blobs.add - // because muxrpc doesnt support the `sink` callback yet, we need this manual override - // -prf - if (process.argv[2] === 'blobs.add') { - var filename = process.argv[3] - var source = - filename ? File(process.argv[3]) - : !process.stdin.isTTY ? toPull.source(process.stdin) - : (function () { - console.error('USAGE:') - console.error(' blobs.add # add a file') - console.error(' source | blobs.add # read from stdin') - process.exit(1) - })() - var hasher = createHash('sha256') - pull( - source, - hasher, - rpc.blobs.add(function (err) { - if (err) - throw err - console.log('&'+hasher.digest) - process.exit() - }) - ) - return - } - - // run commandline flow - muxrpcli(argv, manifest, rpc, config.verbose) - }) + require('./cli')(config, argv) } diff --git a/cli.js b/cli.js new file mode 100755 index 00000000..cf8ad135 --- /dev/null +++ b/cli.js @@ -0,0 +1,113 @@ +#! /usr/bin/env node +var fs = require('fs') +var path = require('path') +var pull = require('pull-stream') +var toPull = require('stream-to-pull-stream') +var File = require('pull-file') +var explain = require('explain-error') +var Config = require('ssb-config/inject') +var Client = require('ssb-client') +var createHash = require('multiblob/util').createHash +var minimist = require('minimist') +var muxrpcli = require('muxrpcli') +var cmdAliases = require('./lib/cli-cmd-aliases') +//var ProgressBar = require('./lib/progress') +var packageJson = require('./package.json') + +//get config as cli options after --, options before that are +//options to the command. + +module.exports = function (config, argv) { +// var argv = config._ + var manifestFile = path.join(config.path, 'manifest.json') + + // normal command: + // create a client connection to the server + + // read manifest.json + var manifest + try { + manifest = JSON.parse(fs.readFileSync(manifestFile)) + } catch (err) { + throw explain(err, + 'no manifest file' + + '- should be generated first time server is run' + ) + } + + var opts = { + manifest: manifest, + port: config.port, + host: config.host || 'localhost', + caps: config.caps, + key: config.key || config.keys.id + } + + // connect + Client(config.keys, opts, function (err, rpc) { + if(err) { + if (/could not connect/.test(err.message)) { + console.error('Error: Could not connect to ssb-server ' + opts.host + ':' + opts.port) + console.error('Use the "start" command to start it.') + console.error('Use --verbose option to see full error') + if(config.verbose) throw err + process.exit(1) + } + throw err + } + + // add aliases + for (var k in cmdAliases) { + rpc[k] = rpc[cmdAliases[k]] + manifest[k] = manifest[cmdAliases[k]] + } + + // add config command (should we remove this? it's not documented!) + manifest.config = 'sync' + rpc.config = function (cb) { + console.log(JSON.stringify(config, null, 2)) + cb() + } + + // HACK + // we need to output the hash of blobs that are added via blobs.add + // because muxrpc doesnt support the `sink` callback yet, we need this manual override + // -prf + if (process.argv[2] === 'blobs.add') { + var filename = process.argv[3] + var source = + filename ? File(process.argv[3]) + : !process.stdin.isTTY ? toPull.source(process.stdin) + : (function () { + console.error('USAGE:') + console.error(' blobs.add # add a file') + console.error(' source | blobs.add # read from stdin') + process.exit(1) + })() + var hasher = createHash('sha256') + pull( + source, + hasher, + rpc.blobs.add(function (err) { + if (err) + throw err + console.log('&'+hasher.digest) + process.exit() + }) + ) + return + } + + // run commandline flow + muxrpcli(argv, manifest, rpc, config.verbose) + }) +} + +if(!module.parent && process.title != 'browser') { + var argv = process.argv.slice(2) + var i = argv.indexOf('--') + var conf = argv.slice(i+1) + argv = ~i ? argv.slice(0, i) : argv + module.exports(Config(process.env.ssb_appname, minimist(conf)), argv) +} +