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

Commit ce1620a

Browse files
committed
feat: ipfs version flags + ipfs repo version (#1181)
1 parent 905bdc0 commit ce1620a

File tree

4 files changed

+102
-10
lines changed

4 files changed

+102
-10
lines changed

src/cli/commands/version.js

+26-6
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,46 @@ module.exports = {
1111
number: {
1212
alias: 'n',
1313
type: 'boolean',
14-
default: false
14+
default: false,
15+
describe: 'Print only the version number'
1516
},
1617
commit: {
1718
type: 'boolean',
18-
default: false
19+
default: false,
20+
describe: `Include the version's commit hash`
1921
},
2022
repo: {
2123
type: 'boolean',
22-
default: false
24+
default: false,
25+
describe: `Print only the repo's version number`
26+
},
27+
all: {
28+
type: 'boolean',
29+
default: false,
30+
describe: 'Print everything we have'
2331
}
2432
},
2533

2634
handler (argv) {
27-
// TODO: handle argv.{repo|commit|number}
28-
argv.ipfs.version((err, version) => {
35+
argv.ipfs.version((err, ipfs) => {
2936
if (err) {
3037
throw err
3138
}
3239

33-
print(`js-ipfs version: ${version.version}`)
40+
const withCommit = argv.all || argv.commit
41+
const parsedVersion = `${ipfs.version}${withCommit ? `-${ipfs.commit}` : ''}`
42+
43+
if (argv.repo) {
44+
// go-ipfs prints only the number, even without the --number flag.
45+
print(ipfs.repo)
46+
} else if (argv.number) {
47+
print(parsedVersion)
48+
} else if (argv.all) {
49+
print(`js-ipfs version: ${parsedVersion}`)
50+
print(`Repo version: ${ipfs.repo}`)
51+
} else {
52+
print(`js-ipfs version: ${parsedVersion}`)
53+
}
3454
})
3555
}
3656
}

src/core/components/version.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,24 @@
33
const pkg = require('../../../package.json')
44
const promisify = require('promisify-es6')
55

6+
// TODO add the commit hash of the current ipfs version to the response.
67
module.exports = function version (self) {
78
return promisify((opts, callback) => {
89
if (typeof opts === 'function') {
910
callback = opts
1011
opts = {}
1112
}
1213

13-
callback(null, {
14-
version: pkg.version,
15-
repo: '',
16-
commit: ''
14+
self.repo.version((err, repoVersion) => {
15+
if (err) {
16+
throw err
17+
}
18+
19+
callback(null, {
20+
version: pkg.version,
21+
repo: repoVersion,
22+
commit: ''
23+
})
1724
})
1825
})
1926
}

test/cli/repo.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const fs = require('fs')
5+
const path = require('path')
6+
const expect = require('chai').expect
7+
const runOnAndOff = require('../utils/on-and-off')
8+
9+
function getRepoVersion (repoPath) {
10+
const versionPath = path.join(repoPath, 'version')
11+
return String(fs.readFileSync(versionPath))
12+
}
13+
14+
describe('repo', () => runOnAndOff((thing) => {
15+
let ipfs
16+
let repoVersion
17+
18+
before(() => {
19+
ipfs = thing.ipfs
20+
repoVersion = getRepoVersion(ipfs.repoPath)
21+
})
22+
23+
it('get the repo version', () => {
24+
return ipfs('repo version').then((out) => {
25+
expect(out).to.eql(`${repoVersion}\n`)
26+
})
27+
})
28+
}))

test/cli/version.js

+37
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
/* eslint-env mocha */
22
'use strict'
33

4+
const fs = require('fs')
5+
const path = require('path')
46
const expect = require('chai').expect
57
const pkgversion = require('../../package.json').version
68
const runOnAndOff = require('../utils/on-and-off')
79

10+
function getRepoVersion (repoPath) {
11+
const versionPath = path.join(repoPath, 'version')
12+
return String(fs.readFileSync(versionPath))
13+
}
14+
815
describe('version', () => runOnAndOff((thing) => {
916
let ipfs
17+
let repoVersion
1018

1119
before(() => {
1220
ipfs = thing.ipfs
21+
repoVersion = getRepoVersion(ipfs.repoPath)
1322
})
1423

1524
it('get the version', () => {
@@ -19,4 +28,32 @@ describe('version', () => runOnAndOff((thing) => {
1928
)
2029
})
2130
})
31+
32+
it('handles --number', () => {
33+
return ipfs('version --number').then(out =>
34+
expect(out).to.eql(`${pkgversion}\n`)
35+
)
36+
})
37+
38+
it('handles --commit', () => {
39+
return ipfs('version --commit').then(out =>
40+
expect(out).to.eql(`js-ipfs version: ${pkgversion}-\n`)
41+
)
42+
})
43+
44+
it('handles --all', () => {
45+
return ipfs('version --all').then(out =>
46+
expect(out).to.include(
47+
`js-ipfs version: ${pkgversion}-
48+
Repo version: ${repoVersion}
49+
`
50+
)
51+
)
52+
})
53+
54+
it('handles --repo', () => {
55+
return ipfs('version --repo').then(out => {
56+
expect(out).to.eql(`${repoVersion}\n`)
57+
})
58+
})
2259
}))

0 commit comments

Comments
 (0)