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

Commit a4faca8

Browse files
committed
cli: add --human flag to repo stat & stats repo
- Added a `humanize` object to /src/cli/utils with `Bytes` method that takes bytes and returns Human readable size. - Used `humanize.Bytes()` method in /commands/stats/repo and /commands/repo/stat if `--human` flag is passed. License: MIT Signed-off-by: Chirag Shinde <[email protected]>
1 parent 7815c2f commit a4faca8

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/cli/commands/repo/stat.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const print = require('../../utils').print
4+
const humanize = require('../../utils').humanize
45

56
module.exports = {
67
command: 'stat',
@@ -17,7 +18,11 @@ module.exports = {
1718
handler (argv) {
1819
argv.resolve((async () => {
1920
const ipfs = await argv.getIpfs()
20-
const stats = await ipfs.repo.stat({ human: argv.human })
21+
const stats = await ipfs.repo.stat()
22+
if (argv.human) {
23+
stats.repoSize = humanize.Bytes(stats.repoSize)
24+
stats.storageMax = humanize.Bytes(stats.storageMax)
25+
}
2126
print(`repo status
2227
number of objects: ${stats.numObjects}
2328
repo size: ${stats.repoSize}

src/cli/commands/stats/repo.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const print = require('../../utils').print
4+
const humanize = require('../../utils').humanize
45

56
module.exports = {
67
command: 'repo',
@@ -17,7 +18,11 @@ module.exports = {
1718
handler (argv) {
1819
argv.resolve((async () => {
1920
const ipfs = await argv.getIpfs()
20-
const stats = await ipfs.stats.repo({ human: argv.human })
21+
const stats = await ipfs.stats.repo()
22+
if (argv.human) {
23+
stats.repoSize = humanize.Bytes(stats.repoSize)
24+
stats.storageMax = humanize.Bytes(stats.storageMax)
25+
}
2126
print(`repo status
2227
number of objects: ${stats.numObjects}
2328
repo size: ${stats.repoSize}

src/cli/utils.js

+15
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,18 @@ 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)