From 335912dc0e2a5f9d3fb9a6f430176164e02c41fd Mon Sep 17 00:00:00 2001 From: Jonathan Date: Wed, 24 Jan 2018 20:01:25 -0500 Subject: [PATCH] feat: `ipfs version` flags + `ipfs repo version` (#1181) --- src/cli/commands/version.js | 32 +++++++++++++++++++++++------ src/core/components/version.js | 15 ++++++++++---- test/cli/repo.js | 28 +++++++++++++++++++++++++ test/cli/version.js | 37 ++++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 10 deletions(-) create mode 100644 test/cli/repo.js 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`) + }) + }) }))