Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 3 additions & 93 deletions bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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'))
Expand All @@ -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')) {
Expand All @@ -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 <filename> # 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)
}

113 changes: 113 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we name this function cli or something?

as down at the bottom, we call module.exports(...) and took me a few moments to grok what was happening.

// 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 <filename> # 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') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can there be a comment to describe what this is doing?

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's a lot happening here, but it's hard to tell what is happening, what about:

var args = process.argv.slice(2)
var [methodArgs, configArgs] = splitArgsBySeparator(args)
var appName = process.env.ssb_appname
var configOptions = minimist(configArgs)
var config = Config(appName, configOptions)

cli(config, methodArgs)

function splitArgvBySeparator (args) {
  var splitIndex = args.indexOf('--')
  var methodArgs = splitIndex == -1 ? args : args.slice(0, splitIndex)
  var configArgs = args.slice(splitIndex + 1)
  return [methodArgs, configArgs]
} 

}

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -53,15 +53,15 @@
"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",
"ssb-query": "^2.1.0",
"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": {
Expand Down Expand Up @@ -98,3 +98,4 @@
"author": "Paul Frazee <pfrazee@gmail.com>",
"license": "MIT"
}