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

Commit 1c07779

Browse files
grassiasAlan Shaw
authored andcommitted
fix: CLI parsing of --silent arg (#1955)
* fix: create helper function to parse global options * fix: use only one yargs instance * fix: export yargs parser and add some tests * fix: separate yargs parser for ease of testing * fix: remove unneeded dependency fixes #1947
1 parent a3b6235 commit 1c07779

File tree

3 files changed

+119
-68
lines changed

3 files changed

+119
-68
lines changed

src/cli/bin.js

Lines changed: 33 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -3,91 +3,56 @@
33
'use strict'
44

55
const YargsPromise = require('yargs-promise')
6-
const yargs = require('yargs/yargs')
76
const updateNotifier = require('update-notifier')
87
const utils = require('./utils')
98
const print = utils.print
109
const mfs = require('ipfs-mfs/cli')
1110
const debug = require('debug')('ipfs:cli')
1211
const pkg = require('../../package.json')
12+
const parser = require('./parser')
1313

1414
async function main (args) {
1515
const oneWeek = 1000 * 60 * 60 * 24 * 7
1616
updateNotifier({ pkg, updateCheckInterval: oneWeek }).notify()
1717

18-
const cli = yargs(args)
19-
.option('silent', {
20-
desc: 'Write no output',
21-
type: 'boolean',
22-
default: false,
23-
coerce: silent => {
24-
if (silent) utils.disablePrinting()
25-
return silent
26-
}
27-
})
28-
.option('pass', {
29-
desc: 'Pass phrase for the keys',
30-
type: 'string',
31-
default: ''
32-
})
33-
.epilog(utils.ipfsPathHelp)
34-
.demandCommand(1)
35-
.fail((msg, err, yargs) => {
36-
if (err) {
37-
throw err // preserve stack
38-
}
39-
40-
if (args.length > 0) {
41-
print(msg)
42-
}
43-
44-
yargs.showHelp()
45-
})
46-
47-
// Function to get hold of a singleton ipfs instance
48-
const getIpfs = utils.singleton(cb => utils.getIPFS(yargs(args).argv, cb))
18+
const cli = new YargsPromise(parser)
4919

5020
// add MFS (Files API) commands
5121
mfs(cli)
5222

53-
cli
54-
.commandDir('commands')
55-
.help()
56-
.strict()
57-
.completion()
58-
59-
let exitCode = 0
60-
61-
try {
62-
const { data } = await new YargsPromise(cli, { getIpfs }).parse(args)
63-
if (data) print(data)
64-
} catch (err) {
65-
debug(err)
66-
67-
// the argument can have a different shape depending on where the error came from
68-
if (err.message || (err.error && err.error.message)) {
69-
print(err.message || err.error.message)
70-
} else {
71-
print('Unknown error, please re-run the command with DEBUG=ipfs:cli to see debug output')
72-
}
23+
let getIpfs = null
7324

74-
exitCode = 1
75-
} finally {
76-
// If an IPFS instance was used in the handler then clean it up here
77-
if (getIpfs.instance) {
78-
try {
79-
const cleanup = getIpfs.rest[0]
80-
await cleanup()
81-
} catch (err) {
82-
debug(err)
83-
exitCode = 1
25+
cli
26+
.parse(args)
27+
.then(({ data, argv }) => {
28+
getIpfs = argv.getIpfs
29+
if (data) {
30+
print(data)
8431
}
85-
}
86-
}
87-
88-
if (exitCode) {
89-
process.exit(exitCode)
90-
}
32+
})
33+
.catch(({ error, argv }) => {
34+
getIpfs = argv.getIpfs
35+
debug(error)
36+
// the argument can have a different shape depending on where the error came from
37+
if (error.message || (error.error && error.error.message)) {
38+
print(error.message || error.error.message)
39+
} else {
40+
print('Unknown error, please re-run the command with DEBUG=ipfs:cli to see debug output')
41+
}
42+
process.exit(1)
43+
})
44+
.finally(async () => {
45+
// If an IPFS instance was used in the handler then clean it up here
46+
if (getIpfs && getIpfs.instance) {
47+
try {
48+
const cleanup = getIpfs.rest[0]
49+
await cleanup()
50+
} catch (err) {
51+
debug(err)
52+
process.exit(1)
53+
}
54+
}
55+
})
9156
}
9257

9358
main(process.argv.slice(2))

src/cli/parser.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict'
2+
3+
const yargs = require('yargs')
4+
const utils = require('./utils')
5+
const print = utils.print
6+
7+
const parser = yargs
8+
.option('silent', {
9+
desc: 'Write no output',
10+
type: 'boolean',
11+
default: false,
12+
coerce: silent => {
13+
if (silent) utils.disablePrinting()
14+
return silent
15+
}
16+
})
17+
.option('pass', {
18+
desc: 'Pass phrase for the keys',
19+
type: 'string',
20+
default: ''
21+
})
22+
.epilog(utils.ipfsPathHelp)
23+
.demandCommand(1)
24+
.fail((msg, err, yargs) => {
25+
if (err) {
26+
throw err // preserve stack
27+
}
28+
print(msg)
29+
yargs.showHelp()
30+
})
31+
.commandDir('commands')
32+
.middleware(argv => {
33+
// Function to get hold of a singleton ipfs instance
34+
argv.getIpfs = utils.singleton(cb => utils.getIPFS(argv, cb))
35+
return argv
36+
})
37+
.help()
38+
.strict()
39+
.completion()
40+
41+
module.exports = parser

test/cli/parser.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const chai = require('chai')
5+
const dirtyChai = require('dirty-chai')
6+
const expect = chai.expect
7+
chai.use(dirtyChai)
8+
const parser = require('../../src/cli/parser')
9+
const YargsPromise = require('yargs-promise')
10+
11+
describe('yargs cli parser', () => {
12+
let cli
13+
14+
before(() => {
15+
cli = new YargsPromise(parser)
16+
})
17+
18+
it('should handle --silent flag correctly', (done) => {
19+
cli
20+
.parse('serve --silent src/init-files/init-docs/readme')
21+
.then(({ error, argv }) => {
22+
expect(error).to.not.exist()
23+
expect(argv).to.include({ silent: true, pass: '' })
24+
expect(argv.getIpfs.instance).to.exist()
25+
done()
26+
})
27+
.catch(({ error }) => {
28+
done(error)
29+
})
30+
})
31+
32+
it('should handle --pass flag correctly', (done) => {
33+
cli
34+
.parse('serve --pass password')
35+
.then(({ error, argv }) => {
36+
expect(error).to.not.exist()
37+
expect(argv).to.include({ silent: true, pass: '' })
38+
expect(argv.getIpfs.instance).to.exist()
39+
done()
40+
})
41+
.catch(({ error }) => {
42+
done(error)
43+
})
44+
})
45+
})

0 commit comments

Comments
 (0)