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

Commit 1b1fa05

Browse files
pgtedaviddias
authored andcommitted
feat: cli --api option (#1087)
1 parent b2106c1 commit 1b1fa05

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

src/cli/bin.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,27 @@ if (args[0] === 'daemon' || args[0] === 'init') {
5252
.completion()
5353
.parse(args)
5454
} else {
55-
utils.getIPFS((err, ipfs, cleanup) => {
56-
if (err) { throw err }
55+
// here we have to make a separate yargs instance with
56+
// only the `api` option because we need this before doing
57+
// the final yargs parse where the command handler is invoked..
58+
yargs().option('api').parse(process.argv, (err, argv, output) => {
59+
if (err) {
60+
throw err
61+
}
62+
utils.getIPFS(argv.api, (err, ipfs, cleanup) => {
63+
if (err) { throw err }
5764

58-
cli
59-
.help()
60-
.strict(false)
61-
.completion()
62-
.parse(args, { ipfs: ipfs }, (err, argv, output) => {
63-
if (output) { print(output) }
65+
cli
66+
.help()
67+
.strict(false)
68+
.completion()
69+
.parse(args, { ipfs: ipfs }, (err, argv, output) => {
70+
if (output) { print(output) }
6471

65-
cleanup(() => {
66-
if (err) { throw err }
72+
cleanup(() => {
73+
if (err) { throw err }
74+
})
6775
})
68-
})
76+
})
6977
})
7078
}

src/cli/utils.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,20 @@ function isDaemonOn () {
2727
}
2828

2929
exports.getAPICtl = getAPICtl
30-
function getAPICtl () {
31-
if (!isDaemonOn()) {
30+
function getAPICtl (apiAddr) {
31+
if (!apiAddr && !isDaemonOn()) {
3232
throw new Error('daemon is not on')
3333
}
34-
const apiPath = path.join(exports.getRepoPath(), 'api')
35-
const apiAddr = multiaddr(fs.readFileSync(apiPath).toString())
36-
return APIctl(apiAddr.toString())
34+
if (!apiAddr) {
35+
const apiPath = path.join(exports.getRepoPath(), 'api')
36+
apiAddr = multiaddr(fs.readFileSync(apiPath).toString()).toString()
37+
}
38+
return APIctl(apiAddr)
3739
}
3840

39-
exports.getIPFS = (callback) => {
40-
if (isDaemonOn()) {
41-
return callback(null, getAPICtl(), (cb) => cb())
41+
exports.getIPFS = (apiAddr, callback) => {
42+
if (apiAddr || isDaemonOn()) {
43+
return callback(null, getAPICtl(apiAddr), (cb) => cb())
4244
}
4345

4446
const node = new IPFS({

0 commit comments

Comments
 (0)