From 1b1fa054e322247c7cafe0a31967f4cdd8b6daa3 Mon Sep 17 00:00:00 2001 From: Pedro Teixeira Date: Fri, 17 Nov 2017 22:13:32 +0000 Subject: [PATCH] feat: cli --api option (#1087) --- src/cli/bin.js | 30 +++++++++++++++++++----------- src/cli/utils.js | 18 ++++++++++-------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/cli/bin.js b/src/cli/bin.js index c9a72f2aa1..3ea4ecc812 100755 --- a/src/cli/bin.js +++ b/src/cli/bin.js @@ -52,19 +52,27 @@ if (args[0] === 'daemon' || args[0] === 'init') { .completion() .parse(args) } else { - utils.getIPFS((err, ipfs, cleanup) => { - if (err) { throw err } + // here we have to make a separate yargs instance with + // only the `api` option because we need this before doing + // the final yargs parse where the command handler is invoked.. + yargs().option('api').parse(process.argv, (err, argv, output) => { + if (err) { + throw err + } + utils.getIPFS(argv.api, (err, ipfs, cleanup) => { + if (err) { throw err } - cli - .help() - .strict(false) - .completion() - .parse(args, { ipfs: ipfs }, (err, argv, output) => { - if (output) { print(output) } + cli + .help() + .strict(false) + .completion() + .parse(args, { ipfs: ipfs }, (err, argv, output) => { + if (output) { print(output) } - cleanup(() => { - if (err) { throw err } + cleanup(() => { + if (err) { throw err } + }) }) - }) + }) }) } diff --git a/src/cli/utils.js b/src/cli/utils.js index d44bf4b4fd..b3f2f85e9f 100644 --- a/src/cli/utils.js +++ b/src/cli/utils.js @@ -27,18 +27,20 @@ function isDaemonOn () { } exports.getAPICtl = getAPICtl -function getAPICtl () { - if (!isDaemonOn()) { +function getAPICtl (apiAddr) { + if (!apiAddr && !isDaemonOn()) { throw new Error('daemon is not on') } - const apiPath = path.join(exports.getRepoPath(), 'api') - const apiAddr = multiaddr(fs.readFileSync(apiPath).toString()) - return APIctl(apiAddr.toString()) + if (!apiAddr) { + const apiPath = path.join(exports.getRepoPath(), 'api') + apiAddr = multiaddr(fs.readFileSync(apiPath).toString()).toString() + } + return APIctl(apiAddr) } -exports.getIPFS = (callback) => { - if (isDaemonOn()) { - return callback(null, getAPICtl(), (cb) => cb()) +exports.getIPFS = (apiAddr, callback) => { + if (apiAddr || isDaemonOn()) { + return callback(null, getAPICtl(apiAddr), (cb) => cb()) } const node = new IPFS({