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

Commit b4d3bf8

Browse files
authored
feat: add size-only flag to cli repo stat command (#3143)
Makes `stats repo` an alias for `repo stat` to make formatting the same across commmands in line with go-ipfs. Also adds `-s` flag to both commands to print just the size info.
1 parent 62c1422 commit b4d3bf8

File tree

3 files changed

+49
-40
lines changed

3 files changed

+49
-40
lines changed

packages/ipfs/src/cli/commands/repo/stat.js

+16-6
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,18 @@ module.exports = {
1414
alias: 'H',
1515
default: false
1616
},
17+
sizeOnly: {
18+
type: 'boolean',
19+
alias: 's',
20+
default: false
21+
},
1722
timeout: {
1823
type: 'string',
1924
coerce: parseDuration
2025
}
2126
},
2227

23-
async handler ({ ctx: { ipfs, print }, human, timeout }) {
28+
async handler ({ ctx: { ipfs, print }, human, sizeOnly, timeout }) {
2429
const stats = await ipfs.repo.stat({
2530
timeout
2631
})
@@ -31,11 +36,16 @@ module.exports = {
3136
stats.storageMax = prettyBytes(stats.storageMax.toNumber()).toUpperCase()
3237
}
3338

34-
print(
35-
`NumObjects: ${stats.numObjects}
36-
RepoSize: ${stats.repoSize}
39+
if (sizeOnly) {
40+
print(
41+
`RepoSize: ${stats.repoSize}
42+
StorageMax: ${stats.storageMax}`)
43+
} else {
44+
print(`NumObjects: ${stats.numObjects}
45+
RepoSize: ${stats.repoSize}
3746
StorageMax: ${stats.storageMax}
38-
RepoPath: ${stats.repoPath}
39-
Version: ${stats.version}`)
47+
RepoPath: ${stats.repoPath}
48+
Version: ${stats.version}`)
49+
}
4050
}
4151
}
+6-24
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,12 @@
11
'use strict'
22

3-
const parseDuration = require('parse-duration').default
3+
// This is an alias for `repo stat`.
4+
const repoStats = require('../repo/stat.js')
45

56
module.exports = {
6-
command: 'repo',
7+
...repoStats,
78

8-
describe: 'Get stats for the currently used repo',
9-
10-
builder: {
11-
human: {
12-
type: 'boolean',
13-
default: false
14-
},
15-
timeout: {
16-
type: 'string',
17-
coerce: parseDuration
18-
}
19-
},
20-
21-
async handler ({ ctx: { ipfs, print } }, human, timeout) {
22-
const stats = await ipfs.stats.repo({ human, timeout })
23-
print(`repo status
24-
number of objects: ${stats.numObjects}
25-
repo size: ${stats.repoSize}
26-
repo path: ${stats.repoPath}
27-
version: ${stats.version}
28-
maximum storage: ${stats.storageMax}`)
29-
}
9+
// The command needs to be renamed, else it would be `stats stat` instead of
10+
// `stats repo`
11+
command: 'repo'
3012
}

packages/ipfs/test/cli/repo.js

+27-10
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,28 @@ describe('repo', () => {
3535
})
3636

3737
const stats = await cli('repo stat', { ipfs })
38-
expect(stats).to.match(/^NumObjects:\s\d+$/m)
39-
expect(stats).to.match(/^RepoSize:\s\d+$/m)
40-
expect(stats).to.match(/^StorageMax:\s\d+$/m)
38+
expect(stats).to.match(/^NumObjects:\s+\d+$/m)
39+
expect(stats).to.match(/^RepoSize:\s+\d+$/m)
40+
expect(stats).to.match(/^StorageMax:\s+\d+$/m)
4141
expect(stats).to.match(/^RepoPath:\s.+$/m)
42-
expect(stats).to.match(/^Version:\s\d+$/m)
42+
expect(stats).to.match(/^Version:\s+\d+$/m)
43+
})
44+
45+
it('get repo stats with just size', async () => {
46+
ipfs.repo.stat.withArgs(defaultOptions).resolves({
47+
numObjects: BigNumber(10),
48+
repoSize: BigNumber(10),
49+
storageMax: BigNumber(10),
50+
repoPath: '/foo',
51+
version: 5
52+
})
53+
54+
const stats = await cli('repo stat -s', { ipfs })
55+
expect(stats).to.not.match(/^NumObjects:$/m)
56+
expect(stats).to.match(/^RepoSize:\s+\d+$/m)
57+
expect(stats).to.match(/^StorageMax:\s+\d+$/m)
58+
expect(stats).to.not.match(/^RepoPath:$/m)
59+
expect(stats).to.not.match(/^Version:$/m)
4360
})
4461

4562
it('get human readable repo stats', async () => {
@@ -52,11 +69,11 @@ describe('repo', () => {
5269
})
5370

5471
const stats = await cli('repo stat --human', { ipfs })
55-
expect(stats).to.match(/^NumObjects:\s\d+$/m)
72+
expect(stats).to.match(/^NumObjects:\s+\d+$/m)
5673
expect(stats).to.match(/^RepoSize:\s+[\d.]+\s[PTGMK]?B$/gm)
5774
expect(stats).to.match(/^StorageMax:\s+[\d.]+\s[PTGMK]?B$/gm)
5875
expect(stats).to.match(/^RepoPath:\s.+$/m)
59-
expect(stats).to.match(/^Version:\s\d+$/m)
76+
expect(stats).to.match(/^Version:\s+\d+$/m)
6077
})
6178

6279
it('get repo with timeout', async () => {
@@ -72,11 +89,11 @@ describe('repo', () => {
7289
})
7390

7491
const stats = await cli('repo stat --timeout=1s', { ipfs })
75-
expect(stats).to.match(/^NumObjects:\s\d+$/m)
76-
expect(stats).to.match(/^RepoSize:\s\d+$/m)
77-
expect(stats).to.match(/^StorageMax:\s\d+$/m)
92+
expect(stats).to.match(/^NumObjects:\s+\d+$/m)
93+
expect(stats).to.match(/^RepoSize:\s+\d+$/m)
94+
expect(stats).to.match(/^StorageMax:\s+\d+$/m)
7895
expect(stats).to.match(/^RepoPath:\s.+$/m)
79-
expect(stats).to.match(/^Version:\s\d+$/m)
96+
expect(stats).to.match(/^Version:\s+\d+$/m)
8097
})
8198
})
8299

0 commit comments

Comments
 (0)