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

Commit a5e75e0

Browse files
rasmuserikdaviddias
authored andcommitted
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 <[email protected]> * feat: add quiet/verbose CLI flag, - related to #931 License: MIT Signed-off-by: Rasmus Erik Voel Jensen <[email protected]> * add test, and remove verbosity levels License: MIT Signed-off-by: Rasmus Erik Voel Jensen <[email protected]>
1 parent 9860431 commit a5e75e0

40 files changed

+137
-73
lines changed

src/cli/bin.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const yargs = require('yargs')
66
const updateNotifier = require('update-notifier')
77
const readPkgUp = require('read-pkg-up')
88
const utils = require('./utils')
9+
const print = utils.print
910

1011
const pkg = readPkgUp.sync({cwd: __dirname}).pkg
1112
updateNotifier({
@@ -14,6 +15,12 @@ updateNotifier({
1415
}).notify()
1516

1617
const cli = yargs
18+
.option('q', {
19+
alias: 'quiet',
20+
desc: 'suppress output',
21+
type: 'boolean',
22+
coerce: (quiet) => { if (quiet) { utils.disablePrinting() } }
23+
})
1724
.commandDir('commands')
1825
.demandCommand(1)
1926
.fail((msg, err, yargs) => {
@@ -53,7 +60,7 @@ if (args[0] === 'daemon' || args[0] === 'init') {
5360
.strict(false)
5461
.completion()
5562
.parse(args, { ipfs: ipfs }, (err, argv, output) => {
56-
if (output) { console.log(output) }
63+
if (output) { print(output) }
5764

5865
cleanup(() => {
5966
if (err) { throw err }

src/cli/commands/bitswap/stat.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const CID = require('cids')
4+
const print = require('../../utils').print
45

56
module.exports = {
67
command: 'stat',
@@ -23,7 +24,7 @@ module.exports = {
2324
})
2425
stats.Peers = stats.Peers || []
2526

26-
console.log(`bitswap status
27+
print(`bitswap status
2728
blocks received: ${stats.BlocksReceived}
2829
dup blocks received: ${stats.DupBlksReceived}
2930
dup data received: ${stats.DupDataReceived}B

src/cli/commands/bitswap/wantlist.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict'
22

3+
const print = require('../../utils').print
4+
35
module.exports = {
46
command: 'wantlist',
57

@@ -20,7 +22,7 @@ module.exports = {
2022
throw err
2123
}
2224
res.Keys.forEach((cidStr) => {
23-
console.log(cidStr)
25+
print(cidStr)
2426
})
2527
})
2628
}

src/cli/commands/block/put.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const bl = require('bl')
66
const fs = require('fs')
77
const Block = require('ipfs-block')
88
const waterfall = require('async/waterfall')
9+
const print = require('../../utils').print
910

1011
function addBlock (data, opts) {
1112
const ipfs = opts.ipfs
@@ -26,7 +27,7 @@ function addBlock (data, opts) {
2627
if (err) {
2728
throw err
2829
}
29-
console.log(cid.toBaseEncodedString())
30+
print(cid.toBaseEncodedString())
3031
})
3132
}
3233

src/cli/commands/block/rm.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const utils = require('../../utils')
44
const mh = require('multihashes')
5+
const print = utils.print
56

67
module.exports = {
78
command: 'rm <key>',
@@ -21,7 +22,7 @@ module.exports = {
2122
throw err
2223
}
2324

24-
console.log('removed', argv.key)
25+
print('removed ' + argv.key)
2526
})
2627
}
2728
}

src/cli/commands/block/stat.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const CID = require('cids')
4+
const print = require('../../utils').print
45

56
module.exports = {
67
command: 'stat <key>',
@@ -15,8 +16,8 @@ module.exports = {
1516
throw err
1617
}
1718

18-
console.log('Key:', stats.key)
19-
console.log('Size:', stats.size)
19+
print('Key: ' + stats.key)
20+
print('Size: ' + stats.size)
2021
})
2122
}
2223
}

src/cli/commands/bootstrap/add.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict'
22

3+
const print = require('../../utils').print
4+
35
module.exports = {
46
command: 'add [<peer>]',
57

@@ -21,7 +23,7 @@ module.exports = {
2123
throw err
2224
}
2325

24-
list.Peers.forEach((l) => console.log(l))
26+
list.Peers.forEach((peer) => print(peer))
2527
})
2628
}
2729
}

src/cli/commands/bootstrap/list.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict'
22

3+
const print = require('../../utils').print
4+
35
module.exports = {
46
command: 'list',
57

@@ -13,9 +15,7 @@ module.exports = {
1315
throw err
1416
}
1517

16-
list.Peers.forEach((node) => {
17-
console.log(node)
18-
})
18+
list.Peers.forEach((node) => print(node))
1919
})
2020
}
2121
}

src/cli/commands/bootstrap/rm.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
const debug = require('debug')
44
const log = debug('cli:bootstrap')
55
log.error = debug('cli:bootstrap:error')
6+
const print = require('../../utils').print
7+
68
module.exports = {
79
command: 'rm [<peer>]',
810

@@ -24,7 +26,7 @@ module.exports = {
2426
throw err
2527
}
2628

27-
list.Peers.forEach((l) => console.log(l))
29+
list.Peers.forEach((peer) => print(peer))
2830
})
2931
}
3032
}

src/cli/commands/commands.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict'
2-
2+
const print = require('../utils').print
33
const path = require('path')
44
const glob = require('glob').sync
55

@@ -23,6 +23,6 @@ module.exports = {
2323
.replace('.js', '')
2424
}).sort().map((cmd) => `ipfs ${cmd}`)
2525

26-
console.log(['ipfs'].concat(cmds).join('\n'))
26+
print(['ipfs'].concat(cmds).join('\n'))
2727
}
2828
}

0 commit comments

Comments
 (0)