From a5e75e02cca28337d3f91a711c6a27758c2ead96 Mon Sep 17 00:00:00 2001 From: RasmusErik Voel Jensen Date: Mon, 24 Jul 2017 20:38:31 +0200 Subject: [PATCH] feat: new print func for the CLI (#931) * refactor: CLI should use print instead of console.log #495 License: MIT Signed-off-by: Rasmus Erik Voel Jensen * feat: add quiet/verbose CLI flag, - related to #931 License: MIT Signed-off-by: Rasmus Erik Voel Jensen * add test, and remove verbosity levels License: MIT Signed-off-by: Rasmus Erik Voel Jensen --- src/cli/bin.js | 9 +++++++- src/cli/commands/bitswap/stat.js | 3 ++- src/cli/commands/bitswap/wantlist.js | 4 +++- src/cli/commands/block/put.js | 3 ++- src/cli/commands/block/rm.js | 3 ++- src/cli/commands/block/stat.js | 5 ++-- src/cli/commands/bootstrap/add.js | 4 +++- src/cli/commands/bootstrap/list.js | 6 ++--- src/cli/commands/bootstrap/rm.js | 4 +++- src/cli/commands/commands.js | 4 ++-- src/cli/commands/config.js | 5 ++-- src/cli/commands/config/show.js | 4 +++- src/cli/commands/daemon.js | 11 +++++---- src/cli/commands/dag/get.js | 15 ++++++------ src/cli/commands/files/add.js | 3 ++- src/cli/commands/files/get.js | 3 ++- src/cli/commands/id.js | 3 ++- src/cli/commands/init.js | 6 ++--- src/cli/commands/object/data.js | 4 +++- src/cli/commands/object/get.js | 4 +++- src/cli/commands/object/links.js | 8 +++---- src/cli/commands/object/new.js | 3 ++- src/cli/commands/object/patch/add-link.js | 3 ++- src/cli/commands/object/patch/append-data.js | 3 ++- src/cli/commands/object/patch/rm-link.js | 3 ++- src/cli/commands/object/patch/set-data.js | 3 ++- src/cli/commands/object/put.js | 3 ++- src/cli/commands/object/stat.js | 4 +++- src/cli/commands/pubsub/ls.js | 4 +++- src/cli/commands/pubsub/peers.js | 6 ++--- src/cli/commands/pubsub/sub.js | 4 +++- src/cli/commands/repo/version.js | 4 +++- src/cli/commands/swarm/addrs.js | 6 +++-- src/cli/commands/swarm/addrs/local.js | 3 ++- src/cli/commands/swarm/connect.js | 3 ++- src/cli/commands/swarm/disconnect.js | 3 ++- src/cli/commands/swarm/peers.js | 3 ++- src/cli/commands/version.js | 4 +++- src/cli/utils.js | 24 +++++++++++--------- test/cli/general.js | 13 +++++++++++ 40 files changed, 137 insertions(+), 73 deletions(-) create mode 100644 test/cli/general.js diff --git a/src/cli/bin.js b/src/cli/bin.js index 0b02459ae1..8da1473cdd 100755 --- a/src/cli/bin.js +++ b/src/cli/bin.js @@ -6,6 +6,7 @@ const yargs = require('yargs') const updateNotifier = require('update-notifier') const readPkgUp = require('read-pkg-up') const utils = require('./utils') +const print = utils.print const pkg = readPkgUp.sync({cwd: __dirname}).pkg updateNotifier({ @@ -14,6 +15,12 @@ updateNotifier({ }).notify() const cli = yargs + .option('q', { + alias: 'quiet', + desc: 'suppress output', + type: 'boolean', + coerce: (quiet) => { if (quiet) { utils.disablePrinting() } } + }) .commandDir('commands') .demandCommand(1) .fail((msg, err, yargs) => { @@ -53,7 +60,7 @@ if (args[0] === 'daemon' || args[0] === 'init') { .strict(false) .completion() .parse(args, { ipfs: ipfs }, (err, argv, output) => { - if (output) { console.log(output) } + if (output) { print(output) } cleanup(() => { if (err) { throw err } diff --git a/src/cli/commands/bitswap/stat.js b/src/cli/commands/bitswap/stat.js index 6a404cdd40..f19d5d9f08 100644 --- a/src/cli/commands/bitswap/stat.js +++ b/src/cli/commands/bitswap/stat.js @@ -1,6 +1,7 @@ 'use strict' const CID = require('cids') +const print = require('../../utils').print module.exports = { command: 'stat', @@ -23,7 +24,7 @@ module.exports = { }) stats.Peers = stats.Peers || [] - console.log(`bitswap status + print(`bitswap status blocks received: ${stats.BlocksReceived} dup blocks received: ${stats.DupBlksReceived} dup data received: ${stats.DupDataReceived}B diff --git a/src/cli/commands/bitswap/wantlist.js b/src/cli/commands/bitswap/wantlist.js index 94a38e6dcf..63692a993a 100644 --- a/src/cli/commands/bitswap/wantlist.js +++ b/src/cli/commands/bitswap/wantlist.js @@ -1,5 +1,7 @@ 'use strict' +const print = require('../../utils').print + module.exports = { command: 'wantlist', @@ -20,7 +22,7 @@ module.exports = { throw err } res.Keys.forEach((cidStr) => { - console.log(cidStr) + print(cidStr) }) }) } diff --git a/src/cli/commands/block/put.js b/src/cli/commands/block/put.js index 73814f7c37..9d75a9c72f 100644 --- a/src/cli/commands/block/put.js +++ b/src/cli/commands/block/put.js @@ -6,6 +6,7 @@ const bl = require('bl') const fs = require('fs') const Block = require('ipfs-block') const waterfall = require('async/waterfall') +const print = require('../../utils').print function addBlock (data, opts) { const ipfs = opts.ipfs @@ -26,7 +27,7 @@ function addBlock (data, opts) { if (err) { throw err } - console.log(cid.toBaseEncodedString()) + print(cid.toBaseEncodedString()) }) } diff --git a/src/cli/commands/block/rm.js b/src/cli/commands/block/rm.js index 729a1fd6f9..71fc26726c 100644 --- a/src/cli/commands/block/rm.js +++ b/src/cli/commands/block/rm.js @@ -2,6 +2,7 @@ const utils = require('../../utils') const mh = require('multihashes') +const print = utils.print module.exports = { command: 'rm ', @@ -21,7 +22,7 @@ module.exports = { throw err } - console.log('removed', argv.key) + print('removed ' + argv.key) }) } } diff --git a/src/cli/commands/block/stat.js b/src/cli/commands/block/stat.js index c4df1e35d9..07190fd225 100644 --- a/src/cli/commands/block/stat.js +++ b/src/cli/commands/block/stat.js @@ -1,6 +1,7 @@ 'use strict' const CID = require('cids') +const print = require('../../utils').print module.exports = { command: 'stat ', @@ -15,8 +16,8 @@ module.exports = { throw err } - console.log('Key:', stats.key) - console.log('Size:', stats.size) + print('Key: ' + stats.key) + print('Size: ' + stats.size) }) } } diff --git a/src/cli/commands/bootstrap/add.js b/src/cli/commands/bootstrap/add.js index da0ed43805..ebab4736c9 100644 --- a/src/cli/commands/bootstrap/add.js +++ b/src/cli/commands/bootstrap/add.js @@ -1,5 +1,7 @@ 'use strict' +const print = require('../../utils').print + module.exports = { command: 'add []', @@ -21,7 +23,7 @@ module.exports = { throw err } - list.Peers.forEach((l) => console.log(l)) + list.Peers.forEach((peer) => print(peer)) }) } } diff --git a/src/cli/commands/bootstrap/list.js b/src/cli/commands/bootstrap/list.js index 7cf0bd5b29..4a9a76baee 100644 --- a/src/cli/commands/bootstrap/list.js +++ b/src/cli/commands/bootstrap/list.js @@ -1,5 +1,7 @@ 'use strict' +const print = require('../../utils').print + module.exports = { command: 'list', @@ -13,9 +15,7 @@ module.exports = { throw err } - list.Peers.forEach((node) => { - console.log(node) - }) + list.Peers.forEach((node) => print(node)) }) } } diff --git a/src/cli/commands/bootstrap/rm.js b/src/cli/commands/bootstrap/rm.js index 1a2462a9ed..cb8e398cc3 100644 --- a/src/cli/commands/bootstrap/rm.js +++ b/src/cli/commands/bootstrap/rm.js @@ -3,6 +3,8 @@ const debug = require('debug') const log = debug('cli:bootstrap') log.error = debug('cli:bootstrap:error') +const print = require('../../utils').print + module.exports = { command: 'rm []', @@ -24,7 +26,7 @@ module.exports = { throw err } - list.Peers.forEach((l) => console.log(l)) + list.Peers.forEach((peer) => print(peer)) }) } } diff --git a/src/cli/commands/commands.js b/src/cli/commands/commands.js index ea60b587df..176706ab3a 100644 --- a/src/cli/commands/commands.js +++ b/src/cli/commands/commands.js @@ -1,5 +1,5 @@ 'use strict' - +const print = require('../utils').print const path = require('path') const glob = require('glob').sync @@ -23,6 +23,6 @@ module.exports = { .replace('.js', '') }).sort().map((cmd) => `ipfs ${cmd}`) - console.log(['ipfs'].concat(cmds).join('\n')) + print(['ipfs'].concat(cmds).join('\n')) } } diff --git a/src/cli/commands/config.js b/src/cli/commands/config.js index d2a3828f2f..c31be9372e 100644 --- a/src/cli/commands/config.js +++ b/src/cli/commands/config.js @@ -1,4 +1,5 @@ 'use strict' +const print = require('../utils').print module.exports = { command: 'config [value]', @@ -41,9 +42,9 @@ module.exports = { } if (typeof value === 'object') { - console.log(JSON.stringify(value, null, 2)) + print(JSON.stringify(value, null, 2)) } else { - console.log(value) + print(value) } }) } else { diff --git a/src/cli/commands/config/show.js b/src/cli/commands/config/show.js index 45c9c615e2..be3730602b 100644 --- a/src/cli/commands/config/show.js +++ b/src/cli/commands/config/show.js @@ -3,6 +3,8 @@ const debug = require('debug') const log = debug('cli:config') log.error = debug('cli:config:error') +const print = require('../../utils').print + module.exports = { command: 'show', @@ -19,7 +21,7 @@ module.exports = { throw err } - console.log(JSON.stringify(config, null, 4)) + print(JSON.stringify(config, null, 4)) }) } } diff --git a/src/cli/commands/daemon.js b/src/cli/commands/daemon.js index 67a590956b..0414ca9654 100644 --- a/src/cli/commands/daemon.js +++ b/src/cli/commands/daemon.js @@ -2,6 +2,7 @@ const HttpAPI = require('../../http-api') const utils = require('../utils') +const print = utils.print let httpAPI @@ -22,25 +23,25 @@ module.exports = { }, handler (argv) { - console.log('Initializing daemon...') + print('Initializing daemon...') const repoPath = utils.getRepoPath() httpAPI = new HttpAPI(process.env.IPFS_PATH, null, argv) httpAPI.start((err) => { if (err && err.code === 'ENOENT' && err.message.match(/Uninitalized repo/i)) { - console.log('Error: no initialized ipfs repo found in ' + repoPath) - console.log('please run: jsipfs init') + print('Error: no initialized ipfs repo found in ' + repoPath) + print('please run: jsipfs init') process.exit(1) } if (err) { throw err } - console.log('Daemon is ready') + print('Daemon is ready') }) process.on('SIGINT', () => { - console.log('Received interrupt signal, shutting down..') + print('Received interrupt signal, shutting down..') httpAPI.stop((err) => { if (err) { throw err diff --git a/src/cli/commands/dag/get.js b/src/cli/commands/dag/get.js index e4a65d6a83..957185c59c 100644 --- a/src/cli/commands/dag/get.js +++ b/src/cli/commands/dag/get.js @@ -1,6 +1,7 @@ 'use strict' const CID = require('cids') +const print = require('../../utils').print module.exports = { command: 'get ', @@ -26,12 +27,12 @@ module.exports = { argv.ipfs.dag.get(cid, path, options, (err, result) => { if (err) { - return console.log('dag get failed:', err.message) + return print(`dag get failed: ${err.message}`) } if (options.localResolve) { - console.log('resolving path within the node only') - console.log('remainder path:', result.remainderPath || 'n/a', '\n') + print('resolving path within the node only') + print(`remainder path: ${result.remainderPath || 'n/a'}\n`) } const node = result.value @@ -41,19 +42,19 @@ module.exports = { if (node._json) { delete node._json.multihash node._json.data = '0x' + node._json.data.toString('hex') - console.log(node._json) + print(node._json) return } if (Buffer.isBuffer(node)) { - console.log('0x' + node.toString('hex')) + print('0x' + node.toString('hex')) return } if (node.raw) { - console.log(node.raw) + print(node.raw) } else { - console.log(node) + print(node) } }) } diff --git a/src/cli/commands/files/add.js b/src/cli/commands/files/add.js index 415dd431b9..9e7d192106 100644 --- a/src/cli/commands/files/add.js +++ b/src/cli/commands/files/add.js @@ -9,6 +9,7 @@ const paramap = require('pull-paramap') const zip = require('pull-zip') const toPull = require('stream-to-pull-stream') const utils = require('../../utils') +const print = require('../../utils').print const WRAPPER = 'wrapper/' @@ -77,7 +78,7 @@ function addPipeline (index, addStream, list, wrapWithDirectory) { return log.join(' ') }) - .forEach((msg) => console.log(msg)) + .forEach((msg) => print(msg)) }) ) } diff --git a/src/cli/commands/files/get.js b/src/cli/commands/files/get.js index 534a2d31f3..598bd851cb 100644 --- a/src/cli/commands/files/get.js +++ b/src/cli/commands/files/get.js @@ -5,6 +5,7 @@ const path = require('path') const mkdirp = require('mkdirp') const pull = require('pull-stream') const toPull = require('stream-to-pull-stream') +const print = require('../../utils').print function checkArgs (hash, outPath) { // format the output directory @@ -68,7 +69,7 @@ module.exports = { if (err) { throw err } - console.log(`Saving file(s) ${ipfsPath}`) + print(`Saving file(s) ${ipfsPath}`) pull( toPull.source(stream), pull.asyncMap(fileHandler(dir)), diff --git a/src/cli/commands/id.js b/src/cli/commands/id.js index d1e63afa1f..81ed38eef4 100644 --- a/src/cli/commands/id.js +++ b/src/cli/commands/id.js @@ -1,4 +1,5 @@ 'use strict' +const print = require('../utils').print module.exports = { command: 'id', @@ -19,7 +20,7 @@ module.exports = { throw err } - console.log(JSON.stringify(id, '', 2)) + print(JSON.stringify(id, '', 2)) }) } } diff --git a/src/cli/commands/init.js b/src/cli/commands/init.js index ca80e85027..cf8f294d2b 100644 --- a/src/cli/commands/init.js +++ b/src/cli/commands/init.js @@ -3,6 +3,7 @@ const Repo = require('ipfs-repo') const IPFS = require('../../core') const utils = require('../utils') +const print = utils.print module.exports = { command: 'init', @@ -26,8 +27,7 @@ module.exports = { handler (argv) { const path = utils.getRepoPath() - const log = utils.createLogger(true) - log(`initializing ipfs node at ${path}`) + print(`initializing ipfs node at ${path}`) const node = new IPFS({ repo: new Repo(path), @@ -38,7 +38,7 @@ module.exports = { node.init({ bits: argv.bits, emptyRepo: argv.emptyRepo, - log: log + log: print }, (err) => { if (err) { if (err.code === 'EACCES') { diff --git a/src/cli/commands/object/data.js b/src/cli/commands/object/data.js index ec595c90ea..d356e1c6c1 100644 --- a/src/cli/commands/object/data.js +++ b/src/cli/commands/object/data.js @@ -1,5 +1,7 @@ 'use strict' +const print = require('../../utils').print + module.exports = { command: 'data ', @@ -15,7 +17,7 @@ module.exports = { throw err } - console.log(data.toString()) + print(data.toString()) }) } } diff --git a/src/cli/commands/object/get.js b/src/cli/commands/object/get.js index c0e77e624e..629a3a82d1 100644 --- a/src/cli/commands/object/get.js +++ b/src/cli/commands/object/get.js @@ -1,5 +1,7 @@ 'use strict' +const print = require('../../utils').print + module.exports = { command: 'get ', @@ -29,7 +31,7 @@ module.exports = { }) } - console.log(JSON.stringify(answer)) + print(JSON.stringify(answer)) }) } } diff --git a/src/cli/commands/object/links.js b/src/cli/commands/object/links.js index 1c391cf502..d657820fb4 100644 --- a/src/cli/commands/object/links.js +++ b/src/cli/commands/object/links.js @@ -1,5 +1,7 @@ 'use strict' +const print = require('../../utils').print + module.exports = { command: 'links ', @@ -18,11 +20,7 @@ module.exports = { links.forEach((link) => { link = link.toJSON() - console.log( - link.multihash, - link.size, - link.name - ) + print(`${link.multihash} ${link.size} ${link.name}`) }) }) } diff --git a/src/cli/commands/object/new.js b/src/cli/commands/object/new.js index b121989672..f96db85fb2 100644 --- a/src/cli/commands/object/new.js +++ b/src/cli/commands/object/new.js @@ -3,6 +3,7 @@ const debug = require('debug') const log = debug('cli:object') log.error = debug('cli:object:error') +const print = require('../../utils').print module.exports = { command: 'new [