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

Commit 94f26f0

Browse files
committed
Merge pull request #96 from noffle/init-cli-http
'ipfs init': CLI + tests
2 parents fb855cb + 21f875a commit 94f26f0

File tree

3 files changed

+121
-6
lines changed

3 files changed

+121
-6
lines changed

src/cli/commands/init.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
const Command = require('ronin').Command
2+
const IpfsRepo = require('ipfs-repo')
3+
const Ipfs = require('../../core')
4+
const fsBlobStore = require('fs-blob-store')
5+
const utils = require('../utils')
26

37
module.exports = Command.extend({
4-
desc: 'Initialize ipfs local configuration',
8+
desc: 'Initialize a local IPFS node',
59

610
options: {
711
bits: {
@@ -22,5 +26,30 @@ module.exports = Command.extend({
2226
}
2327
},
2428

25-
run: () => {}
29+
run: (bits, force, empty) => {
30+
const path = utils.getRepoPath()
31+
32+
const repo = new IpfsRepo(path, {
33+
stores: {
34+
keys: fsBlobStore,
35+
config: fsBlobStore,
36+
datastore: fsBlobStore,
37+
logs: fsBlobStore,
38+
locks: fsBlobStore,
39+
version: fsBlobStore
40+
}
41+
})
42+
43+
var ipfs = new Ipfs(repo)
44+
ipfs.init({
45+
bits: bits,
46+
force: force,
47+
emptyRepo: empty
48+
}, function (err, res) {
49+
if (err) {
50+
console.error(err.toString())
51+
process.exit(1)
52+
}
53+
})
54+
}
2655
})

src/cli/utils.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ log.error = debug('cli:error')
99

1010
exports = module.exports
1111

12-
const repoPath = process.env.IPFS_PATH || os.homedir() + '/.ipfs'
13-
1412
exports.isDaemonOn = isDaemonOn
1513
function isDaemonOn () {
1614
try {
17-
fs.readFileSync(repoPath + '/api')
15+
fs.readFileSync(exports.getRepoPath() + '/api')
1816
log('daemon is on')
1917
return true
2018
} catch (err) {
@@ -29,7 +27,7 @@ function getAPICtl () {
2927
throw new Error('daemon is not on')
3028
}
3129

32-
const apiAddr = multiaddr(fs.readFileSync(repoPath + '/api').toString())
30+
const apiAddr = multiaddr(fs.readFileSync(exports.getRepoPath() + '/api').toString())
3331
return APIctl(apiAddr.toString())
3432
}
3533

@@ -44,3 +42,7 @@ exports.getIPFS = (callback) => {
4442

4543
callback(null, getAPICtl())
4644
}
45+
46+
exports.getRepoPath = () => {
47+
return process.env.IPFS_PATH || os.homedir() + '/.ipfs'
48+
}

tests/test-cli/test-init.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/* eslint-env mocha */
2+
3+
const expect = require('chai').expect
4+
const nexpect = require('nexpect')
5+
const rimraf = require('rimraf')
6+
const path = require('path')
7+
const fs = require('fs')
8+
const utils = require('../../src/cli/utils')
9+
10+
function repoExistsSync (p) {
11+
return fs.existsSync(path.join(utils.getRepoPath(), p))
12+
}
13+
14+
describe('init', function () {
15+
this.timeout(10000)
16+
17+
var oldRepoPath = process.env.IPFS_PATH
18+
beforeEach((done) => {
19+
oldRepoPath = process.env.IPFS_PATH
20+
const repoPath = '/tmp/ipfs-test-' + Math.random().toString().substring(2, 8) + '/'
21+
process.env.IPFS_PATH = repoPath
22+
done()
23+
})
24+
25+
afterEach((done) => {
26+
rimraf(process.env.IPFS_PATH, (err) => {
27+
expect(err).to.not.exist
28+
process.env.IPFS_PATH = oldRepoPath
29+
done()
30+
})
31+
})
32+
33+
it('basic', (done) => {
34+
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'init'])
35+
.run((err, stdout, exitcode) => {
36+
expect(err).to.not.exist
37+
expect(repoExistsSync('blocks')).to.equal(true)
38+
expect(repoExistsSync('config')).to.equal(true)
39+
expect(repoExistsSync('version')).to.equal(true)
40+
done()
41+
})
42+
})
43+
44+
it('bits', (done) => {
45+
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'init', '--bits', '64'])
46+
.run((err, stdout, exitcode) => {
47+
expect(err).to.not.exist
48+
done()
49+
})
50+
})
51+
52+
it('empty', (done) => {
53+
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'init', '--bits', '64', '--empty-repo', 'true'])
54+
.run((err, stdout, exitcode) => {
55+
expect(err).to.not.exist
56+
expect(repoExistsSync('blocks')).to.equal(false)
57+
expect(repoExistsSync('config')).to.equal(true)
58+
expect(repoExistsSync('version')).to.equal(true)
59+
done()
60+
})
61+
})
62+
63+
it('force', (done) => {
64+
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'init', '--bits', '64', '--force'])
65+
.run((err, stdout, exitcode) => {
66+
expect(err).to.not.exist
67+
expect(exitcode).to.equal(0)
68+
69+
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'init', '--bits', '64'])
70+
.run((err, stdout, exitcode) => {
71+
expect(err).to.not.exist
72+
expect(exitcode).to.equal(1)
73+
74+
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'init', '--bits', '64', '--force'])
75+
.run((err, stdout, exitcode) => {
76+
expect(err).to.not.exist
77+
expect(exitcode).to.equal(0)
78+
done()
79+
})
80+
})
81+
})
82+
})
83+
})
84+

0 commit comments

Comments
 (0)