Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
feat: ipfs version flags + ipfs repo version (#1181)
Browse files Browse the repository at this point in the history
  • Loading branch information
JonKrone authored and daviddias committed Jan 25, 2018
1 parent d16a129 commit 54d47e1
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/repo/version.js
Expand Up @@ -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
}
Expand Down
32 changes: 26 additions & 6 deletions src/cli/commands/version.js
Expand Up @@ -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}`)
}
})
}
}
15 changes: 11 additions & 4 deletions src/core/components/version.js
Expand Up @@ -3,17 +3,24 @@
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') {
callback = opts
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: ''
})
})
})
}
16 changes: 16 additions & 0 deletions 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)
})
}
2 changes: 1 addition & 1 deletion src/http/api/routes/index.js
Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions src/http/api/routes/repo.js
Expand Up @@ -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
})
}
28 changes: 28 additions & 0 deletions 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`)
})
})
}))
37 changes: 37 additions & 0 deletions 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', () => {
Expand All @@ -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`)
})
})
}))

0 comments on commit 54d47e1

Please sign in to comment.