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

Commit 3b45202

Browse files
daviddiasdryajov
authored andcommitted
feat: ipfs version flags + ipfs repo version (#1181) (#1188)
1 parent d2f55ea commit 3b45202

File tree

7 files changed

+135
-16
lines changed

7 files changed

+135
-16
lines changed

src/cli/commands/version.js

+29-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
const os = require('os')
34
const print = require('../utils').print
45

56
module.exports = {
@@ -11,26 +12,48 @@ module.exports = {
1112
number: {
1213
alias: 'n',
1314
type: 'boolean',
14-
default: false
15+
default: false,
16+
describe: 'Print only the version number'
1517
},
1618
commit: {
1719
type: 'boolean',
18-
default: false
20+
default: false,
21+
describe: `Include the version's commit hash`
1922
},
2023
repo: {
2124
type: 'boolean',
22-
default: false
25+
default: false,
26+
describe: `Print only the repo's version number`
27+
},
28+
all: {
29+
type: 'boolean',
30+
default: false,
31+
describe: 'Print everything we have'
2332
}
2433
},
2534

2635
handler (argv) {
27-
// TODO: handle argv.{repo|commit|number}
28-
argv.ipfs.version((err, version) => {
36+
argv.ipfs.version((err, data) => {
2937
if (err) {
3038
throw err
3139
}
3240

33-
print(`js-ipfs version: ${version.version}`)
41+
const withCommit = argv.all || argv.commit
42+
const parsedVersion = `${data.version}${withCommit ? `-${data.commit}` : ''}`
43+
44+
if (argv.repo) {
45+
// go-ipfs prints only the number, even without the --number flag.
46+
print(data.repo)
47+
} else if (argv.number) {
48+
print(parsedVersion)
49+
} else if (argv.all) {
50+
print(`js-ipfs version: ${parsedVersion}`)
51+
print(`Repo version: ${data.repo}`)
52+
print(`System version: ${os.arch()}/${os.platform()}`)
53+
print(`Node.js version: ${process.version}`)
54+
} else {
55+
print(`js-ipfs version: ${parsedVersion}`)
56+
}
3457
})
3558
}
3659
}

src/core/components/repo.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
11
'use strict'
22

33
const promisify = require('promisify-es6')
4+
const repoVersion = require('ipfs-repo').repoVersion
45

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

12+
/**
13+
* If the repo has been initialized, report the current version.
14+
* Otherwise report the version that would be initialized.
15+
*
16+
* @param {function(Error, Number)} [callback]
17+
* @returns {undefined}
18+
*/
1119
version: promisify((callback) => {
12-
self._repo.version.get(callback)
20+
self._repo._isInitialized(err => {
21+
if (err) {
22+
if (/ENOENT|not yet initialized/.test(err.message)) {
23+
// this repo has not been initialized
24+
return callback(null, repoVersion)
25+
}
26+
return callback(err)
27+
}
28+
29+
self._repo.version.get(callback)
30+
})
1331
}),
1432

1533
gc: () => {},

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+
callback(err)
17+
}
18+
19+
callback(null, {
20+
version: pkg.version,
21+
repo: repoVersion,
22+
commit: ''
23+
})
1724
})
1825
})
1926
}

test/cli/files.js

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

4-
const expect = require('chai').expect
54
const fs = require('fs')
5+
const expect = require('chai').expect
66
const path = require('path')
77
const compareDir = require('dir-compare').compareSync
88
const rimraf = require('rimraf').sync

test/cli/repo.js

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

test/cli/version.js

+50-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
/* eslint max-nested-callbacks: ["error", 5] */
12
/* eslint-env mocha */
23
'use strict'
34

5+
const os = require('os')
46
const expect = require('chai').expect
7+
const repoVersion = require('ipfs-repo').repoVersion
58
const pkgversion = require('../../package.json').version
69
const runOnAndOff = require('../utils/on-and-off')
710

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

15-
it('get the version', () => {
16-
return ipfs('version').then((out) => {
18+
it('get the version', () =>
19+
ipfs('version').then(out =>
1720
expect(out).to.eql(
1821
`js-ipfs version: ${pkgversion}\n`
1922
)
20-
})
23+
)
24+
)
25+
26+
it('handles --number', () =>
27+
ipfs('version --number').then(out =>
28+
expect(out).to.eql(`${pkgversion}\n`)
29+
)
30+
)
31+
32+
it('handles --commit', () =>
33+
ipfs('version --commit').then(out =>
34+
expect(out).to.eql(`js-ipfs version: ${pkgversion}-\n`)
35+
)
36+
)
37+
38+
describe('handles --all', function () {
39+
it('prints js-ipfs version', () =>
40+
ipfs('version --all').then(out => {
41+
expect(out).to.include(`js-ipfs version: ${pkgversion}`)
42+
})
43+
)
44+
45+
it('prints repo version', () =>
46+
ipfs('version --all').then(out => {
47+
expect(out).to.include(`Repo version: ${repoVersion}`)
48+
})
49+
)
50+
51+
it('prints arch/platform', () =>
52+
ipfs('version --all').then(out => {
53+
expect(out).to.include(`System version: ${os.arch()}/${os.platform()}`)
54+
})
55+
)
56+
57+
it('prints Node.js version', () =>
58+
ipfs('version --all').then(out => {
59+
expect(out).to.include(`Node.js version: ${process.version}`)
60+
})
61+
)
2162
})
63+
64+
it('handles --repo', () =>
65+
ipfs('version --repo').then(out =>
66+
expect(out).to.eql(`${repoVersion}\n`)
67+
)
68+
)
2269
}))

test/sharness/t0010-basic-commands.sh

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ test_expect_success "ipfs version output looks good" '
2828

2929
test_expect_success "ipfs version --all has all required fields" '
3030
ipfs version --all > version_all.txt &&
31-
grep "js-ipfs version" version_all.txt
31+
grep "js-ipfs version" version_all.txt &&
32+
grep "Repo version" version_all.txt &&
33+
grep "System version" version_all.txt &&
34+
grep "Node.js version" version_all.txt
3235
'
3336

3437
test_expect_success "ipfs help succeeds" '

0 commit comments

Comments
 (0)