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

Commit 69523e8

Browse files
committed
fix(cli): add human flag to repo stat & stats repo
- Use filesize to return human readable sizes. - handler now returns stats instead of printing them. License: MIT Signed-off-by: Chirag Shinde <[email protected]>
1 parent 92f8d4c commit 69523e8

File tree

4 files changed

+12
-28
lines changed

4 files changed

+12
-28
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@
109109
"debug": "^4.1.0",
110110
"dlv": "^1.1.3",
111111
"err-code": "^1.1.2",
112-
"file-type": "^11.1.0",
112+
"file-type": "^11.0.0",
113+
"filesize": "^4.1.2",
113114
"fnv1a": "^1.0.1",
114115
"fsm-event": "^2.1.0",
115116
"get-folder-size": "^2.0.0",

src/cli/commands/repo/stat.js

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

3-
const print = require('../../utils').print
4-
const humanize = require('../../utils').humanize
3+
const filesize = require('filesize')
54

65
module.exports = {
76
command: 'stat',
@@ -20,15 +19,15 @@ module.exports = {
2019
const ipfs = await argv.getIpfs()
2120
const stats = await ipfs.repo.stat()
2221
if (argv.human) {
23-
stats.repoSize = humanize.Bytes(stats.repoSize)
24-
stats.storageMax = humanize.Bytes(stats.storageMax)
22+
stats.repoSize = filesize(stats.repoSize)
23+
stats.storageMax = filesize(stats.storageMax)
2524
}
26-
print(`repo status
25+
return `repo status
2726
number of objects: ${stats.numObjects}
2827
repo size: ${stats.repoSize}
2928
repo path: ${stats.repoPath}
3029
version: ${stats.version}
31-
maximum storage: ${stats.storageMax}`)
30+
maximum storage: ${stats.storageMax}`
3231
})())
3332
}
3433
}

src/cli/commands/stats/repo.js

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

3-
const print = require('../../utils').print
4-
const humanize = require('../../utils').humanize
3+
const filesize = require('filesize')
54

65
module.exports = {
76
command: 'repo',
@@ -20,15 +19,15 @@ module.exports = {
2019
const ipfs = await argv.getIpfs()
2120
const stats = await ipfs.stats.repo()
2221
if (argv.human) {
23-
stats.repoSize = humanize.Bytes(stats.repoSize)
24-
stats.storageMax = humanize.Bytes(stats.storageMax)
22+
stats.repoSize = filesize(stats.repoSize)
23+
stats.storageMax = filesize(stats.storageMax)
2524
}
26-
print(`repo status
25+
return `repo status
2726
number of objects: ${stats.numObjects}
2827
repo size: ${stats.repoSize}
2928
repo path: ${stats.repoPath}
3029
version: ${stats.version}
31-
maximum storage: ${stats.storageMax}`)
30+
maximum storage: ${stats.storageMax}`
3231
})())
3332
}
3433
}

src/cli/utils.js

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,3 @@ exports.singleton = create => {
135135
})
136136
return getter
137137
}
138-
exports.humanize = {
139-
Bytes: (bytes) => {
140-
const thresh = 1024
141-
if (Math.abs(bytes) < thresh) {
142-
return bytes + ' B'
143-
}
144-
const units = ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
145-
let unit = -1
146-
do {
147-
bytes /= thresh
148-
++unit
149-
} while (Math.abs(bytes) >= thresh && unit < units.length - 1)
150-
return bytes.toFixed(1) + ' ' + units[unit]
151-
}
152-
}

0 commit comments

Comments
 (0)