diff --git a/src/cli/commands/init.js b/src/cli/commands/init.js index fc85528eb2..dcde142265 100644 --- a/src/cli/commands/init.js +++ b/src/cli/commands/init.js @@ -1,7 +1,11 @@ const Command = require('ronin').Command +const IpfsRepo = require('ipfs-repo') +const Ipfs = require('../../core') +const fsBlobStore = require('fs-blob-store') +const utils = require('../utils') module.exports = Command.extend({ - desc: 'Initialize ipfs local configuration', + desc: 'Initialize a local IPFS node', options: { bits: { @@ -22,5 +26,30 @@ module.exports = Command.extend({ } }, - run: () => {} + run: (bits, force, empty) => { + const path = utils.getRepoPath() + + const repo = new IpfsRepo(path, { + stores: { + keys: fsBlobStore, + config: fsBlobStore, + datastore: fsBlobStore, + logs: fsBlobStore, + locks: fsBlobStore, + version: fsBlobStore + } + }) + + var ipfs = new Ipfs(repo) + ipfs.init({ + bits: bits, + force: force, + emptyRepo: empty + }, function (err, res) { + if (err) { + console.error(err.toString()) + process.exit(1) + } + }) + } }) diff --git a/src/cli/utils.js b/src/cli/utils.js index c6e43daf4c..d714da286b 100644 --- a/src/cli/utils.js +++ b/src/cli/utils.js @@ -9,12 +9,10 @@ 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') + fs.readFileSync(exports.getRepoPath() + '/api') log('daemon is on') return true } catch (err) { @@ -29,7 +27,7 @@ function getAPICtl () { throw new Error('daemon is not on') } - const apiAddr = multiaddr(fs.readFileSync(repoPath + '/api').toString()) + const apiAddr = multiaddr(fs.readFileSync(exports.getRepoPath() + '/api').toString()) return APIctl(apiAddr.toString()) } @@ -44,3 +42,7 @@ exports.getIPFS = (callback) => { callback(null, getAPICtl()) } + +exports.getRepoPath = () => { + return process.env.IPFS_PATH || os.homedir() + '/.ipfs' +} diff --git a/tests/test-cli/test-init.js b/tests/test-cli/test-init.js new file mode 100644 index 0000000000..29b3a54d69 --- /dev/null +++ b/tests/test-cli/test-init.js @@ -0,0 +1,84 @@ +/* eslint-env mocha */ + +const expect = require('chai').expect +const nexpect = require('nexpect') +const rimraf = require('rimraf') +const path = require('path') +const fs = require('fs') +const utils = require('../../src/cli/utils') + +function repoExistsSync (p) { + return fs.existsSync(path.join(utils.getRepoPath(), p)) +} + +describe('init', function () { + this.timeout(10000) + + var oldRepoPath = process.env.IPFS_PATH + beforeEach((done) => { + oldRepoPath = process.env.IPFS_PATH + const repoPath = '/tmp/ipfs-test-' + Math.random().toString().substring(2, 8) + '/' + process.env.IPFS_PATH = repoPath + done() + }) + + afterEach((done) => { + rimraf(process.env.IPFS_PATH, (err) => { + expect(err).to.not.exist + process.env.IPFS_PATH = oldRepoPath + done() + }) + }) + + it('basic', (done) => { + nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'init']) + .run((err, stdout, exitcode) => { + expect(err).to.not.exist + expect(repoExistsSync('blocks')).to.equal(true) + expect(repoExistsSync('config')).to.equal(true) + expect(repoExistsSync('version')).to.equal(true) + done() + }) + }) + + it('bits', (done) => { + nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'init', '--bits', '64']) + .run((err, stdout, exitcode) => { + expect(err).to.not.exist + done() + }) + }) + + it('empty', (done) => { + nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'init', '--bits', '64', '--empty-repo', 'true']) + .run((err, stdout, exitcode) => { + expect(err).to.not.exist + expect(repoExistsSync('blocks')).to.equal(false) + expect(repoExistsSync('config')).to.equal(true) + expect(repoExistsSync('version')).to.equal(true) + done() + }) + }) + + it('force', (done) => { + nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'init', '--bits', '64', '--force']) + .run((err, stdout, exitcode) => { + expect(err).to.not.exist + expect(exitcode).to.equal(0) + + nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'init', '--bits', '64']) + .run((err, stdout, exitcode) => { + expect(err).to.not.exist + expect(exitcode).to.equal(1) + + nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'init', '--bits', '64', '--force']) + .run((err, stdout, exitcode) => { + expect(err).to.not.exist + expect(exitcode).to.equal(0) + done() + }) + }) + }) + }) +}) +