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

Commit 593334b

Browse files
pkafeiAlan Shaw
authored andcommitted
feat: add slient option (#1712)
1 parent 34c8eb0 commit 593334b

File tree

9 files changed

+55
-5
lines changed

9 files changed

+55
-5
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,14 @@ Instead of a boolean, you may provide an object with custom initialization optio
268268

269269
A passphrase to encrypt/decrypt your keys.
270270

271+
##### `options.silent`
272+
273+
| Type | Default |
274+
|------|---------|
275+
| Boolean | `false` |
276+
277+
Prevents all logging output from the IPFS node.
278+
271279
##### `options.relay`
272280

273281
| Type | Default |

src/cli/bin.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ const cli = yargs
2424
desc: 'Write no output',
2525
type: 'boolean',
2626
default: false,
27-
coerce: ('silent', silent => silent ? utils.disablePrinting() : silent)
27+
coerce: ('silent', silent => {
28+
if (silent) {
29+
utils.disablePrinting()
30+
}
31+
return silent
32+
})
2833
})
2934
.option('pass', {
3035
desc: 'Pass phrase for the keys',

src/cli/utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ exports.getIPFS = (argv, callback) => {
4747
// Required inline to reduce startup time
4848
const IPFS = require('../core')
4949
const node = new IPFS({
50+
silent: argv.silent,
5051
repo: exports.getRepoPath(),
5152
init: false,
5253
start: false,

src/core/components/libp2p.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ module.exports = function libp2p (self) {
107107
if (err) { return callback(err) }
108108

109109
self._libp2pNode.peerInfo.multiaddrs.forEach((ma) => {
110-
console.log('Swarm listening on', ma.toString())
110+
self._print('Swarm listening on', ma.toString())
111111
})
112112

113113
callback()

src/core/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ class IPFS extends EventEmitter {
127127
this._preload = preload(this)
128128
this._mfsPreload = mfsPreload(this)
129129
this._ipns = undefined
130+
this._print = this._options.silent ? this.log : console.log
130131

131132
// IPFS Core exposed components
132133
// - for booting up a node

src/http/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ function HttpApi (repo, config, cliArgs) {
6868
try {
6969
// start the daemon
7070
this.node = new IPFS({
71+
silent: cliArgs.silent,
7172
repo: repo,
7273
init: init,
7374
start: true,
@@ -157,9 +158,9 @@ function HttpApi (repo, config, cliArgs) {
157158
api.info.ma = uriToMultiaddr(api.info.uri)
158159
gateway.info.ma = uriToMultiaddr(gateway.info.uri)
159160

160-
console.log('API listening on %s', api.info.ma)
161-
console.log('Gateway (read only) listening on %s', gateway.info.ma)
162-
console.log('Web UI available at %s', api.info.uri + '/webui')
161+
this.node._print('API listening on %s', api.info.ma)
162+
this.node._print('Gateway (read only) listening on %s', gateway.info.ma)
163+
this.node._print('Web UI available at %s', api.info.uri + '/webui')
163164

164165
// for the CLI to know the where abouts of the API
165166
this.node._repo.apiAddr.set(api.info.ma, cb)

test/cli/daemon.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,21 @@ describe('daemon', () => {
126126
})
127127
})
128128

129+
it('should be silent', function (done) {
130+
this.timeout(10 * 1000)
131+
const res = ipfs('daemon --silent')
132+
res.catch(function () {}) // Handles the unhandled promise rejection
133+
let output = ''
134+
const onData = (d) => { output += d }
135+
res.stdout.on('data', onData)
136+
res.stderr.on('data', onData)
137+
setTimeout(function () {
138+
res.kill()
139+
expect(output).to.be.empty()
140+
done()
141+
}, 5 * 1000)
142+
})
143+
129144
it('should present ipfs path help when option help is received', function (done) {
130145
this.timeout(100 * 1000)
131146

test/core/create-node.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const dirtyChai = require('dirty-chai')
77
const expect = chai.expect
88
chai.use(dirtyChai)
99
const series = require('async/series')
10+
const sinon = require('sinon')
1011
const waterfall = require('async/waterfall')
1112
const parallel = require('async/parallel')
1213
const os = require('os')
@@ -131,6 +132,21 @@ describe('create node', function () {
131132
})
132133
})
133134

135+
it('should be silent', (done) => {
136+
sinon.spy(console, 'log')
137+
138+
const ipfs = new IPFS({
139+
silent: true,
140+
repo: tempRepo
141+
})
142+
143+
ipfs.on('ready', () => {
144+
expect(console.log.called).to.be.false()
145+
console.log.restore()
146+
done()
147+
})
148+
})
149+
134150
it('init: false errors (start default: true) and errors only once', function (done) {
135151
this.timeout(80 * 1000)
136152

test/core/libp2p.spec.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ describe('libp2p customization', function () {
7676
},
7777
_peerInfo: peerInfo,
7878
_peerBook: peerBook,
79+
_print: console.log,
7980
config: mockConfig,
8081
_options: {
8182
libp2p: (opts) => {
@@ -122,6 +123,7 @@ describe('libp2p customization', function () {
122123
},
123124
_peerInfo: peerInfo,
124125
_peerBook: peerBook,
126+
_print: console.log,
125127
config: mockConfig
126128
}
127129

@@ -164,6 +166,7 @@ describe('libp2p customization', function () {
164166
},
165167
_peerInfo: peerInfo,
166168
_peerBook: peerBook,
169+
_print: console.log,
167170
config: mockConfig,
168171
_options: {
169172
config: {

0 commit comments

Comments
 (0)