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

Commit 54d47e1

Browse files
JonKronedaviddias
authored andcommitted
feat: ipfs version flags + ipfs repo version (#1181)
1 parent d16a129 commit 54d47e1

File tree

9 files changed

+123
-16
lines changed

9 files changed

+123
-16
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
"hapi": "^16.6.2",
106106
"hapi-set-header": "^1.0.2",
107107
"hoek": "^5.0.2",
108-
"ipfs-api": "^17.3.0",
108+
"ipfs-api": "^17.5.0",
109109
"ipfs-bitswap": "~0.18.0",
110110
"ipfs-block": "~0.6.1",
111111
"ipfs-block-service": "~0.13.0",

src/cli/commands/repo/version.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = {
1010
builder: {},
1111

1212
handler (argv) {
13-
argv.ipfs.repo.version(function (err, version) {
13+
argv.ipfs.repo.version((err, version) => {
1414
if (err) {
1515
throw err
1616
}

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
}

src/http/api/resources/repo.js

+16
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
11
'use strict'
2+
3+
const boom = require('boom')
4+
5+
exports = module.exports
6+
7+
exports.version = (request, reply) => {
8+
const ipfs = request.server.app.ipfs
9+
10+
ipfs.repo.version((err, version) => {
11+
if (err) {
12+
return reply(boom.badRequest(err))
13+
}
14+
15+
reply(version)
16+
})
17+
}

src/http/api/routes/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = (server) => {
66
require('./bootstrap')(server)
77
require('./block')(server)
88
require('./object')(server)
9-
// require('./repo')(server)
9+
require('./repo')(server)
1010
require('./config')(server)
1111
require('./swarm')(server)
1212
require('./bitswap')(server)

src/http/api/routes/repo.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
const resources = require('./../resources')
44

5-
// TODO
65
module.exports = (server) => {
76
const api = server.select('API')
87

98
api.route({
109
method: '*',
11-
path: '/api/v0/repo',
12-
handler: resources.repo
10+
path: '/api/v0/repo/version',
11+
handler: resources.repo.version
1312
})
1413
}

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)