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

Commit

Permalink
Merge pull request #83 from ipfs/cli-utils
Browse files Browse the repository at this point in the history
Add getIPFS util and refactor id command to use it
  • Loading branch information
daviddias committed Mar 12, 2016
2 parents 64073cb + 58bdca3 commit c46e055
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
26 changes: 8 additions & 18 deletions src/cli/commands/id.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const Command = require('ronin').Command
const IPFS = require('../../ipfs-core')
const debug = require('debug')
const utils = require('../utils')
const log = debug('cli')
Expand All @@ -16,22 +15,13 @@ module.exports = Command.extend({
},

run: (name) => {
if (utils.isDaemonOn()) {
const ctl = utils.getAPICtl()
ctl.id((err, result) => {
if (err) {
return log.error(err)
}
console.log(result)
})
} else {
const node = new IPFS()
node.id((err, id) => {
if (err) {
return log.error(err)
}
console.log(id)
})
}
var ipfs = utils.getIPFS()

ipfs.id((err, id) => {
if (err) {
return log.error(err)
}
console.log(id)
})
}
})
14 changes: 12 additions & 2 deletions src/cli/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fs = require('fs')
const os = require('os')
const APIctl = require('ipfs-api')
const multiaddr = require('multiaddr')
const IPFS = require('../ipfs-core')
const debug = require('debug')
const log = debug('cli')
log.error = debug('cli:error')
Expand All @@ -22,11 +23,20 @@ function isDaemonOn () {
}
}

exports.getAPICtl = () => {
if (!isDaemonOn) {
exports.getAPICtl = getAPICtl
function getAPICtl () {
if (!isDaemonOn()) {
throw new Error('daemon is not on')
}

const apiAddr = multiaddr(fs.readFileSync(repoPath + '/api').toString())
return APIctl(apiAddr.toString())
}

exports.getIPFS = () => {
if (!isDaemonOn()) {
return new IPFS()
}

return getAPICtl()
}
2 changes: 1 addition & 1 deletion tests/test-cli/test-bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const expect = require('chai').expect
const nexpect = require('nexpect')

describe('id', () => {
describe('bootstrap', () => {
describe('api offline', () => {
const defaultList = [
'/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
Expand Down

0 comments on commit c46e055

Please sign in to comment.