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

Commit 39bc5b4

Browse files
Pedro SantosAlan Shaw
Pedro Santos
authored and
Alan Shaw
committed
feat: add human flag to repo stat cli command (#2630)
1 parent 44579fd commit 39bc5b4

File tree

5 files changed

+41
-15
lines changed

5 files changed

+41
-15
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
"ipfs-http-response": "~0.4.0",
105105
"ipfs-mfs": "^0.13.0",
106106
"ipfs-multipart": "^0.2.0",
107-
"ipfs-repo": "^0.29.0",
107+
"ipfs-repo": "github:ipfs/js-ipfs-repo#feat/remove-options-object-from-stat",
108108
"ipfs-unixfs": "~0.1.16",
109109
"ipfs-unixfs-exporter": "^0.38.0",
110110
"ipfs-unixfs-importer": "^0.40.0",

src/cli/commands/repo/stat.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict'
22

3+
const prettyBytes = require('pretty-bytes')
4+
35
module.exports = {
46
command: 'stat',
57

@@ -15,14 +17,23 @@ module.exports = {
1517

1618
handler (argv) {
1719
argv.resolve((async () => {
18-
const ipfs = await argv.getIpfs()
19-
const stats = await ipfs.repo.stat({ human: argv.human })
20-
argv.print(`repo status
21-
number of objects: ${stats.numObjects}
22-
repo size: ${stats.repoSize}
23-
repo path: ${stats.repoPath}
24-
version: ${stats.version}
25-
maximum storage: ${stats.storageMax}`)
20+
const { getIpfs, human } = argv
21+
22+
const ipfs = await getIpfs()
23+
const stats = await ipfs.repo.stat()
24+
25+
if (human) {
26+
stats.numObjects = stats.numObjects.toNumber()
27+
stats.repoSize = prettyBytes(stats.repoSize.toNumber()).toUpperCase()
28+
stats.storageMax = prettyBytes(stats.storageMax.toNumber()).toUpperCase()
29+
}
30+
31+
argv.print(
32+
`NumObjects: ${stats.numObjects}
33+
RepoSize: ${stats.repoSize}
34+
StorageMax: ${stats.storageMax}
35+
RepoPath: ${stats.repoPath}
36+
Version: ${stats.version}`)
2637
})())
2738
}
2839
}

src/core/components/repo.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,8 @@ module.exports = function repo (self) {
4040

4141
gc: require('./pin/gc')(self),
4242

43-
stat: callbackify.variadic(async (options) => {
44-
options = options || {}
45-
46-
const stats = await self._repo.stat(options)
43+
stat: callbackify.variadic(async () => {
44+
const stats = await self._repo.stat()
4745

4846
return {
4947
numObjects: stats.numObjects,

src/http/api/resources/repo.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ exports.version = async (request, h) => {
3535

3636
exports.stat = async (request, h) => {
3737
const { ipfs } = request.server.app
38-
const human = request.query.human === 'true'
39-
const stat = await ipfs.repo.stat({ human })
38+
const stat = await ipfs.repo.stat()
4039

4140
return h.response({
4241
NumObjects: stat.numObjects,

test/cli/repo.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,24 @@ describe('repo', () => runOnAndOff((thing) => {
1212
ipfs = thing.ipfs
1313
})
1414

15+
it('get repo stats', async () => {
16+
const stats = await ipfs('repo stat')
17+
expect(stats).to.match(/^NumObjects:\s\d+$/m)
18+
expect(stats).to.match(/^RepoSize:\s\d+$/m)
19+
expect(stats).to.match(/^StorageMax:\s\d+$/m)
20+
expect(stats).to.match(/^RepoPath:\s.+$/m)
21+
expect(stats).to.match(/^Version:\s\d+$/m)
22+
})
23+
24+
it('get human readable repo stats', async () => {
25+
const stats = await ipfs('repo stat --human')
26+
expect(stats).to.match(/^NumObjects:\s\d+$/m)
27+
expect(stats).to.match(/^RepoSize:\s+[\d.]+\s[PTGMK]?B$/gm)
28+
expect(stats).to.match(/^StorageMax:\s+[\d.]+\s[PTGMK]?B$/gm)
29+
expect(stats).to.match(/^RepoPath:\s.+$/m)
30+
expect(stats).to.match(/^Version:\s\d+$/m)
31+
})
32+
1533
it('get the repo version', async () => {
1634
const out = await ipfs('repo version')
1735
expect(out).to.eql(`${repoVersion}\n`)

0 commit comments

Comments
 (0)