diff --git a/src/cli/commands/version.js b/src/cli/commands/version.js index 6d9ecb45d4..dc322616e0 100644 --- a/src/cli/commands/version.js +++ b/src/cli/commands/version.js @@ -11,26 +11,46 @@ module.exports = { number: { alias: 'n', type: 'boolean', - default: false + default: false, + describe: 'Print only the version number' }, commit: { type: 'boolean', - default: false + default: false, + describe: `Include the version's commit hash` }, repo: { type: 'boolean', - default: false + default: false, + describe: `Print only the repo's version number` + }, + all: { + type: 'boolean', + default: false, + describe: 'Print everything we have' } }, handler (argv) { - // TODO: handle argv.{repo|commit|number} - argv.ipfs.version((err, version) => { + argv.ipfs.version((err, ipfs) => { if (err) { throw err } - print(`js-ipfs version: ${version.version}`) + const withCommit = argv.all || argv.commit + const parsedVersion = `${ipfs.version}${withCommit ? `-${ipfs.commit}` : ''}` + + if (argv.repo) { + // go-ipfs prints only the number, even without the --number flag. + print(ipfs.repo) + } else if (argv.number) { + print(parsedVersion) + } else if (argv.all) { + print(`js-ipfs version: ${parsedVersion}`) + print(`Repo version: ${ipfs.repo}`) + } else { + print(`js-ipfs version: ${parsedVersion}`) + } }) } } diff --git a/src/core/components/version.js b/src/core/components/version.js index 1d1fcca9c5..0f3fa2c1e1 100644 --- a/src/core/components/version.js +++ b/src/core/components/version.js @@ -3,6 +3,7 @@ const pkg = require('../../../package.json') const promisify = require('promisify-es6') +// TODO add the commit hash of the current ipfs version to the response. module.exports = function version (self) { return promisify((opts, callback) => { if (typeof opts === 'function') { @@ -10,10 +11,16 @@ module.exports = function version (self) { opts = {} } - callback(null, { - version: pkg.version, - repo: '', - commit: '' + self.repo.version((err, repoVersion) => { + if (err) { + throw err + } + + callback(null, { + version: pkg.version, + repo: repoVersion, + commit: '' + }) }) }) } diff --git a/test/cli/repo.js b/test/cli/repo.js new file mode 100644 index 0000000000..dd1e29533d --- /dev/null +++ b/test/cli/repo.js @@ -0,0 +1,28 @@ +/* eslint-env mocha */ +'use strict' + +const fs = require('fs') +const path = require('path') +const expect = require('chai').expect +const runOnAndOff = require('../utils/on-and-off') + +function getRepoVersion (repoPath) { + const versionPath = path.join(repoPath, 'version') + return String(fs.readFileSync(versionPath)) +} + +describe('repo', () => runOnAndOff((thing) => { + let ipfs + let repoVersion + + before(() => { + ipfs = thing.ipfs + repoVersion = getRepoVersion(ipfs.repoPath) + }) + + it('get the repo version', () => { + return ipfs('repo version').then((out) => { + expect(out).to.eql(`${repoVersion}\n`) + }) + }) +})) diff --git a/test/cli/version.js b/test/cli/version.js index 44986adf77..ba86356088 100644 --- a/test/cli/version.js +++ b/test/cli/version.js @@ -1,15 +1,24 @@ /* eslint-env mocha */ 'use strict' +const fs = require('fs') +const path = require('path') const expect = require('chai').expect const pkgversion = require('../../package.json').version const runOnAndOff = require('../utils/on-and-off') +function getRepoVersion (repoPath) { + const versionPath = path.join(repoPath, 'version') + return String(fs.readFileSync(versionPath)) +} + describe('version', () => runOnAndOff((thing) => { let ipfs + let repoVersion before(() => { ipfs = thing.ipfs + repoVersion = getRepoVersion(ipfs.repoPath) }) it('get the version', () => { @@ -19,4 +28,32 @@ describe('version', () => runOnAndOff((thing) => { ) }) }) + + it('handles --number', () => { + return ipfs('version --number').then(out => + expect(out).to.eql(`${pkgversion}\n`) + ) + }) + + it('handles --commit', () => { + return ipfs('version --commit').then(out => + expect(out).to.eql(`js-ipfs version: ${pkgversion}-\n`) + ) + }) + + it('handles --all', () => { + return ipfs('version --all').then(out => + expect(out).to.include( + `js-ipfs version: ${pkgversion}- +Repo version: ${repoVersion} +` + ) + ) + }) + + it('handles --repo', () => { + return ipfs('version --repo').then(out => { + expect(out).to.eql(`${repoVersion}\n`) + }) + }) }))