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

Commit c46e055

Browse files
committed
Merge pull request #83 from ipfs/cli-utils
Add getIPFS util and refactor id command to use it
2 parents 64073cb + 58bdca3 commit c46e055

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

src/cli/commands/id.js

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const Command = require('ronin').Command
2-
const IPFS = require('../../ipfs-core')
32
const debug = require('debug')
43
const utils = require('../utils')
54
const log = debug('cli')
@@ -16,22 +15,13 @@ module.exports = Command.extend({
1615
},
1716

1817
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-
}
18+
var ipfs = utils.getIPFS()
19+
20+
ipfs.id((err, id) => {
21+
if (err) {
22+
return log.error(err)
23+
}
24+
console.log(id)
25+
})
3626
}
3727
})

src/cli/utils.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const fs = require('fs')
22
const os = require('os')
33
const APIctl = require('ipfs-api')
44
const multiaddr = require('multiaddr')
5+
const IPFS = require('../ipfs-core')
56
const debug = require('debug')
67
const log = debug('cli')
78
log.error = debug('cli:error')
@@ -22,11 +23,20 @@ function isDaemonOn () {
2223
}
2324
}
2425

25-
exports.getAPICtl = () => {
26-
if (!isDaemonOn) {
26+
exports.getAPICtl = getAPICtl
27+
function getAPICtl () {
28+
if (!isDaemonOn()) {
2729
throw new Error('daemon is not on')
2830
}
2931

3032
const apiAddr = multiaddr(fs.readFileSync(repoPath + '/api').toString())
3133
return APIctl(apiAddr.toString())
3234
}
35+
36+
exports.getIPFS = () => {
37+
if (!isDaemonOn()) {
38+
return new IPFS()
39+
}
40+
41+
return getAPICtl()
42+
}

tests/test-cli/test-bootstrap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const expect = require('chai').expect
44
const nexpect = require('nexpect')
55

6-
describe('id', () => {
6+
describe('bootstrap', () => {
77
describe('api offline', () => {
88
const defaultList = [
99
'/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',

0 commit comments

Comments
 (0)