提交 5e40ee19 作者: tg

command repo stat: improve human flag behavior

Now use go-humanize

License: MIT
Signed-off-by: 's avatarGeorgij Tolstov <tolstov.georgij@gmail.com>
上级 e11bbd78
...@@ -12,6 +12,7 @@ import ( ...@@ -12,6 +12,7 @@ import (
"sync" "sync"
"text/tabwriter" "text/tabwriter"
humanize "github.com/dustin/go-humanize"
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv" cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
corerepo "github.com/ipfs/go-ipfs/core/corerepo" corerepo "github.com/ipfs/go-ipfs/core/corerepo"
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo" fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
...@@ -148,7 +149,7 @@ Version string The repo version. ...@@ -148,7 +149,7 @@ Version string The repo version.
}, },
Options: []cmdkit.Option{ Options: []cmdkit.Option{
cmdkit.BoolOption(repoSizeOnlyOptionName, "Only report RepoSize and StorageMax."), cmdkit.BoolOption(repoSizeOnlyOptionName, "Only report RepoSize and StorageMax."),
cmdkit.BoolOption(repoHumanOptionName, "Output sizes in MiB."), cmdkit.BoolOption(repoHumanOptionName, "Print sizes in human readable format (e.g., 1K 234M 2G)"),
}, },
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
n, err := cmdenv.GetNode(env) n, err := cmdenv.GetNode(env)
...@@ -185,12 +186,12 @@ Version string The repo version. ...@@ -185,12 +186,12 @@ Version string The repo version.
sizeOnly, _ := req.Options[repoSizeOnlyOptionName].(bool) sizeOnly, _ := req.Options[repoSizeOnlyOptionName].(bool)
printSize := func(name string, size uint64) { printSize := func(name string, size uint64) {
sizeInMiB := size / (1024 * 1024) sizeStr := fmt.Sprintf("%d", size)
if human && sizeInMiB > 0 { if human {
fmt.Fprintf(wtr, "%s (MiB):\t%d\n", name, sizeInMiB) sizeStr = humanize.Bytes(size)
} else {
fmt.Fprintf(wtr, "%s:\t%d\n", name, size)
} }
fmt.Fprintf(wtr, "%s:\t%s\n", name, sizeStr)
} }
if !sizeOnly { if !sizeOnly {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论