Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 15e95d7

Browse files
committedFeb 23, 2016
add cli:id to use the daemon if on + tests
1 parent bfc3ee6 commit 15e95d7

File tree

8 files changed

+61
-81
lines changed

8 files changed

+61
-81
lines changed
 

‎src/cli/commands/id.js

+21-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
const Command = require('ronin').Command
22
const IPFS = require('../../ipfs-core')
33
const debug = require('debug')
4-
const log = debug('cli:id')
5-
log.error = debug('cli:id:error')
4+
const utils = require('../utils')
5+
const log = debug('cli')
6+
log.error = debug('cli:error')
67

78
module.exports = Command.extend({
89
desc: 'Shows IPFS Node ID info',
@@ -14,11 +15,23 @@ module.exports = Command.extend({
1415
}
1516
},
1617

17-
run: name => {
18-
const node = new IPFS()
19-
node.id((err, id) => {
20-
if (err) { return log.error(err) }
21-
console.log(id)
22-
})
18+
run: (name) => {
19+
if (utils.isDaemonOn()) {
20+
const ctl = utils.getAPICtl()
21+
ctl.id((err, result) => {
22+
if (err) {
23+
return log.error(err)
24+
}
25+
console.log(result)
26+
})
27+
} else {
28+
const node = new IPFS()
29+
node.id((err, id) => {
30+
if (err) {
31+
return log.error(err)
32+
}
33+
console.log(id)
34+
})
35+
}
2336
}
2437
})

‎src/cli/utils.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const fs = require('fs')
2+
const os = require('os')
3+
const APIctl = require('ipfs-api')
4+
const multiaddr = require('multiaddr')
5+
const debug = require('debug')
6+
const log = debug('cli')
7+
log.error = debug('cli:error')
8+
9+
exports = module.exports
10+
11+
const repoPath = process.env.IPFS_PATH || os.homedir() + '/.ipfs'
12+
13+
exports.isDaemonOn = isDaemonOn
14+
function isDaemonOn () {
15+
try {
16+
fs.readFileSync(repoPath + '/api')
17+
log('daemon is on')
18+
return true
19+
} catch (err) {
20+
log('daemon is off')
21+
return false
22+
}
23+
}
24+
25+
exports.getAPICtl = () => {
26+
if (!isDaemonOn) {
27+
throw new Error('daemon is not on')
28+
}
29+
30+
const apiAddr = multiaddr(fs.readFileSync(repoPath + '/api').toString())
31+
return APIctl(apiAddr.toString())
32+
}

‎src/help-menu.js

-14
This file was deleted.

‎tests/test-cli/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('cli', () => {
2626

2727
const tests = fs.readdirSync(__dirname)
2828
tests.filter(file => {
29-
if (file === 'index.js') {
29+
if (file === 'index.js' || file === 'api.js') {
3030
return false
3131
} else {
3232
return true

‎tests/test-cli/test-bootstrap.js

+1-14
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

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

76
describe('id', () => {
87
describe('api offline', () => {
@@ -75,18 +74,6 @@ describe('id', () => {
7574
})
7675

7776
describe('api running', () => {
78-
before((done) => {
79-
httpAPI.start(err => {
80-
expect(err).to.not.exist
81-
done()
82-
})
83-
})
84-
85-
after((done) => {
86-
httpAPI.stop((err) => {
87-
expect(err).to.not.exist
88-
done()
89-
})
90-
})
77+
// TODO
9178
})
9279
})

‎tests/test-cli/test-config.js

+4-19
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,10 @@
22

33
const expect = require('chai').expect
44
const nexpect = require('nexpect')
5-
const httpAPI = require('../../src/http-api')
65
const fs = require('fs')
76

87
describe('config', () => {
98
describe('api offline', () => {
10-
// TODO
11-
})
12-
13-
describe('api running', () => {
14-
before((done) => {
15-
httpAPI.start(err => {
16-
expect(err).to.not.exist
17-
done()
18-
})
19-
})
20-
21-
after((done) => {
22-
httpAPI.stop((err) => {
23-
expect(err).to.not.exist
24-
done()
25-
})
26-
})
27-
289
const repoTests = require('./index').repoTests
2910
const configPath = repoTests + '/config'
3011

@@ -103,4 +84,8 @@ describe('config', () => {
10384
})
10485
})
10586
})
87+
88+
describe('api running', () => {
89+
// TODO
90+
})
10691
})

‎tests/test-cli/test-id.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('id', () => {
3535

3636
describe('api running', () => {
3737
before((done) => {
38-
httpAPI.start(err => {
38+
httpAPI.start((err) => {
3939
expect(err).to.not.exist
4040
done()
4141
})

‎tests/test-cli/test-version.js

+1-24
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

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

76
describe('version', () => {
87
describe('api offline', () => {
@@ -18,28 +17,6 @@ describe('version', () => {
1817
})
1918

2019
describe('api running', () => {
21-
before((done) => {
22-
httpAPI.start(err => {
23-
expect(err).to.not.exist
24-
done()
25-
})
26-
})
27-
28-
after((done) => {
29-
httpAPI.stop((err) => {
30-
expect(err).to.not.exist
31-
done()
32-
})
33-
})
34-
35-
it('get the version', done => {
36-
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'version'])
37-
.expect('0.4.0-dev')
38-
.run((err, stdout, exitcode) => {
39-
expect(err).to.not.exist
40-
expect(exitcode).to.equal(0)
41-
done()
42-
})
43-
})
20+
// TODO
4421
})
4522
})

0 commit comments

Comments
 (0)
This repository has been archived.