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

Commit

Permalink
add cli:id to use the daemon if on + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Feb 23, 2016
1 parent bfc3ee6 commit 7350c0a
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 82 deletions.
29 changes: 21 additions & 8 deletions src/cli/commands/id.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const Command = require('ronin').Command
const IPFS = require('../../ipfs-core')
const debug = require('debug')
const log = debug('cli:id')
log.error = debug('cli:id:error')
const utils = require('../utils')
const log = debug('cli')
log.error = debug('cli:error')

module.exports = Command.extend({
desc: 'Shows IPFS Node ID info',
Expand All @@ -14,11 +15,23 @@ module.exports = Command.extend({
}
},

run: name => {
const node = new IPFS()
node.id((err, id) => {
if (err) { return log.error(err) }
console.log(id)
})
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)
})
}
}
})
32 changes: 32 additions & 0 deletions src/cli/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const fs = require('fs')
const os = require('os')
const APIctl = require('ipfs-api')
const multiaddr = require('multiaddr')
const debug = require('debug')
const log = debug('cli')
log.error = debug('cli:error')

exports = module.exports

const repoPath = process.env.IPFS_PATH || os.homedir() + '/.ipfs'

exports.isDaemonOn = isDaemonOn
function isDaemonOn () {
try {
fs.readFileSync(repoPath + '/api')
log('daemon is on')
return true
} catch (err) {
log('daemon is off')
return false
}
}

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

const apiAddr = multiaddr(fs.readFileSync(repoPath + '/api').toString())
return APIctl(apiAddr.toString())
}
14 changes: 0 additions & 14 deletions src/help-menu.js

This file was deleted.

2 changes: 1 addition & 1 deletion tests/test-cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('cli', () => {

const tests = fs.readdirSync(__dirname)
tests.filter(file => {
if (file === 'index.js') {
if (file === 'index.js' || file === 'api.js') {
return false
} else {
return true
Expand Down
15 changes: 1 addition & 14 deletions tests/test-cli/test-bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const expect = require('chai').expect
const nexpect = require('nexpect')
const httpAPI = require('../../src/http-api')

describe('id', () => {
describe('api offline', () => {
Expand Down Expand Up @@ -75,18 +74,6 @@ describe('id', () => {
})

describe('api running', () => {
before((done) => {
httpAPI.start(err => {
expect(err).to.not.exist
done()
})
})

after((done) => {
httpAPI.stop((err) => {
expect(err).to.not.exist
done()
})
})
// TODO
})
})
23 changes: 4 additions & 19 deletions tests/test-cli/test-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,10 @@

const expect = require('chai').expect
const nexpect = require('nexpect')
const httpAPI = require('../../src/http-api')
const fs = require('fs')

describe('config', () => {
describe('api offline', () => {
// TODO
})

describe('api running', () => {
before((done) => {
httpAPI.start(err => {
expect(err).to.not.exist
done()
})
})

after((done) => {
httpAPI.stop((err) => {
expect(err).to.not.exist
done()
})
})

const repoTests = require('./index').repoTests
const configPath = repoTests + '/config'

Expand Down Expand Up @@ -103,4 +84,8 @@ describe('config', () => {
})
})
})

describe('api running', () => {
// TODO
})
})
2 changes: 1 addition & 1 deletion tests/test-cli/test-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('id', () => {

describe('api running', () => {
before((done) => {
httpAPI.start(err => {
httpAPI.start((err) => {
expect(err).to.not.exist
done()
})
Expand Down
25 changes: 1 addition & 24 deletions tests/test-cli/test-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const expect = require('chai').expect
const nexpect = require('nexpect')
const httpAPI = require('../../src/http-api')

describe('version', () => {
describe('api offline', () => {
Expand All @@ -18,28 +17,6 @@ describe('version', () => {
})

describe('api running', () => {
before((done) => {
httpAPI.start(err => {
expect(err).to.not.exist
done()
})
})

after((done) => {
httpAPI.stop((err) => {
expect(err).to.not.exist
done()
})
})

it('get the version', done => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'version'])
.expect('0.4.0-dev')
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(exitcode).to.equal(0)
done()
})
})
// TODO
})
})
2 changes: 1 addition & 1 deletion tests/test-http-api/test-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('id', () => {
done()
})

it('get the version', (done) => {
it('get the id', (done) => {
ctl.id((err, result) => {
expect(err).to.not.exist
expect(result).to.deep.equal(idResult)
Expand Down

0 comments on commit 7350c0a

Please sign in to comment.