From 49b77b465434f1f9ac71ee082b05466909ea0a72 Mon Sep 17 00:00:00 2001 From: Stephen Whitmore Date: Thu, 24 Mar 2016 11:22:45 -0700 Subject: [PATCH 1/6] early init cli work --- src/cli/commands/init.js | 38 +++++++++++++++++++++++++-- tests/test-cli/test-init.js | 52 +++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 tests/test-cli/test-init.js diff --git a/src/cli/commands/init.js b/src/cli/commands/init.js index fc85528eb2..b1d8735e47 100644 --- a/src/cli/commands/init.js +++ b/src/cli/commands/init.js @@ -1,7 +1,9 @@ const Command = require('ronin').Command +const IpfsRepo = require('ipfs-repo') +const Ipfs = require('../../core') module.exports = Command.extend({ - desc: 'Initialize ipfs local configuration', + desc: 'Initialize a local IPFS local node', options: { bits: { @@ -22,5 +24,37 @@ module.exports = Command.extend({ } }, - run: () => {} + run: (bits, force, empty) => { + // TODO: what blob store do I use for browser? indexdb, right? + // well, this IS cli, and there's no browser cli :P + var someBlobStore = require('fs-blob-store') + + // TODO: --force + --empty-repo will keep your default assets, right? + // TODO: where to init at? $IPFS_PATH var? homedir/.ipfs otherwise? sounds like a helper method job + const repo = new IpfsRepo('/tmp/my-little-repo', { + stores: { + keys: someBlobStore, + config: someBlobStore, + datastore: someBlobStore, + logs: someBlobStore, + locks: someBlobStore, + version: someBlobStore + } + }) + + 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) + } + + // TODO: what console output is desirable? any? mimick go-ipfs? + console.log('res', res) + }) + } }) diff --git a/tests/test-cli/test-init.js b/tests/test-cli/test-init.js new file mode 100644 index 0000000000..785a8d35e4 --- /dev/null +++ b/tests/test-cli/test-init.js @@ -0,0 +1,52 @@ +/* eslint-env mocha */ + +const expect = require('chai').expect +const nexpect = require('nexpect') + +describe('init', () => { + it('basic', (done) => { + nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'init']) + .run((err, stdout, exitcode) => { + expect(err).to.not.exist + 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 + 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() + }) + }) + }) + }) +}) + From 8f757fb80eafaaf7f11e3871e4b799387ed95ef5 Mon Sep 17 00:00:00 2001 From: Stephen Whitmore Date: Thu, 24 Mar 2016 11:40:50 -0700 Subject: [PATCH 2/6] minor cleanup --- src/cli/commands/init.js | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/src/cli/commands/init.js b/src/cli/commands/init.js index b1d8735e47..ce561f2aab 100644 --- a/src/cli/commands/init.js +++ b/src/cli/commands/init.js @@ -1,6 +1,7 @@ const Command = require('ronin').Command const IpfsRepo = require('ipfs-repo') const Ipfs = require('../../core') +const fsBlobStore = require('fs-blob-store') module.exports = Command.extend({ desc: 'Initialize a local IPFS local node', @@ -25,20 +26,14 @@ module.exports = Command.extend({ }, run: (bits, force, empty) => { - // TODO: what blob store do I use for browser? indexdb, right? - // well, this IS cli, and there's no browser cli :P - var someBlobStore = require('fs-blob-store') - - // TODO: --force + --empty-repo will keep your default assets, right? - // TODO: where to init at? $IPFS_PATH var? homedir/.ipfs otherwise? sounds like a helper method job const repo = new IpfsRepo('/tmp/my-little-repo', { stores: { - keys: someBlobStore, - config: someBlobStore, - datastore: someBlobStore, - logs: someBlobStore, - locks: someBlobStore, - version: someBlobStore + keys: fsBlobStore, + config: fsBlobStore, + datastore: fsBlobStore, + logs: fsBlobStore, + locks: fsBlobStore, + version: fsBlobStore } }) @@ -52,9 +47,6 @@ module.exports = Command.extend({ console.error(err.toString()) process.exit(1) } - - // TODO: what console output is desirable? any? mimick go-ipfs? - console.log('res', res) }) } }) From de7779093f68fe981ec94e54127232865850ab3e Mon Sep 17 00:00:00 2001 From: Stephen Whitmore Date: Thu, 24 Mar 2016 13:21:49 -0700 Subject: [PATCH 3/6] cli init tests /w cleanup --- src/cli/commands/init.js | 5 ++++- src/cli/utils.js | 4 ++++ tests/test-cli/test-init.js | 22 +++++++++++++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/cli/commands/init.js b/src/cli/commands/init.js index ce561f2aab..281cb6febc 100644 --- a/src/cli/commands/init.js +++ b/src/cli/commands/init.js @@ -2,6 +2,7 @@ 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 a local IPFS local node', @@ -26,7 +27,9 @@ module.exports = Command.extend({ }, run: (bits, force, empty) => { - const repo = new IpfsRepo('/tmp/my-little-repo', { + const path = utils.getRepoPath() + + const repo = new IpfsRepo(path, { stores: { keys: fsBlobStore, config: fsBlobStore, diff --git a/src/cli/utils.js b/src/cli/utils.js index c6e43daf4c..d11fff4424 100644 --- a/src/cli/utils.js +++ b/src/cli/utils.js @@ -44,3 +44,7 @@ exports.getIPFS = (callback) => { callback(null, getAPICtl()) } + +exports.getRepoPath = () => { + return repoPath +} diff --git a/tests/test-cli/test-init.js b/tests/test-cli/test-init.js index 785a8d35e4..9cf6b2304a 100644 --- a/tests/test-cli/test-init.js +++ b/tests/test-cli/test-init.js @@ -2,8 +2,28 @@ const expect = require('chai').expect const nexpect = require('nexpect') +const rimraf = require('rimraf') + +describe('init', function () { + this.timeout(10000) + + var oldRepoPath = process.env.IPFS_PATH + before((done) => { + oldRepoPath = process.env.IPFS_PATH + console.log('old', oldRepoPath) + const repoPath = '/tmp/ipfs-test-' + Math.random().toString().substring(2, 8) + '/' + process.env.IPFS_PATH = repoPath + done() + }) + + after((done) => { + rimraf(process.env.IPFS_PATH, (err) => { + expect(err).to.not.exist + process.env.IPFS_PATH = oldRepoPath + done() + }) + }) -describe('init', () => { it('basic', (done) => { nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'init']) .run((err, stdout, exitcode) => { From 01eb084cc2e4d1ee6a79f84f77db8cd280180abf Mon Sep 17 00:00:00 2001 From: Stephen Whitmore Date: Thu, 24 Mar 2016 15:50:26 -0700 Subject: [PATCH 4/6] check for repo files during init tests --- src/cli/commands/init.js | 2 +- tests/test-cli/test-init.js | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/cli/commands/init.js b/src/cli/commands/init.js index 281cb6febc..dcde142265 100644 --- a/src/cli/commands/init.js +++ b/src/cli/commands/init.js @@ -5,7 +5,7 @@ const fsBlobStore = require('fs-blob-store') const utils = require('../utils') module.exports = Command.extend({ - desc: 'Initialize a local IPFS local node', + desc: 'Initialize a local IPFS node', options: { bits: { diff --git a/tests/test-cli/test-init.js b/tests/test-cli/test-init.js index 9cf6b2304a..9870bc21bd 100644 --- a/tests/test-cli/test-init.js +++ b/tests/test-cli/test-init.js @@ -3,20 +3,25 @@ const expect = require('chai').expect const nexpect = require('nexpect') const rimraf = require('rimraf') +const path = require('path') +const fs = require('fs') + +function repoExistsSync (p) { + return fs.existsSync(path.join(process.env.IPFS_PATH, p)) +} describe('init', function () { this.timeout(10000) var oldRepoPath = process.env.IPFS_PATH - before((done) => { + beforeEach((done) => { oldRepoPath = process.env.IPFS_PATH - console.log('old', oldRepoPath) const repoPath = '/tmp/ipfs-test-' + Math.random().toString().substring(2, 8) + '/' process.env.IPFS_PATH = repoPath done() }) - after((done) => { + afterEach((done) => { rimraf(process.env.IPFS_PATH, (err) => { expect(err).to.not.exist process.env.IPFS_PATH = oldRepoPath @@ -28,6 +33,9 @@ describe('init', function () { 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() }) }) @@ -44,6 +52,9 @@ describe('init', function () { 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() }) }) From 583e19094546dcec8379784f0caf71705963ae5a Mon Sep 17 00:00:00 2001 From: Stephen Whitmore Date: Thu, 24 Mar 2016 16:28:02 -0700 Subject: [PATCH 5/6] check the repo path each time --- src/cli/utils.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/cli/utils.js b/src/cli/utils.js index d11fff4424..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()) } @@ -46,5 +44,5 @@ exports.getIPFS = (callback) => { } exports.getRepoPath = () => { - return repoPath + return process.env.IPFS_PATH || os.homedir() + '/.ipfs' } From 21f875a4397111d81b31ee052b48bc8b3d65fd3a Mon Sep 17 00:00:00 2001 From: Stephen Whitmore Date: Thu, 24 Mar 2016 16:38:00 -0700 Subject: [PATCH 6/6] uses utils repo path --- tests/test-cli/test-init.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test-cli/test-init.js b/tests/test-cli/test-init.js index 9870bc21bd..29b3a54d69 100644 --- a/tests/test-cli/test-init.js +++ b/tests/test-cli/test-init.js @@ -5,9 +5,10 @@ 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(process.env.IPFS_PATH, p)) + return fs.existsSync(path.join(utils.getRepoPath(), p)) } describe('init', function () {