Skip to content

Commit 3d950c7

Browse files
committed
refactor: CLI should use print instead of console.log ipfs#495
License: MIT Signed-off-by: Rasmus Erik Voel Jensen <[email protected]>
1 parent bc8ffee commit 3d950c7

39 files changed

+106
-62
lines changed

src/cli/bin.js

+2-1
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({
@@ -53,7 +54,7 @@ if (args[0] === 'daemon' || args[0] === 'init') {
5354
.strict(false)
5455
.completion()
5556
.parse(args, { ipfs: ipfs }, (err, argv, output) => {
56-
if (output) { console.log(output) }
57+
if (output) { print(output) }
5758

5859
cleanup(() => {
5960
if (err) { throw err }

src/cli/commands/bitswap/stat.js

+2-1
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

+3-1
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

+2-1
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

+2-1
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

+3-2
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

+3-1
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

+3-3
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

+3-1
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

+2-2
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
}

src/cli/commands/config.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict'
2+
const print = require('../utils').print
23

34
module.exports = {
45
command: 'config <key> [value]',
@@ -41,9 +42,9 @@ module.exports = {
4142
}
4243

4344
if (typeof value === 'object') {
44-
console.log(JSON.stringify(value, null, 2))
45+
print(JSON.stringify(value, null, 2))
4546
} else {
46-
console.log(value)
47+
print(value)
4748
}
4849
})
4950
} else {

src/cli/commands/config/show.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
const debug = require('debug')
44
const log = debug('cli:config')
55
log.error = debug('cli:config:error')
6+
const print = require('../../utils').print
7+
68
module.exports = {
79
command: 'show',
810

@@ -19,7 +21,7 @@ module.exports = {
1921
throw err
2022
}
2123

22-
console.log(JSON.stringify(config, null, 4))
24+
print(JSON.stringify(config, null, 4))
2325
})
2426
}
2527
}

src/cli/commands/daemon.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const HttpAPI = require('../../http-api')
44
const utils = require('../utils')
5+
const print = utils.print
56

67
let httpAPI
78

@@ -22,25 +23,25 @@ module.exports = {
2223
},
2324

2425
handler (argv) {
25-
console.log('Initializing daemon...')
26+
print('Initializing daemon...')
2627

2728
const repoPath = utils.getRepoPath()
2829
httpAPI = new HttpAPI(process.env.IPFS_PATH, null, argv)
2930

3031
httpAPI.start((err) => {
3132
if (err && err.code === 'ENOENT' && err.message.match(/Uninitalized repo/i)) {
32-
console.log('Error: no initialized ipfs repo found in ' + repoPath)
33-
console.log('please run: jsipfs init')
33+
print('Error: no initialized ipfs repo found in ' + repoPath)
34+
print('please run: jsipfs init')
3435
process.exit(1)
3536
}
3637
if (err) {
3738
throw err
3839
}
39-
console.log('Daemon is ready')
40+
print('Daemon is ready')
4041
})
4142

4243
process.on('SIGINT', () => {
43-
console.log('Received interrupt signal, shutting down..')
44+
print('Received interrupt signal, shutting down..')
4445
httpAPI.stop((err) => {
4546
if (err) {
4647
throw err

src/cli/commands/dag/get.js

+8-7
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: 'get <cid path>',
@@ -26,12 +27,12 @@ module.exports = {
2627

2728
argv.ipfs.dag.get(cid, path, options, (err, result) => {
2829
if (err) {
29-
return console.log('dag get failed:', err.message)
30+
return print(`dag get failed: ${err.message}`)
3031
}
3132

3233
if (options.localResolve) {
33-
console.log('resolving path within the node only')
34-
console.log('remainder path:', result.remainderPath || 'n/a', '\n')
34+
print('resolving path within the node only')
35+
print(`remainder path: ${result.remainderPath || 'n/a'}\n`)
3536
}
3637

3738
const node = result.value
@@ -41,19 +42,19 @@ module.exports = {
4142
if (node._json) {
4243
delete node._json.multihash
4344
node._json.data = '0x' + node._json.data.toString('hex')
44-
console.log(node._json)
45+
print(node._json)
4546
return
4647
}
4748

4849
if (Buffer.isBuffer(node)) {
49-
console.log('0x' + node.toString('hex'))
50+
print('0x' + node.toString('hex'))
5051
return
5152
}
5253

5354
if (node.raw) {
54-
console.log(node.raw)
55+
print(node.raw)
5556
} else {
56-
console.log(node)
57+
print(node)
5758
}
5859
})
5960
}

src/cli/commands/files/add.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const paramap = require('pull-paramap')
99
const zip = require('pull-zip')
1010
const toPull = require('stream-to-pull-stream')
1111
const utils = require('../../utils')
12+
const print = require('../../utils').print
1213

1314
const WRAPPER = 'wrapper/'
1415

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

7879
return log.join(' ')
7980
})
80-
.forEach((msg) => console.log(msg))
81+
.forEach((msg) => print(msg))
8182
})
8283
)
8384
}

src/cli/commands/files/get.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const path = require('path')
55
const mkdirp = require('mkdirp')
66
const pull = require('pull-stream')
77
const toPull = require('stream-to-pull-stream')
8+
const print = require('../../utils').print
89

910
function checkArgs (hash, outPath) {
1011
// format the output directory
@@ -68,7 +69,7 @@ module.exports = {
6869
if (err) {
6970
throw err
7071
}
71-
console.log(`Saving file(s) ${ipfsPath}`)
72+
print(`Saving file(s) ${ipfsPath}`)
7273
pull(
7374
toPull.source(stream),
7475
pull.asyncMap(fileHandler(dir)),

src/cli/commands/id.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict'
2+
const print = require('../utils').print
23

34
module.exports = {
45
command: 'id',
@@ -19,7 +20,7 @@ module.exports = {
1920
throw err
2021
}
2122

22-
console.log(JSON.stringify(id, '', 2))
23+
print(JSON.stringify(id, '', 2))
2324
})
2425
}
2526
}

src/cli/commands/init.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const Repo = require('ipfs-repo')
44
const IPFS = require('../../core')
55
const utils = require('../utils')
6+
const print = utils.print
67

78
module.exports = {
89
command: 'init',
@@ -26,8 +27,7 @@ module.exports = {
2627
handler (argv) {
2728
const path = utils.getRepoPath()
2829

29-
const log = utils.createLogger(true)
30-
log(`initializing ipfs node at ${path}`)
30+
print(`initializing ipfs node at ${path}`)
3131

3232
const node = new IPFS({
3333
repo: new Repo(path),
@@ -38,7 +38,7 @@ module.exports = {
3838
node.init({
3939
bits: argv.bits,
4040
emptyRepo: argv.emptyRepo,
41-
log: log
41+
log: print
4242
}, (err) => {
4343
if (err) {
4444
if (err.code === 'EACCES') {

src/cli/commands/object/data.js

+3-1
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: 'data <key>',
57

@@ -15,7 +17,7 @@ module.exports = {
1517
throw err
1618
}
1719

18-
console.log(data.toString())
20+
print(data.toString())
1921
})
2022
}
2123
}

0 commit comments

Comments
 (0)