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

Commit

Permalink
feat: cli --api option (#1087)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgte authored and daviddias committed Nov 17, 2017
1 parent b2106c1 commit 1b1fa05
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
30 changes: 19 additions & 11 deletions src/cli/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
})
})
})
})
})
}
18 changes: 10 additions & 8 deletions src/cli/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 1b1fa05

Please sign in to comment.