command repo stat: improve human flag behavior
Now use go-humanize License: MIT Signed-off-by: Georgij Tolstov <tolstov.georgij@gmail.com>
tg committed
Mar 20, 2019 at 18:35 UTC
5e40ee19bc30a392408f6a9b493b32b9648e074d
1 file changed
+7
-6
core/commands/repo.go
+7
-6
@@ -12,6 +12,7 @@ import (
12
"sync"
13
"text/tabwriter"
14
15
+ humanize "github.com/dustin/go-humanize"
16
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
17
corerepo "github.com/ipfs/go-ipfs/core/corerepo"
18
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
@@ -148,7 +149,7 @@ Version string The repo version.
149
},
150
Options: []cmdkit.Option{
151
cmdkit.BoolOption(repoSizeOnlyOptionName, "Only report RepoSize and StorageMax."),
151
- cmdkit.BoolOption(repoHumanOptionName, "Output sizes in MiB."),
152
+ cmdkit.BoolOption(repoHumanOptionName, "Print sizes in human readable format (e.g., 1K 234M 2G)"),
153
},
154
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
155
n, err := cmdenv.GetNode(env)
@@ -185,12 +186,12 @@ Version string The repo version.
186
sizeOnly, _ := req.Options[repoSizeOnlyOptionName].(bool)
187
188
printSize := func(name string, size uint64) {
188
- sizeInMiB := size / (1024 * 1024)
189
- if human && sizeInMiB > 0 {
190
- fmt.Fprintf(wtr, "%s (MiB):\t%d\n", name, sizeInMiB)
191
- } else {
192
- fmt.Fprintf(wtr, "%s:\t%d\n", name, size)
189
+ sizeStr := fmt.Sprintf("%d", size)
190
+ if human {
191
+ sizeStr = humanize.Bytes(size)
192
}
193
+
194
+ fmt.Fprintf(wtr, "%s:\t%s\n", name, sizeStr)
195
}
196
197
if !sizeOnly {