Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Refactor printing in CLI #931

Merged
merged 3 commits into from
Jul 24, 2017
Merged
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
9 changes: 8 additions & 1 deletion src/cli/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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) => {
Expand Down Expand Up @@ -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 }
Expand Down
3 changes: 2 additions & 1 deletion src/cli/commands/bitswap/stat.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const CID = require('cids')
const print = require('../../utils').print

module.exports = {
command: 'stat',
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/cli/commands/bitswap/wantlist.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

const print = require('../../utils').print

module.exports = {
command: 'wantlist',

Expand All @@ -20,7 +22,7 @@ module.exports = {
throw err
}
res.Keys.forEach((cidStr) => {
console.log(cidStr)
print(cidStr)
})
})
}
Expand Down
3 changes: 2 additions & 1 deletion src/cli/commands/block/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,7 +27,7 @@ function addBlock (data, opts) {
if (err) {
throw err
}
console.log(cid.toBaseEncodedString())
print(cid.toBaseEncodedString())
})
}

Expand Down
3 changes: 2 additions & 1 deletion src/cli/commands/block/rm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const utils = require('../../utils')
const mh = require('multihashes')
const print = utils.print

module.exports = {
command: 'rm <key>',
Expand All @@ -21,7 +22,7 @@ module.exports = {
throw err
}

console.log('removed', argv.key)
print('removed ' + argv.key)
})
}
}
5 changes: 3 additions & 2 deletions src/cli/commands/block/stat.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const CID = require('cids')
const print = require('../../utils').print

module.exports = {
command: 'stat <key>',
Expand All @@ -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)
})
}
}
4 changes: 3 additions & 1 deletion src/cli/commands/bootstrap/add.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

const print = require('../../utils').print

module.exports = {
command: 'add [<peer>]',

Expand All @@ -21,7 +23,7 @@ module.exports = {
throw err
}

list.Peers.forEach((l) => console.log(l))
list.Peers.forEach((peer) => print(peer))
})
}
}
6 changes: 3 additions & 3 deletions src/cli/commands/bootstrap/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

const print = require('../../utils').print

module.exports = {
command: 'list',

Expand All @@ -13,9 +15,7 @@ module.exports = {
throw err
}

list.Peers.forEach((node) => {
console.log(node)
})
list.Peers.forEach((node) => print(node))
})
}
}
4 changes: 3 additions & 1 deletion src/cli/commands/bootstrap/rm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 [<peer>]',

Expand All @@ -24,7 +26,7 @@ module.exports = {
throw err
}

list.Peers.forEach((l) => console.log(l))
list.Peers.forEach((peer) => print(peer))
})
}
}
4 changes: 2 additions & 2 deletions src/cli/commands/commands.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict'

const print = require('../utils').print
const path = require('path')
const glob = require('glob').sync

Expand All @@ -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'))
}
}
5 changes: 3 additions & 2 deletions src/cli/commands/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict'
const print = require('../utils').print

module.exports = {
command: 'config <key> [value]',
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion src/cli/commands/config/show.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',

Expand All @@ -19,7 +21,7 @@ module.exports = {
throw err
}

console.log(JSON.stringify(config, null, 4))
print(JSON.stringify(config, null, 4))
})
}
}
11 changes: 6 additions & 5 deletions src/cli/commands/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const HttpAPI = require('../../http-api')
const utils = require('../utils')
const print = utils.print

let httpAPI

Expand All @@ -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
Expand Down
15 changes: 8 additions & 7 deletions src/cli/commands/dag/get.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const CID = require('cids')
const print = require('../../utils').print

module.exports = {
command: 'get <cid path>',
Expand All @@ -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
Expand All @@ -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)
}
})
}
Expand Down
3 changes: 2 additions & 1 deletion src/cli/commands/files/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/'

Expand Down Expand Up @@ -77,7 +78,7 @@ function addPipeline (index, addStream, list, wrapWithDirectory) {

return log.join(' ')
})
.forEach((msg) => console.log(msg))
.forEach((msg) => print(msg))
})
)
}
Expand Down
3 changes: 2 additions & 1 deletion src/cli/commands/files/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)),
Expand Down
3 changes: 2 additions & 1 deletion src/cli/commands/id.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict'
const print = require('../utils').print

module.exports = {
command: 'id',
Expand All @@ -19,7 +20,7 @@ module.exports = {
throw err
}

console.log(JSON.stringify(id, '', 2))
print(JSON.stringify(id, '', 2))
})
}
}
6 changes: 3 additions & 3 deletions src/cli/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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),
Expand All @@ -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') {
Expand Down
Loading