Skip to content

Commit

Permalink
feat: ipfs version flags + ipfs repo version (ipfs#1181) (ipfs#1188)
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias authored and JonKrone committed Feb 19, 2018
1 parent ba1157f commit ba09a56
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 16 deletions.
35 changes: 29 additions & 6 deletions src/cli/commands/version.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const os = require('os')
const print = require('../utils').print

module.exports = {
Expand All @@ -11,26 +12,48 @@ 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, data) => {
if (err) {
throw err
}

print(`js-ipfs version: ${version.version}`)
const withCommit = argv.all || argv.commit
const parsedVersion = `${data.version}${withCommit ? `-${data.commit}` : ''}`

if (argv.repo) {
// go-ipfs prints only the number, even without the --number flag.
print(data.repo)
} else if (argv.number) {
print(parsedVersion)
} else if (argv.all) {
print(`js-ipfs version: ${parsedVersion}`)
print(`Repo version: ${data.repo}`)
print(`System version: ${os.arch()}/${os.platform()}`)
print(`Node.js version: ${process.version}`)
} else {
print(`js-ipfs version: ${parsedVersion}`)
}
})
}
}
20 changes: 19 additions & 1 deletion src/core/components/repo.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
'use strict'

const promisify = require('promisify-es6')
const repoVersion = require('ipfs-repo').repoVersion

module.exports = function repo (self) {
return {
init: (bits, empty, callback) => {
// 1. check if repo already exists
},

/**
* If the repo has been initialized, report the current version.
* Otherwise report the version that would be initialized.
*
* @param {function(Error, Number)} [callback]
* @returns {undefined}
*/
version: promisify((callback) => {
self._repo.version.get(callback)
self._repo._isInitialized(err => {
if (err) {
if (/ENOENT|not yet initialized/.test(err.message)) {
// this repo has not been initialized
return callback(null, repoVersion)
}
return callback(err)
}

self._repo.version.get(callback)
})
}),

gc: () => {},
Expand Down
15 changes: 11 additions & 4 deletions src/core/components/version.js
Original file line number Diff line number Diff line change
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) {
callback(err)
}

callback(null, {
version: pkg.version,
repo: repoVersion,
commit: ''
})
})
})
}
2 changes: 1 addition & 1 deletion test/cli/files.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect
const fs = require('fs')
const expect = require('chai').expect
const path = require('path')
const compareDir = require('dir-compare').compareSync
const rimraf = require('rimraf').sync
Expand Down
21 changes: 21 additions & 0 deletions test/cli/repo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect
const repoVersion = require('ipfs-repo').repoVersion

const runOnAndOff = require('../utils/on-and-off')

describe('repo', () => runOnAndOff((thing) => {
let ipfs

before(() => {
ipfs = thing.ipfs
})

it('get the repo version', () => {
return ipfs('repo version').then((out) => {
expect(out).to.eql(`${repoVersion}\n`)
})
})
}))
53 changes: 50 additions & 3 deletions test/cli/version.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/* eslint max-nested-callbacks: ["error", 5] */
/* eslint-env mocha */
'use strict'

const os = require('os')
const expect = require('chai').expect
const repoVersion = require('ipfs-repo').repoVersion
const pkgversion = require('../../package.json').version
const runOnAndOff = require('../utils/on-and-off')

Expand All @@ -12,11 +15,55 @@ describe('version', () => runOnAndOff((thing) => {
ipfs = thing.ipfs
})

it('get the version', () => {
return ipfs('version').then((out) => {
it('get the version', () =>
ipfs('version').then(out =>
expect(out).to.eql(
`js-ipfs version: ${pkgversion}\n`
)
})
)
)

it('handles --number', () =>
ipfs('version --number').then(out =>
expect(out).to.eql(`${pkgversion}\n`)
)
)

it('handles --commit', () =>
ipfs('version --commit').then(out =>
expect(out).to.eql(`js-ipfs version: ${pkgversion}-\n`)
)
)

describe('handles --all', function () {
it('prints js-ipfs version', () =>
ipfs('version --all').then(out => {
expect(out).to.include(`js-ipfs version: ${pkgversion}`)
})
)

it('prints repo version', () =>
ipfs('version --all').then(out => {
expect(out).to.include(`Repo version: ${repoVersion}`)
})
)

it('prints arch/platform', () =>
ipfs('version --all').then(out => {
expect(out).to.include(`System version: ${os.arch()}/${os.platform()}`)
})
)

it('prints Node.js version', () =>
ipfs('version --all').then(out => {
expect(out).to.include(`Node.js version: ${process.version}`)
})
)
})

it('handles --repo', () =>
ipfs('version --repo').then(out =>
expect(out).to.eql(`${repoVersion}\n`)
)
)
}))
5 changes: 4 additions & 1 deletion test/sharness/t0010-basic-commands.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ test_expect_success "ipfs version output looks good" '

test_expect_success "ipfs version --all has all required fields" '
ipfs version --all > version_all.txt &&
grep "js-ipfs version" version_all.txt
grep "js-ipfs version" version_all.txt &&
grep "Repo version" version_all.txt &&
grep "System version" version_all.txt &&
grep "Node.js version" version_all.txt
'

test_expect_success "ipfs help succeeds" '
Expand Down

0 comments on commit ba09a56

Please sign in to comment.