From 54d47e15504bc51ddc5e93a1a91b3022111c585c 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) --- package.json | 2 +- src/cli/commands/repo/version.js | 2 +- src/cli/commands/version.js | 32 +++++++++++++++++++++------ src/core/components/version.js | 15 +++++++++---- src/http/api/resources/repo.js | 16 ++++++++++++++ src/http/api/routes/index.js | 2 +- src/http/api/routes/repo.js | 5 ++--- test/cli/repo.js | 28 ++++++++++++++++++++++++ test/cli/version.js | 37 ++++++++++++++++++++++++++++++++ 9 files changed, 123 insertions(+), 16 deletions(-) create mode 100644 test/cli/repo.js diff --git a/package.json b/package.json index dc7d2a1519..db2bdac274 100644 --- a/package.json +++ b/package.json @@ -105,7 +105,7 @@ "hapi": "^16.6.2", "hapi-set-header": "^1.0.2", "hoek": "^5.0.2", - "ipfs-api": "^17.3.0", + "ipfs-api": "^17.5.0", "ipfs-bitswap": "~0.18.0", "ipfs-block": "~0.6.1", "ipfs-block-service": "~0.13.0", diff --git a/src/cli/commands/repo/version.js b/src/cli/commands/repo/version.js index 462a571da2..ca3491cf7a 100644 --- a/src/cli/commands/repo/version.js +++ b/src/cli/commands/repo/version.js @@ -10,7 +10,7 @@ module.exports = { builder: {}, handler (argv) { - argv.ipfs.repo.version(function (err, version) { + argv.ipfs.repo.version((err, version) => { if (err) { throw err } 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/src/http/api/resources/repo.js b/src/http/api/resources/repo.js index ccacec309b..9650b4141a 100644 --- a/src/http/api/resources/repo.js +++ b/src/http/api/resources/repo.js @@ -1 +1,17 @@ 'use strict' + +const boom = require('boom') + +exports = module.exports + +exports.version = (request, reply) => { + const ipfs = request.server.app.ipfs + + ipfs.repo.version((err, version) => { + if (err) { + return reply(boom.badRequest(err)) + } + + reply(version) + }) +} diff --git a/src/http/api/routes/index.js b/src/http/api/routes/index.js index 908c0c0878..3cca6d5d32 100644 --- a/src/http/api/routes/index.js +++ b/src/http/api/routes/index.js @@ -6,7 +6,7 @@ module.exports = (server) => { require('./bootstrap')(server) require('./block')(server) require('./object')(server) - // require('./repo')(server) + require('./repo')(server) require('./config')(server) require('./swarm')(server) require('./bitswap')(server) diff --git a/src/http/api/routes/repo.js b/src/http/api/routes/repo.js index f42f03bfc1..1410211a93 100644 --- a/src/http/api/routes/repo.js +++ b/src/http/api/routes/repo.js @@ -2,13 +2,12 @@ const resources = require('./../resources') -// TODO module.exports = (server) => { const api = server.select('API') api.route({ method: '*', - path: '/api/v0/repo', - handler: resources.repo + path: '/api/v0/repo/version', + handler: resources.repo.version }) } 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`) + }) + }) }))