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

Commit 968ae08

Browse files
JonKronedaviddias
authored andcommitted
feat: ipfs version flags and ipfs repo version (#1199)
1 parent 335912d commit 968ae08

File tree

7 files changed

+74
-47
lines changed

7 files changed

+74
-47
lines changed

src/cli/commands/version.js

Lines changed: 7 additions & 4 deletions
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 = {
@@ -32,22 +33,24 @@ module.exports = {
3233
},
3334

3435
handler (argv) {
35-
argv.ipfs.version((err, ipfs) => {
36+
argv.ipfs.version((err, data) => {
3637
if (err) {
3738
throw err
3839
}
3940

4041
const withCommit = argv.all || argv.commit
41-
const parsedVersion = `${ipfs.version}${withCommit ? `-${ipfs.commit}` : ''}`
42+
const parsedVersion = `${data.version}${withCommit ? `-${data.commit}` : ''}`
4243

4344
if (argv.repo) {
4445
// go-ipfs prints only the number, even without the --number flag.
45-
print(ipfs.repo)
46+
print(data.repo)
4647
} else if (argv.number) {
4748
print(parsedVersion)
4849
} else if (argv.all) {
4950
print(`js-ipfs version: ${parsedVersion}`)
50-
print(`Repo version: ${ipfs.repo}`)
51+
print(`Repo version: ${data.repo}`)
52+
print(`System version: ${os.arch()}/${os.platform()}`)
53+
print(`Node.js version: ${process.version}`)
5154
} else {
5255
print(`js-ipfs version: ${parsedVersion}`)
5356
}

src/core/components/repo.js

Lines changed: 19 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = function version (self) {
1313

1414
self.repo.version((err, repoVersion) => {
1515
if (err) {
16-
throw err
16+
callback(err)
1717
}
1818

1919
callback(null, {

test/cli/files.js

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const fs = require('fs')
5-
const path = require('path')
64
const expect = require('chai').expect
7-
const runOnAndOff = require('../utils/on-and-off')
5+
const repoVersion = require('ipfs-repo').repoVersion
86

9-
function getRepoVersion (repoPath) {
10-
const versionPath = path.join(repoPath, 'version')
11-
return String(fs.readFileSync(versionPath))
12-
}
7+
const runOnAndOff = require('../utils/on-and-off')
138

149
describe('repo', () => runOnAndOff((thing) => {
1510
let ipfs
16-
let repoVersion
1711

1812
before(() => {
1913
ipfs = thing.ipfs
20-
repoVersion = getRepoVersion(ipfs.repoPath)
2114
})
2215

2316
it('get the repo version', () => {

test/cli/version.js

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,69 @@
1+
/* eslint max-nested-callbacks: ["error", 5] */
12
/* eslint-env mocha */
23
'use strict'
34

4-
const fs = require('fs')
5-
const path = require('path')
5+
const os = require('os')
66
const expect = require('chai').expect
7+
const repoVersion = require('ipfs-repo').repoVersion
78
const pkgversion = require('../../package.json').version
89
const runOnAndOff = require('../utils/on-and-off')
910

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

1914
before(() => {
2015
ipfs = thing.ipfs
21-
repoVersion = getRepoVersion(ipfs.repoPath)
2216
})
2317

24-
it('get the version', () => {
25-
return ipfs('version').then((out) => {
18+
it('get the version', () =>
19+
ipfs('version').then(out =>
2620
expect(out).to.eql(
2721
`js-ipfs version: ${pkgversion}\n`
2822
)
29-
})
30-
})
23+
)
24+
)
3125

32-
it('handles --number', () => {
33-
return ipfs('version --number').then(out =>
26+
it('handles --number', () =>
27+
ipfs('version --number').then(out =>
3428
expect(out).to.eql(`${pkgversion}\n`)
3529
)
36-
})
30+
)
3731

38-
it('handles --commit', () => {
39-
return ipfs('version --commit').then(out =>
32+
it('handles --commit', () =>
33+
ipfs('version --commit').then(out =>
4034
expect(out).to.eql(`js-ipfs version: ${pkgversion}-\n`)
4135
)
42-
})
36+
)
4337

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-
)
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+
})
5161
)
5262
})
5363

54-
it('handles --repo', () => {
55-
return ipfs('version --repo').then(out => {
64+
it('handles --repo', () =>
65+
ipfs('version --repo').then(out =>
5666
expect(out).to.eql(`${repoVersion}\n`)
57-
})
58-
})
67+
)
68+
)
5969
}))

test/sharness/t0010-basic-commands.sh

Lines changed: 4 additions & 1 deletion
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)