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 Feb 17, 2018
1 parent 698b5b6 commit 335912d
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 10 deletions.
32 changes: 26 additions & 6 deletions src/cli/commands/version.js
Original file line number Diff line number Diff line change
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
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) {
throw err
}

callback(null, {
version: pkg.version,
repo: repoVersion,
commit: ''
})
})
})
}
28 changes: 28 additions & 0 deletions test/cli/repo.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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 335912d

Please sign in to comment.