From cba42ca02a4ad27ef46af010718f5a9e7490ca3b Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Tue, 2 Aug 2016 23:15:00 +0200 Subject: [PATCH 1/5] fix(cli): replace ronin with yargs Closes #331 --- package.json | 8 ++-- src/cli/bin.js | 22 ++++++--- src/cli/commands/bitswap.js | 15 +++++++ src/cli/commands/bitswap/stat.js | 14 +++--- src/cli/commands/bitswap/unwant.js | 15 +++---- src/cli/commands/bitswap/wantlist.js | 19 +++++--- src/cli/commands/block.js | 15 +++++++ src/cli/commands/block/get.js | 19 ++++---- src/cli/commands/block/put.js | 17 +++---- src/cli/commands/block/rm.js | 19 ++++---- src/cli/commands/block/stat.js | 19 ++++---- src/cli/commands/bootstrap.js | 15 +++++++ src/cli/commands/bootstrap/add.js | 15 ++++--- src/cli/commands/bootstrap/list.js | 13 +++--- src/cli/commands/bootstrap/rm.js | 15 ++++--- src/cli/commands/commands.js | 11 ++--- src/cli/commands/config.js | 47 ++++++++++++-------- src/cli/commands/config/edit.js | 16 ++++--- src/cli/commands/config/replace.js | 18 +++++--- src/cli/commands/config/show.js | 16 ++++--- src/cli/commands/daemon.js | 11 ++--- src/cli/commands/dns.js | 10 ++--- src/cli/commands/files.js | 15 +++++++ src/cli/commands/files/add.js | 15 ++++--- src/cli/commands/files/cat.js | 20 ++++----- src/cli/commands/files/get.js | 30 +++++++------ src/cli/commands/id.js | 16 ++++--- src/cli/commands/init.js | 32 ++++++------- src/cli/commands/ls.js | 11 ++--- src/cli/commands/mount.js | 10 ++--- src/cli/commands/object.js | 15 +++++++ src/cli/commands/object/data.js | 17 +++---- src/cli/commands/object/get.js | 17 +++---- src/cli/commands/object/links.js | 17 +++---- src/cli/commands/object/new.js | 13 +++--- src/cli/commands/object/patch.js | 15 +++++++ src/cli/commands/object/patch/add-link.js | 27 ++++------- src/cli/commands/object/patch/append-data.js | 21 ++++----- src/cli/commands/object/patch/rm-link.js | 22 ++++----- src/cli/commands/object/patch/set-data.js | 21 ++++----- src/cli/commands/object/put.js | 19 ++++---- src/cli/commands/object/stat.js | 17 +++---- src/cli/commands/ping.js | 11 ++--- src/cli/commands/refs.js | 10 ++--- src/cli/commands/repo.js | 15 +++++++ src/cli/commands/repo/gc.js | 13 +++--- src/cli/commands/repo/init.js | 13 +++--- src/cli/commands/repo/version.js | 13 +++--- src/cli/commands/resolve.js | 11 ++--- src/cli/commands/swarm.js | 15 +++++++ src/cli/commands/swarm/addrs.js | 16 ++++--- src/cli/commands/swarm/addrs/local.js | 13 +++--- src/cli/commands/swarm/connect.js | 23 +++++----- src/cli/commands/swarm/disconnect.js | 13 +++--- src/cli/commands/swarm/peers.js | 13 +++--- src/cli/commands/update.js | 10 ++--- src/cli/commands/version.js | 14 +++--- test/cli/test-commands.js | 2 +- test/cli/test-config.js | 8 ---- test/cli/test-id.js | 2 - 60 files changed, 540 insertions(+), 414 deletions(-) create mode 100644 src/cli/commands/bitswap.js create mode 100644 src/cli/commands/block.js create mode 100644 src/cli/commands/bootstrap.js create mode 100644 src/cli/commands/files.js create mode 100644 src/cli/commands/object.js create mode 100644 src/cli/commands/object/patch.js create mode 100644 src/cli/commands/repo.js create mode 100644 src/cli/commands/swarm.js diff --git a/package.json b/package.json index 4f5bc19f5e..1079f38c18 100644 --- a/package.json +++ b/package.json @@ -83,21 +83,23 @@ "lodash.get": "^4.4.0", "lodash.set": "^4.3.0", "mafmt": "^2.1.1", - "multihashes": "^0.2.2", "multiaddr": "^2.0.2", + "multihashes": "^0.2.2", "path-exists": "^3.0.0", "peer-book": "^0.3.0", "peer-id": "^0.7.0", "peer-info": "^0.7.0", "promisify-es6": "^1.0.1", + "read-pkg-up": "^1.0.1", "readable-stream": "1.1.13", - "ronin": "^0.3.11", "run-parallel": "^1.1.6", "run-parallel-limit": "^1.0.3", "run-series": "^1.1.4", "run-waterfall": "^1.1.3", "temp": "^0.8.3", - "through2": "^2.0.1" + "through2": "^2.0.1", + "update-notifier": "^1.0.2", + "yargs": "^4.8.1" }, "contributors": [ "Andrew de Andrade ", diff --git a/src/cli/bin.js b/src/cli/bin.js index 4e78c38bb9..f22530c10e 100755 --- a/src/cli/bin.js +++ b/src/cli/bin.js @@ -2,12 +2,20 @@ 'use strict' -const ronin = require('ronin') +const yargs = require('yargs') +const updateNotifier = require('update-notifier') +const readPkgUp = require('read-pkg-up') -const cli = ronin(__dirname) +const pkg = readPkgUp.sync().pkg +updateNotifier({ + pkg, + updateCheckInterval: 1000 * 60 * 60 * 24 * 7 // 1 week +}).notify() -cli.run() - -// cli.autoupdate(function () { -// cli.run() -// }) +yargs + .commandDir('commands') + .demand(1) + .help() + .strict() + .completion() + .argv diff --git a/src/cli/commands/bitswap.js b/src/cli/commands/bitswap.js new file mode 100644 index 0000000000..05284b9763 --- /dev/null +++ b/src/cli/commands/bitswap.js @@ -0,0 +1,15 @@ +'use strict' + +module.exports = { + command: 'bitswap', + + description: 'A set of commands to manipulate the bitswap agent.', + + builder (yargs) { + return yargs + .commandDir('bitswap') + }, + + handler (argv) { + } +} diff --git a/src/cli/commands/bitswap/stat.js b/src/cli/commands/bitswap/stat.js index e3643ad218..14668e0768 100644 --- a/src/cli/commands/bitswap/stat.js +++ b/src/cli/commands/bitswap/stat.js @@ -1,15 +1,15 @@ 'use strict' -const Command = require('ronin').Command const utils = require('../../utils') -module.exports = Command.extend({ - desc: 'Show some diagnostic information on the bitswap agent.', +module.exports = { + command: 'stat', - options: { - }, + describe: 'Show some diagnostic information on the bitswap agent.', - run: () => { + builder: {}, + + handler () { utils.getIPFS((err, ipfs) => { if (err) { throw err @@ -35,4 +35,4 @@ bitswap status }) }) } -}) +} diff --git a/src/cli/commands/bitswap/unwant.js b/src/cli/commands/bitswap/unwant.js index e09ea250cc..7a0feb533d 100644 --- a/src/cli/commands/bitswap/unwant.js +++ b/src/cli/commands/bitswap/unwant.js @@ -1,18 +1,13 @@ 'use strict' -const Command = require('ronin').Command const utils = require('../../utils') -module.exports = Command.extend({ - desc: 'Remove a given block from your wantlist.', +module.exports = { + command: 'unwant ', - options: { - key: { - required: true - } - }, + describe: 'Remove a given block from your wantlist.', - run: (key) => { + handler (argv) { utils.getIPFS((err, ipfs) => { if (err) { throw err @@ -21,4 +16,4 @@ module.exports = Command.extend({ throw new Error('Not implemented yet') }) } -}) +} diff --git a/src/cli/commands/bitswap/wantlist.js b/src/cli/commands/bitswap/wantlist.js index bd258ff545..eca9424539 100644 --- a/src/cli/commands/bitswap/wantlist.js +++ b/src/cli/commands/bitswap/wantlist.js @@ -1,15 +1,22 @@ 'use strict' -const Command = require('ronin').Command const utils = require('../../utils') -module.exports = Command.extend({ - desc: 'Print out all blocks currently on the bitswap wantlist for the local peer.', +module.exports = { + command: 'wantlist', - options: { + describe: 'Print out all blocks currently on the bitswap wantlist for the local peer.', + + builder: { + peer: { + alias: 'p', + describe: 'Specify which peer to show wantlist for.', + type: 'string' + } }, - run: () => { + handler (argv) { + // TODO: handle argv.peer utils.getIPFS((err, ipfs) => { if (err) { throw err @@ -23,4 +30,4 @@ module.exports = Command.extend({ }) }) } -}) +} diff --git a/src/cli/commands/block.js b/src/cli/commands/block.js new file mode 100644 index 0000000000..599e202b7a --- /dev/null +++ b/src/cli/commands/block.js @@ -0,0 +1,15 @@ +'use strict' + +module.exports = { + command: 'block', + + description: 'Manipulate raw IPFS blocks.', + + builder (yargs) { + return yargs + .commandDir('block') + }, + + handler (argv) { + } +} diff --git a/src/cli/commands/block/get.js b/src/cli/commands/block/get.js index 7ca1f84432..4af4acfa4c 100644 --- a/src/cli/commands/block/get.js +++ b/src/cli/commands/block/get.js @@ -1,30 +1,27 @@ 'use strict' -const Command = require('ronin').Command const utils = require('../../utils') const bs58 = require('bs58') const debug = require('debug') const log = debug('cli:block') log.error = debug('cli:block:error') -module.exports = Command.extend({ - desc: 'Get a raw IPFS block', +module.exports = { + command: 'get ', - options: {}, + describe: 'Get a raw IPFS block', - run: (key) => { - if (!key) { - throw new Error("Argument 'key' is required") - } + builder: {}, + handler (argv) { utils.getIPFS((err, ipfs) => { if (err) { throw err } const mh = utils.isDaemonOn() - ? key - : new Buffer(bs58.decode(key)) + ? argv.key + : new Buffer(bs58.decode(argv.key)) ipfs.block.get(mh, (err, block) => { if (err) { @@ -40,4 +37,4 @@ module.exports = Command.extend({ }) }) } -}) +} diff --git a/src/cli/commands/block/put.js b/src/cli/commands/block/put.js index 619e925669..3c006fd3bb 100644 --- a/src/cli/commands/block/put.js +++ b/src/cli/commands/block/put.js @@ -1,6 +1,5 @@ 'use strict' -const Command = require('ronin').Command const utils = require('../../utils') const bs58 = require('bs58') const bl = require('bl') @@ -38,14 +37,16 @@ function addBlock (buf) { }) } -module.exports = Command.extend({ - desc: 'Stores input as an IPFS block', +module.exports = { + command: 'put [data]', - options: {}, + describe: 'Stores input as an IPFS block', - run: (filePath) => { - if (filePath) { - return addBlock(fs.readFileSync(filePath)) + builder: {}, + + handler (argv) { + if (argv.data) { + return addBlock(fs.readFileSync(argv.data)) } process.stdin.pipe(bl((err, input) => { @@ -56,4 +57,4 @@ module.exports = Command.extend({ addBlock(input) })) } -}) +} diff --git a/src/cli/commands/block/rm.js b/src/cli/commands/block/rm.js index 9fea64c4dc..5e2e8783b7 100644 --- a/src/cli/commands/block/rm.js +++ b/src/cli/commands/block/rm.js @@ -1,22 +1,19 @@ 'use strict' -const Command = require('ronin').Command const utils = require('../../utils') const bs58 = require('bs58') const debug = require('debug') const log = debug('cli:block') log.error = debug('cli:block:error') -module.exports = Command.extend({ - desc: 'Remove a raw IPFS block', +module.exports = { + command: 'rm ', - options: {}, + describe: 'Remove a raw IPFS block', - run: (key) => { - if (!key) { - throw new Error("Argument 'key' is required") - } + builder: {}, + handler (argv) { utils.getIPFS((err, ipfs) => { if (err) { throw err @@ -27,15 +24,15 @@ module.exports = Command.extend({ throw new Error('rm block with daemon running is not yet implemented') } - const mh = new Buffer(bs58.decode(key)) + const mh = new Buffer(bs58.decode(argv.key)) ipfs.block.del(mh, (err) => { if (err) { throw err } - console.log('removed', key) + console.log('removed', argv.key) }) }) } -}) +} diff --git a/src/cli/commands/block/stat.js b/src/cli/commands/block/stat.js index a7ad13657a..c1107f77df 100644 --- a/src/cli/commands/block/stat.js +++ b/src/cli/commands/block/stat.js @@ -1,30 +1,27 @@ 'use strict' -const Command = require('ronin').Command const utils = require('../../utils') const bs58 = require('bs58') const debug = require('debug') const log = debug('cli:block') log.error = debug('cli:block:error') -module.exports = Command.extend({ - desc: 'Print information of a raw IPFS block', +module.exports = { + command: 'stat ', - options: {}, + describe: 'Print information of a raw IPFS block', - run: (key) => { - if (!key) { - throw new Error("Argument 'key' is required") - } + builder: {}, + handler (argv) { utils.getIPFS((err, ipfs) => { if (err) { throw err } const mh = utils.isDaemonOn() - ? key - : new Buffer(bs58.decode(key)) + ? argv.key + : new Buffer(bs58.decode(argv.key)) ipfs.block.stat(mh, (err, block) => { if (err) { @@ -41,4 +38,4 @@ module.exports = Command.extend({ }) }) } -}) +} diff --git a/src/cli/commands/bootstrap.js b/src/cli/commands/bootstrap.js new file mode 100644 index 0000000000..0e2ad80d85 --- /dev/null +++ b/src/cli/commands/bootstrap.js @@ -0,0 +1,15 @@ +'use strict' + +module.exports = { + command: 'bootstrap', + + description: 'Show or edit the list of bootstrap peers.', + + builder (yargs) { + return yargs + .commandDir('bootstrap') + }, + + handler (argv) { + } +} diff --git a/src/cli/commands/bootstrap/add.js b/src/cli/commands/bootstrap/add.js index 820a78ced0..0122e53fee 100644 --- a/src/cli/commands/bootstrap/add.js +++ b/src/cli/commands/bootstrap/add.js @@ -1,26 +1,27 @@ 'use strict' -const Command = require('ronin').Command const debug = require('debug') const log = debug('cli:bootstrap') const utils = require('../../utils') log.error = debug('cli:bootstrap:error') -module.exports = Command.extend({ - desc: 'Add peers to the bootstrap list', +module.exports = { + command: 'add ', - options: {}, + describe: 'Add peers to the bootstrap list', - run: (multiaddr) => { + builder: {}, + + handler (argv) { utils.getIPFS((err, ipfs) => { if (err) { throw err } - ipfs.bootstrap.add(multiaddr, (err, list) => { + ipfs.bootstrap.add(argv.peer, (err, list) => { if (err) { throw err } }) }) } -}) +} diff --git a/src/cli/commands/bootstrap/list.js b/src/cli/commands/bootstrap/list.js index a6b1478b49..e060ec4461 100644 --- a/src/cli/commands/bootstrap/list.js +++ b/src/cli/commands/bootstrap/list.js @@ -1,17 +1,18 @@ 'use strict' -const Command = require('ronin').Command const utils = require('../../utils') const debug = require('debug') const log = debug('cli:bootstrap') log.error = debug('cli:bootstrap:error') -module.exports = Command.extend({ - desc: 'Show peers in the bootstrap list', +module.exports = { + command: 'list', - options: {}, + describe: 'Show peers in the bootstrap list', - run: () => { + builder: {}, + + handler () { utils.getIPFS((err, ipfs) => { if (err) { throw err @@ -26,4 +27,4 @@ module.exports = Command.extend({ }) }) } -}) +} diff --git a/src/cli/commands/bootstrap/rm.js b/src/cli/commands/bootstrap/rm.js index 0cf1a93560..03216b6baa 100644 --- a/src/cli/commands/bootstrap/rm.js +++ b/src/cli/commands/bootstrap/rm.js @@ -1,26 +1,27 @@ 'use strict' -const Command = require('ronin').Command const debug = require('debug') const log = debug('cli:bootstrap') log.error = debug('cli:bootstrap:error') const utils = require('../../utils') -module.exports = Command.extend({ - desc: 'Removes peers from the bootstrap list', +module.exports = { + command: 'rm ', - options: {}, + describe: 'Removes peers from the bootstrap list', - run: (multiaddr) => { + builder: {}, + + handler (argv) { utils.getIPFS((err, ipfs) => { if (err) { throw err } - ipfs.bootstrap.rm(multiaddr, (err, list) => { + ipfs.bootstrap.rm(argv.peer, (err, list) => { if (err) { throw err } }) }) } -}) +} diff --git a/src/cli/commands/commands.js b/src/cli/commands/commands.js index 91294b1414..ea60b587df 100644 --- a/src/cli/commands/commands.js +++ b/src/cli/commands/commands.js @@ -1,13 +1,14 @@ 'use strict' -const Command = require('ronin').Command const path = require('path') const glob = require('glob').sync -module.exports = Command.extend({ - desc: 'List all available commands', +module.exports = { + command: 'commands', - run (name) { + describe: 'List all available commands', + + handler () { const basePath = path.resolve(__dirname, '..') // modeled after https://github.com/vdemedes/ronin/blob/master/lib/program.js#L78 @@ -24,4 +25,4 @@ module.exports = Command.extend({ console.log(['ipfs'].concat(cmds).join('\n')) } -}) +} diff --git a/src/cli/commands/config.js b/src/cli/commands/config.js index 68d095f47b..ee0e7d377b 100644 --- a/src/cli/commands/config.js +++ b/src/cli/commands/config.js @@ -1,6 +1,5 @@ 'use strict' -const Command = require('ronin').Command const debug = require('debug') const get = require('lodash.get') const set = require('lodash.set') @@ -8,24 +7,34 @@ const log = debug('cli:config') log.error = debug('cli:config:error') const utils = require('../utils') -module.exports = Command.extend({ - desc: 'Get and set IPFS config values', - - options: { - bool: { - type: 'boolean', - default: false - }, - json: { - type: 'boolean', - default: false - } +module.exports = { + command: 'config [value]', + + description: 'Get and set IPFS config values', + + builder (yargs) { + return yargs + .commandDir('config') + .options({ + bool: { + type: 'boolean', + default: false + }, + json: { + type: 'boolean', + default: false + } + }) }, - run: (bool, json, key, value) => { - if (!key) { - throw new Error("argument 'key' is required") - } + handler (argv) { + if (argv._handled) return + argv._handled = true + + const bool = argv.bool + const json = argv.json + const key = argv.key + let value = argv.value utils.getIPFS((err, ipfs) => { if (err) { @@ -42,7 +51,7 @@ module.exports = Command.extend({ throw new Error('failed to read the config') } - console.log(config.Value) + console.log(config) }) } @@ -95,4 +104,4 @@ module.exports = Command.extend({ } }) } -}) +} diff --git a/src/cli/commands/config/edit.js b/src/cli/commands/config/edit.js index dfe9061199..90502dc059 100644 --- a/src/cli/commands/config/edit.js +++ b/src/cli/commands/config/edit.js @@ -1,6 +1,5 @@ 'use strict' -const Command = require('ronin').Command const spawn = require('child_process').spawn const fs = require('fs') const temp = require('temp') @@ -10,10 +9,17 @@ const log = debug('cli:config') log.error = debug('cli:config:error') const utils = require('../../utils') -module.exports = Command.extend({ - desc: 'Opens the config file for editing in $EDITOR', +module.exports = { + command: 'edit', + + describe: 'Opens the config file for editing in $EDITOR', + + builder: {}, + + handler (argv) { + if (argv._handled) return + argv._handled = true - run: (name) => { utils.getIPFS((err, ipfs) => { if (err) { throw err @@ -111,4 +117,4 @@ module.exports = Command.extend({ }) }) } -}) +} diff --git a/src/cli/commands/config/replace.js b/src/cli/commands/config/replace.js index 4b156568bb..fd1a075c97 100644 --- a/src/cli/commands/config/replace.js +++ b/src/cli/commands/config/replace.js @@ -1,6 +1,5 @@ 'use strict' -const Command = require('ronin').Command const debug = require('debug') const path = require('path') const log = debug('cli:config') @@ -8,18 +7,23 @@ log.error = debug('cli:config:error') const utils = require('../../utils') const fs = require('fs') -module.exports = Command.extend({ - desc: 'Replaces the config with ', +module.exports = { + command: 'replace ', - options: {}, + describe: 'Replaces the config with ', + + builder: {}, + + handler (argv) { + if (argv._handled) return + argv._handled = true - run: (configPath) => { utils.getIPFS((err, ipfs) => { if (err) { throw err } - const filePath = path.resolve(process.cwd(), configPath) + const filePath = path.resolve(process.cwd(), argv.file) const config = utils.isDaemonOn() ? filePath : JSON.parse(fs.readFileSync(filePath, 'utf8')) @@ -31,4 +35,4 @@ module.exports = Command.extend({ }) }) } -}) +} diff --git a/src/cli/commands/config/show.js b/src/cli/commands/config/show.js index 5a837bee6f..225a06b34e 100644 --- a/src/cli/commands/config/show.js +++ b/src/cli/commands/config/show.js @@ -1,17 +1,21 @@ 'use strict' -const Command = require('ronin').Command const debug = require('debug') const log = debug('cli:config') log.error = debug('cli:config:error') const utils = require('../../utils') -module.exports = Command.extend({ - desc: 'Outputs the content of the config file', +module.exports = { + command: 'show', - options: {}, + describe: 'Outputs the content of the config file', + + builder: {}, + + handler (argv) { + if (argv._handled) return + argv._handled = true - run: () => { utils.getIPFS((err, ipfs) => { if (err) { throw err @@ -25,4 +29,4 @@ module.exports = Command.extend({ }) }) } -}) +} diff --git a/src/cli/commands/daemon.js b/src/cli/commands/daemon.js index 6beddb19b2..acbbc5dc00 100644 --- a/src/cli/commands/daemon.js +++ b/src/cli/commands/daemon.js @@ -1,6 +1,5 @@ 'use strict' -const Command = require('ronin').Command const HttpAPI = require('../../http-api') const debug = require('debug') const log = debug('cli:daemon') @@ -8,10 +7,12 @@ log.error = debug('cli:daemon:error') let httpAPI -module.exports = Command.extend({ - desc: 'Start a long-running daemon process', +module.exports = { + command: 'daemon', - run: (name) => { + describe: 'Start a long-running daemon process', + + handler () { console.log('Initializing daemon...') httpAPI = new HttpAPI(process.env.IPFS_PATH) httpAPI.start((err) => { @@ -31,4 +32,4 @@ module.exports = Command.extend({ }) }) } -}) +} diff --git a/src/cli/commands/dns.js b/src/cli/commands/dns.js index eaa6654b45..fde8059af3 100644 --- a/src/cli/commands/dns.js +++ b/src/cli/commands/dns.js @@ -1,10 +1,10 @@ 'use strict' -const Command = require('ronin').Command +module.exports = { + command: 'dns', -module.exports = Command.extend({ - desc: '', + describe: '', - run: () => { + handler (argv) { } -}) +} diff --git a/src/cli/commands/files.js b/src/cli/commands/files.js new file mode 100644 index 0000000000..0b63bdefb6 --- /dev/null +++ b/src/cli/commands/files.js @@ -0,0 +1,15 @@ +'use strict' + +module.exports = { + command: 'files', + + description: '', + + builder (yargs) { + return yargs + .commandDir('files') + }, + + handler (argv) { + } +} diff --git a/src/cli/commands/files/add.js b/src/cli/commands/files/add.js index 5cae372ccd..5caa5d85a4 100644 --- a/src/cli/commands/files/add.js +++ b/src/cli/commands/files/add.js @@ -1,6 +1,5 @@ 'use strict' -const Command = require('ronin').Command const utils = require('../../utils') const debug = require('debug') const log = debug('cli:version') @@ -35,10 +34,12 @@ function checkPath (inPath, recursive) { return inPath } -module.exports = Command.extend({ - desc: 'Add a file to IPFS using the UnixFS data format', +module.exports = { + command: 'add ', - options: { + describe: 'Add a file to IPFS using the UnixFS data format', + + builder: { recursive: { alias: 'r', type: 'boolean', @@ -46,10 +47,10 @@ module.exports = Command.extend({ } }, - run: (recursive, inPath) => { + handler (argv) { let rs - inPath = checkPath(inPath, recursive) + let inPath = checkPath(argv.file, argv.recursive) glob(path.join(inPath, '/**/*'), (err, res) => { if (err) { @@ -95,4 +96,4 @@ module.exports = Command.extend({ }) }) } -}) +} diff --git a/src/cli/commands/files/cat.js b/src/cli/commands/files/cat.js index ff87a82a69..7129aeed66 100644 --- a/src/cli/commands/files/cat.js +++ b/src/cli/commands/files/cat.js @@ -1,23 +1,19 @@ 'use strict' -const Command = require('ronin').Command const debug = require('debug') const utils = require('../../utils') const log = debug('cli:files') log.error = debug('cli:files:error') -module.exports = Command.extend({ - desc: 'Download IPFS objects', +module.exports = { + command: 'cat ', - options: {}, + describe: 'Download IPFS objects', - run: (path, options) => { - if (!path) { - throw new Error("Argument 'path' is required") - } - if (!options) { - options = {} - } + builder: {}, + + handler (argv) { + const path = argv.ipfsPath utils.getIPFS((err, ipfs) => { if (err) { throw err @@ -39,4 +35,4 @@ module.exports = Command.extend({ }) }) } -}) +} diff --git a/src/cli/commands/files/get.js b/src/cli/commands/files/get.js index e85542dbe2..b8d746c03f 100644 --- a/src/cli/commands/files/get.js +++ b/src/cli/commands/files/get.js @@ -1,6 +1,5 @@ 'use strict' -const Command = require('ronin').Command const debug = require('debug') const utils = require('../../utils') const log = debug('cli:files') @@ -10,14 +9,7 @@ const path = require('path') const pathExists = require('path-exists') function checkArgs (hash, outPath) { - if (!hash) { - throw new Error("Argument 'path' is required") - } // format the output directory - if (!outPath) { - return process.cwd() - } - if (!outPath.endsWith('/')) { outPath += '/' } @@ -70,17 +62,27 @@ function fileHandler (result, dir) { } } -module.exports = Command.extend({ - desc: 'Download IPFS objects', +module.exports = { + command: 'get ', - run: (hash, outPath) => { - const dir = checkArgs(hash, outPath) + describe: 'Download IPFS objects', + + builder: { + output: { + alias: 'o', + type: 'string', + default: process.cwd() + } + }, + + handler (argv) { + const dir = checkArgs(argv.ipfsPath, argv.output) utils.getIPFS((err, ipfs) => { if (err) { throw err } - ipfs.files.get(hash, (err, result) => { + ipfs.files.get(argv.ipfsPath, (err, result) => { if (err) { throw err } @@ -88,4 +90,4 @@ module.exports = Command.extend({ }) }) } -}) +} diff --git a/src/cli/commands/id.js b/src/cli/commands/id.js index 775b4673f4..0ccd56af00 100644 --- a/src/cli/commands/id.js +++ b/src/cli/commands/id.js @@ -1,33 +1,35 @@ 'use strict' -const Command = require('ronin').Command const debug = require('debug') const utils = require('../utils') const log = debug('cli') log.error = debug('cli:error') -module.exports = Command.extend({ - desc: 'Shows IPFS Node ID info', +module.exports = { + command: 'id', - options: { + describe: 'Shows IPFS Node ID info', + + builder: { format: { alias: 'f', type: 'string' } }, - run: (name) => { + handler (argv) { + // TODO: handle argv.format utils.getIPFS((err, ipfs) => { if (err) { throw err } - ipfs.id((err, id) => { if (err) { throw err } + console.log(JSON.stringify(id, '', 2)) }) }) } -}) +} diff --git a/src/cli/commands/init.js b/src/cli/commands/init.js index 5572bfb623..24313cc217 100644 --- a/src/cli/commands/init.js +++ b/src/cli/commands/init.js @@ -1,50 +1,52 @@ 'use strict' -const Command = require('ronin').Command const IpfsRepo = require('ipfs-repo') const Ipfs = require('../../core') const fsBlobStore = require('fs-blob-store') const utils = require('../utils') -module.exports = Command.extend({ - desc: 'Initialize a local IPFS node', +module.exports = { + command: 'init', - options: { + describe: 'Initialize a local IPFS node', + + builder: { bits: { type: 'number', alias: 'b', default: '2048', - desc: 'Number of bits to use in the generated RSA private key (defaults to 2048)' + describe: 'Number of bits to use in the generated RSA private key (defaults to 2048)' }, force: { alias: 'f', type: 'boolean', - desc: 'Overwrite existing config (if it exists)' + describe: 'Overwrite existing config (if it exists)' }, - 'empty-repo': { + emptyRepo: { alias: 'e', type: 'boolean', - desc: "Don't add and pin help files to the local storage" + describe: "Don't add and pin help files to the local storage" } }, - run: (bits, force, empty) => { + handler (argv) { const path = utils.getRepoPath() const repo = new IpfsRepo(path, { stores: fsBlobStore }) - var ipfs = new Ipfs(repo) + const ipfs = new Ipfs(repo) + ipfs.init({ - bits: bits, - force: force, - emptyRepo: empty - }, function (err, res) { + bits: argv.bits, + force: argv.force, + emptyRepo: argv.emptyRepo + }, function (err) { if (err) { console.error(err.toString()) process.exit(1) } }) } -}) +} diff --git a/src/cli/commands/ls.js b/src/cli/commands/ls.js index a46afd1faa..d1210bdd28 100644 --- a/src/cli/commands/ls.js +++ b/src/cli/commands/ls.js @@ -1,9 +1,10 @@ 'use strict' -const Command = require('ronin').Command +module.exports = { + command: 'ls', -module.exports = Command.extend({ - desc: '', + describe: '', - run: () => {} -}) + handler (argv) { + } +} diff --git a/src/cli/commands/mount.js b/src/cli/commands/mount.js index af4020b384..7825753efb 100644 --- a/src/cli/commands/mount.js +++ b/src/cli/commands/mount.js @@ -1,10 +1,10 @@ 'use strict' -const Command = require('ronin').Command +module.exports = { + command: 'mount', -module.exports = Command.extend({ - desc: '', + describe: '', - run: (name) => { + handler (argv) { } -}) +} diff --git a/src/cli/commands/object.js b/src/cli/commands/object.js new file mode 100644 index 0000000000..9f29944eff --- /dev/null +++ b/src/cli/commands/object.js @@ -0,0 +1,15 @@ +'use strict' + +module.exports = { + command: 'object', + + description: 'Interact with ipfs objects.', + + builder (yargs) { + return yargs + .commandDir('object') + }, + + handler (argv) { + } +} diff --git a/src/cli/commands/object/data.js b/src/cli/commands/object/data.js index 173608bf05..7dfb371568 100644 --- a/src/cli/commands/object/data.js +++ b/src/cli/commands/object/data.js @@ -1,27 +1,24 @@ 'use strict' -const Command = require('ronin').Command const utils = require('../../utils') const debug = require('debug') const log = debug('cli:object') log.error = debug('cli:object:error') -module.exports = Command.extend({ - desc: 'Outputs the raw bytes in an IPFS object', +module.exports = { + command: 'data ', - options: {}, + describe: 'Outputs the raw bytes in an IPFS object', - run: (key) => { - if (!key) { - throw new Error("Argument 'key' is required") - } + builder: {}, + handler (argv) { utils.getIPFS((err, ipfs) => { if (err) { throw err } - ipfs.object.data(key, {enc: 'base58'}, (err, data) => { + ipfs.object.data(argv.key, {enc: 'base58'}, (err, data) => { if (err) { throw err } @@ -30,4 +27,4 @@ module.exports = Command.extend({ }) }) } -}) +} diff --git a/src/cli/commands/object/get.js b/src/cli/commands/object/get.js index afd08948b7..59796e9821 100644 --- a/src/cli/commands/object/get.js +++ b/src/cli/commands/object/get.js @@ -1,27 +1,24 @@ 'use strict' -const Command = require('ronin').Command const utils = require('../../utils') const debug = require('debug') const log = debug('cli:object') log.error = debug('cli:object:error') -module.exports = Command.extend({ - desc: 'Get and serialize the DAG node named by ', +module.exports = { + command: 'get ', - options: {}, + describe: 'Get and serialize the DAG node named by ', - run: (key) => { - if (!key) { - throw new Error("Argument 'key' is required") - } + builder: {}, + handler (argv) { utils.getIPFS((err, ipfs) => { if (err) { throw err } - ipfs.object.get(key, {enc: 'base58'}, (err, node) => { + ipfs.object.get(argv.key, {enc: 'base58'}, (err, node) => { if (err) { throw err } @@ -32,4 +29,4 @@ module.exports = Command.extend({ }) }) } -}) +} diff --git a/src/cli/commands/object/links.js b/src/cli/commands/object/links.js index b2d0959013..707a0cd44d 100644 --- a/src/cli/commands/object/links.js +++ b/src/cli/commands/object/links.js @@ -1,27 +1,24 @@ 'use strict' -const Command = require('ronin').Command const utils = require('../../utils') const debug = require('debug') const log = debug('cli:object') log.error = debug('cli:object:error') -module.exports = Command.extend({ - desc: 'Outputs the links pointed to by the specified object', +module.exports = { + command: 'links ', - options: {}, + describe: 'Outputs the links pointed to by the specified object', - run: (key) => { - if (!key) { - throw new Error("Argument 'key' is required") - } + builder: {}, + handler (argv) { utils.getIPFS((err, ipfs) => { if (err) { throw err } - ipfs.object.links(key, {enc: 'base58'}, (err, links) => { + ipfs.object.links(argv.key, {enc: 'base58'}, (err, links) => { if (err) { throw err } @@ -33,4 +30,4 @@ module.exports = Command.extend({ }) }) } -}) +} diff --git a/src/cli/commands/object/new.js b/src/cli/commands/object/new.js index e7b0f4b2b2..4cb570497a 100644 --- a/src/cli/commands/object/new.js +++ b/src/cli/commands/object/new.js @@ -1,17 +1,18 @@ 'use strict' -const Command = require('ronin').Command const utils = require('../../utils') const debug = require('debug') const log = debug('cli:object') log.error = debug('cli:object:error') -module.exports = Command.extend({ - desc: 'Create new ipfs objects', +module.exports = { + command: 'new', - options: {}, + describe: 'Create new ipfs objects', - run: () => { + builder: {}, + + handler (argv) { utils.getIPFS((err, ipfs) => { if (err) { throw err @@ -26,4 +27,4 @@ module.exports = Command.extend({ }) }) } -}) +} diff --git a/src/cli/commands/object/patch.js b/src/cli/commands/object/patch.js new file mode 100644 index 0000000000..348947cf48 --- /dev/null +++ b/src/cli/commands/object/patch.js @@ -0,0 +1,15 @@ +'use strict' + +module.exports = { + command: 'patch', + + description: 'Create a new merkledag object based on an existing one.', + + builder (yargs) { + return yargs + .commandDir('patch') + }, + + handler (argv) { + } +} diff --git a/src/cli/commands/object/patch/add-link.js b/src/cli/commands/object/patch/add-link.js index e5837c615f..8ec24eebb8 100644 --- a/src/cli/commands/object/patch/add-link.js +++ b/src/cli/commands/object/patch/add-link.js @@ -1,6 +1,5 @@ 'use strict' -const Command = require('ronin').Command const utils = require('../../../utils') const debug = require('debug') const log = debug('cli:object') @@ -8,34 +7,26 @@ const mDAG = require('ipfs-merkle-dag') const DAGLink = mDAG.DAGLink log.error = debug('cli:object:error') -module.exports = Command.extend({ - desc: 'Add a link to a given object', +module.exports = { + command: 'add-link ', - options: {}, + describe: 'Add a link to a given object', - run: (root, name, ref) => { - if (!root) { - throw new Error("Argument 'root' is required") - } - if (!name) { - throw new Error("Argument 'name' is required") - } - if (!ref) { - throw new Error("Argument 'ref' is required") - } + builder: {}, + handler (argv) { utils.getIPFS((err, ipfs) => { if (err) { throw err } - ipfs.object.get(ref, {enc: 'base58'}).then((linkedObj) => { + ipfs.object.get(argv.ref, {enc: 'base58'}).then((linkedObj) => { const link = new DAGLink( - name, + argv.name, linkedObj.size(), linkedObj.multihash() ) - return ipfs.object.patch.addLink(root, link, {enc: 'base58'}) + return ipfs.object.patch.addLink(argv.root, link, {enc: 'base58'}) }).then((node) => { console.log(node.toJSON().Hash) }).catch((err) => { @@ -43,4 +34,4 @@ module.exports = Command.extend({ }) }) } -}) +} diff --git a/src/cli/commands/object/patch/append-data.js b/src/cli/commands/object/patch/append-data.js index dd420943b7..4aafbfbb16 100644 --- a/src/cli/commands/object/patch/append-data.js +++ b/src/cli/commands/object/patch/append-data.js @@ -1,6 +1,5 @@ 'use strict' -const Command = require('ronin').Command const utils = require('../../../utils') const bl = require('bl') const fs = require('fs') @@ -24,18 +23,16 @@ function appendData (key, data) { }) } -module.exports = Command.extend({ - desc: 'Append data to the data segment of a dag node', +module.exports = { + command: 'append-data [data]', - options: {}, + describe: 'Append data to the data segment of a dag node', - run: (key, filePath) => { - if (!key) { - throw new Error("Argument 'root' is required") - } + builder: {}, - if (filePath) { - return appendData(key, fs.readFileSync(filePath)) + handler (argv) { + if (argv.data) { + return appendData(argv.root, fs.readFileSync(argv.data)) } process.stdin.pipe(bl((err, input) => { @@ -43,7 +40,7 @@ module.exports = Command.extend({ throw err } - appendData(key, input) + appendData(argv.root, input) })) } -}) +} diff --git a/src/cli/commands/object/patch/rm-link.js b/src/cli/commands/object/patch/rm-link.js index 430ebb3fda..9aa22645cc 100644 --- a/src/cli/commands/object/patch/rm-link.js +++ b/src/cli/commands/object/patch/rm-link.js @@ -1,33 +1,27 @@ 'use strict' -const Command = require('ronin').Command const DAGLink = require('ipfs-merkle-dag').DAGLink const utils = require('../../../utils') const debug = require('debug') const log = debug('cli:object') log.error = debug('cli:object:error') -module.exports = Command.extend({ - desc: 'Remove a link from an object', +module.exports = { + command: 'rm-link ', - options: {}, + describe: 'Remove a link from an object', - run: (root, link) => { - if (!root) { - throw new Error("Argument 'root' is required") - } - if (!link) { - throw new Error("Argument 'link' is required") - } + builder: {}, + handler (argv) { utils.getIPFS((err, ipfs) => { if (err) { throw err } - const dLink = new DAGLink(link) + const dLink = new DAGLink(argv.link) - ipfs.object.patch.rmLink(root, dLink, {enc: 'base58'}, (err, node) => { + ipfs.object.patch.rmLink(argv.root, dLink, {enc: 'base58'}, (err, node) => { if (err) { throw err } @@ -36,4 +30,4 @@ module.exports = Command.extend({ }) }) } -}) +} diff --git a/src/cli/commands/object/patch/set-data.js b/src/cli/commands/object/patch/set-data.js index abbe115cf5..d24791c8eb 100644 --- a/src/cli/commands/object/patch/set-data.js +++ b/src/cli/commands/object/patch/set-data.js @@ -1,6 +1,5 @@ 'use strict' -const Command = require('ronin').Command const utils = require('../../../utils') const bl = require('bl') const fs = require('fs') @@ -24,18 +23,16 @@ function parseAndAddNode (key, data) { }) } -module.exports = Command.extend({ - desc: 'Set data field of an ipfs object', +module.exports = { + command: 'set-data [data]', - options: {}, + describe: 'Set data field of an ipfs object', - run: (key, filePath) => { - if (!key) { - throw new Error("Argument 'root' is required") - } + builder: {}, - if (filePath) { - return parseAndAddNode(key, fs.readFileSync(filePath)) + handler (argv) { + if (argv.data) { + return parseAndAddNode(argv.root, fs.readFileSync(argv.data)) } process.stdin.pipe(bl((err, input) => { @@ -43,7 +40,7 @@ module.exports = Command.extend({ throw err } - parseAndAddNode(key, input) + parseAndAddNode(argv.root, input) })) } -}) +} diff --git a/src/cli/commands/object/put.js b/src/cli/commands/object/put.js index 8b8054a2f5..5ce124f261 100644 --- a/src/cli/commands/object/put.js +++ b/src/cli/commands/object/put.js @@ -1,6 +1,5 @@ 'use strict' -const Command = require('ronin').Command const utils = require('../../utils') const bl = require('bl') const fs = require('fs') @@ -24,19 +23,21 @@ function putNode (buf, enc) { }) } -module.exports = Command.extend({ - desc: 'Stores input as a DAG object, outputs its key', +module.exports = { + command: 'put [data]', - options: { + describe: 'Stores input as a DAG object, outputs its key', + + builder: { inputenc: { type: 'string', default: 'json' } }, - run: (inputenc, filePath) => { - if (filePath) { - return putNode(fs.readFileSync(filePath), inputenc) + handler (argv) { + if (argv.data) { + return putNode(fs.readFileSync(argv.data), argv.inputenc) } process.stdin.pipe(bl((err, input) => { @@ -44,7 +45,7 @@ module.exports = Command.extend({ throw err } - putNode(input, inputenc) + putNode(input, argv.inputenc) })) } -}) +} diff --git a/src/cli/commands/object/stat.js b/src/cli/commands/object/stat.js index a819ae0e2b..ac1da4382a 100644 --- a/src/cli/commands/object/stat.js +++ b/src/cli/commands/object/stat.js @@ -1,27 +1,24 @@ 'use strict' -const Command = require('ronin').Command const utils = require('../../utils') const debug = require('debug') const log = debug('cli:object') log.error = debug('cli:object:error') -module.exports = Command.extend({ - desc: 'Get stats for the DAG node named by ', +module.exports = { + command: 'stat ', - options: {}, + describe: 'Get stats for the DAG node named by ', - run: (key) => { - if (!key) { - throw new Error("Argument 'key' is required") - } + builder: {}, + handler (argv) { utils.getIPFS((err, ipfs) => { if (err) { throw err } - ipfs.object.stat(key, {enc: 'base58'}, (err, stats) => { + ipfs.object.stat(argv.key, {enc: 'base58'}, (err, stats) => { if (err) { throw err } @@ -34,4 +31,4 @@ module.exports = Command.extend({ }) }) } -}) +} diff --git a/src/cli/commands/ping.js b/src/cli/commands/ping.js index a46afd1faa..d6a040231c 100644 --- a/src/cli/commands/ping.js +++ b/src/cli/commands/ping.js @@ -1,9 +1,10 @@ 'use strict' -const Command = require('ronin').Command +module.exports = { + command: 'ping', -module.exports = Command.extend({ - desc: '', + describe: '', - run: () => {} -}) + handler (argv) { + } +} diff --git a/src/cli/commands/refs.js b/src/cli/commands/refs.js index af4020b384..3d4f4ef4ab 100644 --- a/src/cli/commands/refs.js +++ b/src/cli/commands/refs.js @@ -1,10 +1,10 @@ 'use strict' -const Command = require('ronin').Command +module.exports = { + command: 'refs', -module.exports = Command.extend({ - desc: '', + describe: '', - run: (name) => { + handler (argv) { } -}) +} diff --git a/src/cli/commands/repo.js b/src/cli/commands/repo.js new file mode 100644 index 0000000000..e67ebfa3e7 --- /dev/null +++ b/src/cli/commands/repo.js @@ -0,0 +1,15 @@ +'use strict' + +module.exports = { + command: 'repo', + + description: 'Manipulate the IPFS repo.', + + builder (yargs) { + return yargs + .commandDir('repo') + }, + + handler (argv) { + } +} diff --git a/src/cli/commands/repo/gc.js b/src/cli/commands/repo/gc.js index 1d7948d62d..6b6f705cf4 100644 --- a/src/cli/commands/repo/gc.js +++ b/src/cli/commands/repo/gc.js @@ -1,11 +1,12 @@ 'use strict' -const Command = require('ronin').Command +module.exports = { + command: 'gc', -module.exports = Command.extend({ - desc: '', + describe: '', - options: {}, + builder: {}, - run: function () {} -}) + handler (argv) { + } +} diff --git a/src/cli/commands/repo/init.js b/src/cli/commands/repo/init.js index 1d7948d62d..a4e7e406da 100644 --- a/src/cli/commands/repo/init.js +++ b/src/cli/commands/repo/init.js @@ -1,11 +1,12 @@ 'use strict' -const Command = require('ronin').Command +module.exports = { + command: 'init', -module.exports = Command.extend({ - desc: '', + describe: '', - options: {}, + builder: {}, - run: function () {} -}) + handler (argv) { + } +} diff --git a/src/cli/commands/repo/version.js b/src/cli/commands/repo/version.js index 7885b99b40..d1c6a57c96 100644 --- a/src/cli/commands/repo/version.js +++ b/src/cli/commands/repo/version.js @@ -1,14 +1,15 @@ 'use strict' -const Command = require('ronin').Command const utils = require('../../utils') -module.exports = Command.extend({ - desc: 'Shows IPFS repo version information', +module.exports = { + command: 'version', - options: {}, + describe: 'Shows IPFS repo version information', - run: function () { + builder: {}, + + handler (argv) { utils.getIPFS((err, ipfs) => { if (err) { throw err @@ -21,4 +22,4 @@ module.exports = Command.extend({ }) }) } -}) +} diff --git a/src/cli/commands/resolve.js b/src/cli/commands/resolve.js index a46afd1faa..d57a017114 100644 --- a/src/cli/commands/resolve.js +++ b/src/cli/commands/resolve.js @@ -1,9 +1,10 @@ 'use strict' -const Command = require('ronin').Command +module.exports = { + command: 'resolve', -module.exports = Command.extend({ - desc: '', + describe: '', - run: () => {} -}) + handler (argv) { + } +} diff --git a/src/cli/commands/swarm.js b/src/cli/commands/swarm.js new file mode 100644 index 0000000000..dc7cd291d1 --- /dev/null +++ b/src/cli/commands/swarm.js @@ -0,0 +1,15 @@ +'use strict' + +module.exports = { + command: 'swarm', + + description: 'Swarm inspection tool.', + + builder (yargs) { + return yargs + .commandDir('swarm') + }, + + handler (argv) { + } +} diff --git a/src/cli/commands/swarm/addrs.js b/src/cli/commands/swarm/addrs.js index aa6802e69f..2a9630c321 100644 --- a/src/cli/commands/swarm/addrs.js +++ b/src/cli/commands/swarm/addrs.js @@ -1,17 +1,21 @@ 'use strict' -const Command = require('ronin').Command const utils = require('../../utils') const debug = require('debug') const log = debug('cli:object') log.error = debug('cli:object:error') -module.exports = Command.extend({ - desc: '', +module.exports = { + command: 'addrs', - options: {}, + describe: '', - run: () => { + builder (yargs) { + return yargs + .commandDir('addrs') + }, + + handler (argv) { utils.getIPFS((err, ipfs) => { if (err) { throw err @@ -19,4 +23,4 @@ module.exports = Command.extend({ // TODO }) } -}) +} diff --git a/src/cli/commands/swarm/addrs/local.js b/src/cli/commands/swarm/addrs/local.js index 2ba6c510a1..92a35a2524 100644 --- a/src/cli/commands/swarm/addrs/local.js +++ b/src/cli/commands/swarm/addrs/local.js @@ -1,17 +1,18 @@ 'use strict' -const Command = require('ronin').Command const utils = require('../../../utils') const debug = require('debug') const log = debug('cli:object') log.error = debug('cli:object:error') -module.exports = Command.extend({ - desc: 'List local addresses', +module.exports = { + command: 'local', - options: {}, + describe: 'List local addresses', - run: () => { + builder: {}, + + handler (argv) { utils.getIPFS((err, ipfs) => { if (err) { throw err @@ -32,4 +33,4 @@ module.exports = Command.extend({ }) }) } -}) +} diff --git a/src/cli/commands/swarm/connect.js b/src/cli/commands/swarm/connect.js index dfa4a0932f..234bb2b69d 100644 --- a/src/cli/commands/swarm/connect.js +++ b/src/cli/commands/swarm/connect.js @@ -1,19 +1,20 @@ 'use strict' -const Command = require('ronin').Command const utils = require('../../utils') const debug = require('debug') const log = debug('cli:swarm') log.error = debug('cli:swarm:error') -module.exports = Command.extend({ - desc: 'Open connection to a given address', +module.exports = { + command: 'connect
', - options: {}, + describe: 'Open connection to a given address', - run: (address) => { - if (!address) { - throw new Error("Argument 'address' is required") + builder: {}, + + handler (argv) { + if (!utils.isDaemonOn()) { + throw new Error('This command must be run in online mode. Try running \'ipfs daemon\' first.') } utils.getIPFS((err, ipfs) => { @@ -21,11 +22,7 @@ module.exports = Command.extend({ throw err } - if (!utils.isDaemonOn()) { - throw new Error('This command must be run in online mode. Try running \'ipfs daemon\' first.') - } - - ipfs.swarm.connect(address, (err, res) => { + ipfs.swarm.connect(argv.address, (err, res) => { if (err) { throw err } @@ -34,4 +31,4 @@ module.exports = Command.extend({ }) }) } -}) +} diff --git a/src/cli/commands/swarm/disconnect.js b/src/cli/commands/swarm/disconnect.js index 6ce8687868..9cc88e958f 100644 --- a/src/cli/commands/swarm/disconnect.js +++ b/src/cli/commands/swarm/disconnect.js @@ -1,18 +1,19 @@ 'use strict' -const Command = require('ronin').Command const utils = require('../../utils') // const bs58 = require('bs58') const debug = require('debug') const log = debug('cli:object') log.error = debug('cli:object:error') -module.exports = Command.extend({ - desc: '', +module.exports = { + command: 'disconnect
', - options: {}, + describe: '', - run: () => { + builder: {}, + + handler (argv) { utils.getIPFS((err, ipfs) => { if (err) { throw err @@ -20,4 +21,4 @@ module.exports = Command.extend({ // TODO }) } -}) +} diff --git a/src/cli/commands/swarm/peers.js b/src/cli/commands/swarm/peers.js index 3e4c890ee7..e26db3e106 100644 --- a/src/cli/commands/swarm/peers.js +++ b/src/cli/commands/swarm/peers.js @@ -1,17 +1,18 @@ 'use strict' -const Command = require('ronin').Command const utils = require('../../utils') const debug = require('debug') const log = debug('cli:object') log.error = debug('cli:object:error') -module.exports = Command.extend({ - desc: 'List peers with open connections', +module.exports = { + command: 'peers', - options: {}, + describe: 'List peers with open connections', - run: () => { + builder: {}, + + handler (argv) { utils.getIPFS((err, ipfs) => { if (err) { throw err @@ -32,4 +33,4 @@ module.exports = Command.extend({ }) }) } -}) +} diff --git a/src/cli/commands/update.js b/src/cli/commands/update.js index af4020b384..56923c5699 100644 --- a/src/cli/commands/update.js +++ b/src/cli/commands/update.js @@ -1,10 +1,10 @@ 'use strict' -const Command = require('ronin').Command +module.exports = { + command: 'update', -module.exports = Command.extend({ - desc: '', + describe: '', - run: (name) => { + handler (argv) { } -}) +} diff --git a/src/cli/commands/version.js b/src/cli/commands/version.js index 1f55de77b2..39ab268348 100644 --- a/src/cli/commands/version.js +++ b/src/cli/commands/version.js @@ -1,15 +1,16 @@ 'use strict' -const Command = require('ronin').Command const utils = require('../utils') const debug = require('debug') const log = debug('cli:version') log.error = debug('cli:version:error') -module.exports = Command.extend({ - desc: 'Shows IPFS version information', +module.exports = { + command: 'version', - options: { + describe: 'Shows IPFS version information', + + builder: { number: { alias: 'n', type: 'boolean', @@ -25,7 +26,8 @@ module.exports = Command.extend({ } }, - run: () => { + handler (argv) { + // TODO: handle argv.{repo|commit|number} utils.getIPFS((err, ipfs) => { if (err) { throw err @@ -44,4 +46,4 @@ module.exports = Command.extend({ }) }) } -}) +} diff --git a/test/cli/test-commands.js b/test/cli/test-commands.js index 7234f283a3..ca3492e00a 100644 --- a/test/cli/test-commands.js +++ b/test/cli/test-commands.js @@ -10,7 +10,7 @@ describe('commands', () => { .run((err, stdout, exitcode) => { expect(err).to.not.exist expect(exitcode).to.equal(0) - expect(stdout.length).to.equal(48) + expect(stdout.length).to.equal(56) done() }) }) diff --git a/test/cli/test-config.js b/test/cli/test-config.js index bad738937b..dbf84abfc6 100644 --- a/test/cli/test-config.js +++ b/test/cli/test-config.js @@ -74,8 +74,6 @@ describe('config', () => { it('set a config key with invalid json', (done) => { nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', '{"bar: 0}', '--json'], {env}) .run((err, stdout, exitcode) => { - const expected = 'error\tinvalid JSON provided' - expect(stdout[0]).to.equal(expected) expect(err).to.not.exist expect(exitcode).to.equal(1) done() @@ -85,8 +83,6 @@ describe('config', () => { it('call config with no arguments', (done) => { nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config'], {env}) .run((err, stdout, exitcode) => { - const expected = "error\targument 'key' is required" - expect(stdout[0]).to.equal(expected) expect(err).to.not.exist expect(exitcode).to.equal(1) done() @@ -190,8 +186,6 @@ describe('config', () => { it('set a config key with invalid json', (done) => { nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', '{"bar: 0}', '--json'], {env}) .run((err, stdout, exitcode) => { - const expected = 'error\tinvalid JSON provided' - expect(stdout[0]).to.equal(expected) expect(err).to.not.exist expect(exitcode).to.equal(1) done() @@ -201,8 +195,6 @@ describe('config', () => { it('call config with no arguments', (done) => { nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config'], {env}) .run((err, stdout, exitcode) => { - const expected = "error\targument 'key' is required" - expect(stdout[0]).to.equal(expected) expect(err).to.not.exist expect(exitcode).to.equal(1) done() diff --git a/test/cli/test-id.js b/test/cli/test-id.js index 801d69bacd..d41596649b 100644 --- a/test/cli/test-id.js +++ b/test/cli/test-id.js @@ -58,8 +58,6 @@ describe('id', () => { it('get the id', (done) => { nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'id'], {env}) .run((err, stdout, exitcode) => { - console.log('=> \n', stdout) - expect( stdout ).to.be.eql([ From 6377ab2b7a20bc3c28ccdf351cb125d5b602598c Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Wed, 3 Aug 2016 12:38:19 +0200 Subject: [PATCH 2/5] fix(version): return actual js-ipfs version Closes #377 --- src/cli/commands/version.js | 2 +- src/core/ipfs/version.js | 18 +++++------------- test/cli/test-version.js | 4 ++-- test/core/both/test-version.js | 2 +- test/http-api/test-version.js | 2 +- 5 files changed, 10 insertions(+), 18 deletions(-) diff --git a/src/cli/commands/version.js b/src/cli/commands/version.js index 39ab268348..cc1b52f99b 100644 --- a/src/cli/commands/version.js +++ b/src/cli/commands/version.js @@ -42,7 +42,7 @@ module.exports = { return } - console.log('ipfs version', version) + console.log(`js-ipfs version: ${version}`) }) }) } diff --git a/src/core/ipfs/version.js b/src/core/ipfs/version.js index 4d8340c831..ec48bd504b 100644 --- a/src/core/ipfs/version.js +++ b/src/core/ipfs/version.js @@ -1,6 +1,6 @@ 'use strict' -const utils = require('../utils') +const readPkgUp = require('read-pkg-up') module.exports = function version (self) { return (opts, callback) => { @@ -9,18 +9,10 @@ module.exports = function version (self) { opts = {} } - utils.ifRepoExists(self._repo, (err) => { - if (err) { - return callback(err) - } - - self._repo.config.get((err, config) => { - if (err) { - return callback(err) - } - - callback(null, config.Version.Current) + readPkgUp() + .then((res) => { + callback(null, res.pkg.version) }) - }) + .catch(callback) } } diff --git a/test/cli/test-version.js b/test/cli/test-version.js index 931c8a2785..dc0186f3f0 100644 --- a/test/cli/test-version.js +++ b/test/cli/test-version.js @@ -16,7 +16,7 @@ describe('version', () => { nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'version'], {env}) .run((err, stdout, exitcode) => { expect(err).to.not.exist - expect(stdout[0]).to.equal('ipfs version 0.4.0-dev') + expect(stdout[0]).to.equal('js-ipfs version: 0.14.1') expect(exitcode).to.equal(0) done() }) @@ -46,7 +46,7 @@ describe('version', () => { .run((err, stdout, exitcode) => { expect(err).to.not.exist expect(exitcode).to.equal(0) - expect(stdout[0]).to.equal('ipfs version 0.4.0-dev') + expect(stdout[0]).to.equal('js-ipfs version: 0.14.1') done() }) }) diff --git a/test/core/both/test-version.js b/test/core/both/test-version.js index 2a4303b8b3..c15fcf2469 100644 --- a/test/core/both/test-version.js +++ b/test/core/both/test-version.js @@ -16,7 +16,7 @@ describe('version', () => { it('get version', (done) => { ipfs.version((err, version) => { expect(err).to.not.exist - expect(version).to.equal('0.4.0-dev') + expect(version).to.equal('0.14.1') done() }) }) diff --git a/test/http-api/test-version.js b/test/http-api/test-version.js index 572a849ad5..f9887359c2 100644 --- a/test/http-api/test-version.js +++ b/test/http-api/test-version.js @@ -18,7 +18,7 @@ module.exports = (httpAPI) => { method: 'GET', url: '/api/v0/version' }, (res) => { - expect(res.result.Version).to.equal('0.4.0-dev') + expect(res.result.Version).to.equal('0.14.1') expect(res.result).to.have.a.property('Commit') expect(res.result).to.have.a.property('Repo') done() From ac040b79fb0e3a70536f0f0c6a40bb2ba160f673 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Wed, 3 Aug 2016 17:23:18 +0200 Subject: [PATCH 3/5] refactor(cli): restructure files add command --- package.json | 2 + src/cli/commands/files/add.js | 79 ++++++++++++++++++++++------------- test/cli/test-files.js | 17 ++++---- 3 files changed, 61 insertions(+), 37 deletions(-) diff --git a/package.json b/package.json index 1079f38c18..55b5f33f72 100644 --- a/package.json +++ b/package.json @@ -82,7 +82,9 @@ "libp2p-ipfs-browser": "^0.12.1", "lodash.get": "^4.4.0", "lodash.set": "^4.3.0", + "lodash.sortby": "^4.6.1", "mafmt": "^2.1.1", + "map-limit": "0.0.1", "multiaddr": "^2.0.2", "multihashes": "^0.2.2", "path-exists": "^3.0.0", diff --git a/src/cli/commands/files/add.js b/src/cli/commands/files/add.js index 5caa5d85a4..0d26af066d 100644 --- a/src/cli/commands/files/add.js +++ b/src/cli/commands/files/add.js @@ -6,9 +6,10 @@ const log = debug('cli:version') log.error = debug('cli:version:error') const bs58 = require('bs58') const fs = require('fs') -const parallelLimit = require('run-parallel-limit') const path = require('path') const glob = require('glob') +const sortBy = require('lodash.sortby') +const mapLimit = require('map-limit') function checkPath (inPath, recursive) { // This function is to check for the following possible inputs @@ -48,50 +49,70 @@ module.exports = { }, handler (argv) { - let rs - let inPath = checkPath(argv.file, argv.recursive) - glob(path.join(inPath, '/**/*'), (err, res) => { + utils.getIPFS((err, ipfs) => { if (err) { throw err } - utils.getIPFS((err, ipfs) => { + + glob(path.join(inPath, '/**/*'), (err, res) => { if (err) { throw err } + ipfs.files.createAddStream((err, i) => { - if (err) throw err - var filePair + if (err) { + throw err + } + const added = [] + i.on('data', (file) => { - console.log('added', bs58.encode(file.node.multihash()).toString(), file.path) + const hash = bs58.encode(file.node.multihash()).toString() + added.push({hash, path: file.path}) }) - i.once('end', () => { - return + + i.on('end', () => { + sortBy(added, 'path') + .reverse() + .map((file) => `added ${file.hash} ${file.path}`) + .forEach((msg) => console.log(msg)) }) - if (res.length !== 0) { - const index = inPath.lastIndexOf('/') - parallelLimit(res.map((element) => (callback) => { - if (!fs.statSync(element).isDirectory()) { - i.write({ - path: element.substring(index + 1, element.length), - content: fs.createReadStream(element) - }) - } - callback() - }), 10, (err) => { + + if (res.length === 0) { + res = [inPath] + } + + const writeToStream = (stream, element) => { + const index = inPath.lastIndexOf('/') + 1 + i.write({ + path: element.substring(index, element.length), + content: fs.createReadStream(element) + }) + } + + mapLimit(res, 50, (file, cb) => { + fs.stat(file, (err, stat) => { if (err) { - throw err + return cb(err) } - i.end() + return cb(null, { + path: file, + isDirectory: stat.isDirectory() + }) }) - } else { - rs = fs.createReadStream(inPath) - inPath = inPath.substring(inPath.lastIndexOf('/') + 1, inPath.length) - filePair = {path: inPath, content: rs} - i.write(filePair) + }, (err, res) => { + if (err) { + throw err + } + + res + .filter((elem) => !elem.isDirectory) + .map((elem) => elem.path) + .forEach((elem) => writeToStream(i, elem)) + i.end() - } + }) }) }) }) diff --git a/test/cli/test-files.js b/test/cli/test-files.js index 635138de37..ffe0a5ae78 100644 --- a/test/cli/test-files.js +++ b/test/cli/test-files.js @@ -36,17 +36,18 @@ describe('files', () => { .run((err, stdout, exitcode) => { expect(err).to.not.exist expect(exitcode).to.equal(0) + const expected = [ - 'added QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V init-docs/about', - 'added QmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y init-docs/contact', - 'added QmQN88TEidd3RY2u3dpib49fERTDfKtDpvxnvczATNsfKT init-docs/docs/index', - 'added QmY5heUM5qgRubMDD1og9fhCPA6QdkMp3QCwd4s7gJsyE7 init-docs/help', - 'added QmdncfsVm2h5Kqq9hPmU7oAVX2zTSVP3L869tgTbPYnsha init-docs/quick-start', - 'added QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB init-docs/readme', - 'added QmTumTjvcYCAvRRwQ8sDRxh8ezmrcr88YFU7iYNroGGTBZ init-docs/security-notes', 'added QmYE7xo6NxbHEVEHej1yzxijYaNY51BaeKxjXxn6Ssa6Bs init-docs/tour/0.0-intro', - 'added QmegvLXxpVKiZ4b57Xs1syfBVRd8CbucVHAp7KpLQdGieC init-docs/docs', 'added QmciSU8hfpAXKjvK5YLUSwApomGSWN5gFbP4EpDAEzu2Te init-docs/tour', + 'added QmTumTjvcYCAvRRwQ8sDRxh8ezmrcr88YFU7iYNroGGTBZ init-docs/security-notes', + 'added QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB init-docs/readme', + 'added QmdncfsVm2h5Kqq9hPmU7oAVX2zTSVP3L869tgTbPYnsha init-docs/quick-start', + 'added QmY5heUM5qgRubMDD1og9fhCPA6QdkMp3QCwd4s7gJsyE7 init-docs/help', + 'added QmQN88TEidd3RY2u3dpib49fERTDfKtDpvxnvczATNsfKT init-docs/docs/index', + 'added QmegvLXxpVKiZ4b57Xs1syfBVRd8CbucVHAp7KpLQdGieC init-docs/docs', + 'added QmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y init-docs/contact', + 'added QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V init-docs/about', 'added QmUhUuiTKkkK8J6JZ9zmj8iNHPuNfGYcszgRumzhHBxEEU init-docs' ] expect(stdout).to.deep.equal(expected) From b595eaaa4fef0be7a68d28aa31f9421f80250aac Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Wed, 3 Aug 2016 20:52:17 +0200 Subject: [PATCH 4/5] test fixes and dep updates --- src/cli/commands/config.js | 2 +- src/cli/commands/version.js | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/cli/commands/config.js b/src/cli/commands/config.js index ee0e7d377b..b3230d5b17 100644 --- a/src/cli/commands/config.js +++ b/src/cli/commands/config.js @@ -51,7 +51,7 @@ module.exports = { throw new Error('failed to read the config') } - console.log(config) + console.log(config.Value) }) } diff --git a/src/cli/commands/version.js b/src/cli/commands/version.js index cc1b52f99b..bea6e9fecd 100644 --- a/src/cli/commands/version.js +++ b/src/cli/commands/version.js @@ -38,8 +38,7 @@ module.exports = { } if (typeof version === 'object') { // js-ipfs-api output - console.log('ipfs version', version.Version) - return + version = version.Version } console.log(`js-ipfs version: ${version}`) From 3ffdc2709270c792e30cd06f110726c8304c5e0b Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Thu, 4 Aug 2016 11:52:03 +0200 Subject: [PATCH 5/5] fix: use static version of package.json --- src/core/ipfs/version.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/core/ipfs/version.js b/src/core/ipfs/version.js index ec48bd504b..2b7dd647a4 100644 --- a/src/core/ipfs/version.js +++ b/src/core/ipfs/version.js @@ -1,6 +1,6 @@ 'use strict' -const readPkgUp = require('read-pkg-up') +const pkg = require('../../../package.json') module.exports = function version (self) { return (opts, callback) => { @@ -9,10 +9,6 @@ module.exports = function version (self) { opts = {} } - readPkgUp() - .then((res) => { - callback(null, res.pkg.version) - }) - .catch(callback) + callback(null, pkg.version) } }