Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command repo stat: improve human flag behavior #6106

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions core/commands/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"sync"
"text/tabwriter"

humanize "github.com/dustin/go-humanize"
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
corerepo "github.com/ipfs/go-ipfs/core/corerepo"
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
Expand Down Expand Up @@ -148,7 +149,7 @@ Version string The repo version.
},
Options: []cmdkit.Option{
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 {
n, err := cmdenv.GetNode(env)
Expand Down Expand Up @@ -185,12 +186,12 @@ Version string The repo version.
sizeOnly, _ := req.Options[repoSizeOnlyOptionName].(bool)

printSize := func(name string, size uint64) {
sizeInMiB := size / (1024 * 1024)
if human && sizeInMiB > 0 {
fmt.Fprintf(wtr, "%s (MiB):\t%d\n", name, sizeInMiB)
} else {
fmt.Fprintf(wtr, "%s:\t%d\n", name, size)
sizeStr := fmt.Sprintf("%d", size)
if human {
sizeStr = humanize.Bytes(size)
}

fmt.Fprintf(wtr, "%s:\t%s\n", name, sizeStr)
}

if !sizeOnly {
Expand Down