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

Support --human flag in cmd/object-stat #6241

Merged
merged 1 commit into from
Apr 23, 2019
Merged
Show file tree
Hide file tree
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
12 changes: 11 additions & 1 deletion core/commands/object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/ipfs/go-ipfs/core/commands/cmdenv"

humanize "github.com/dustin/go-humanize"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-ipfs-cmdkit"
"github.com/ipfs/go-ipfs-cmds"
Expand Down Expand Up @@ -43,6 +44,7 @@ const (
datafieldencOptionName = "datafieldenc"
pinOptionName = "pin"
quietOptionName = "quiet"
humanOptionName = "human"
)

var ObjectCmd = &cmds.Command{
Expand Down Expand Up @@ -303,6 +305,9 @@ var ObjectStatCmd = &cmds.Command{
Arguments: []cmdkit.Argument{
cmdkit.StringArg("key", true, false, "Key of the object to retrieve, in base58-encoded multihash format.").EnableStdin(),
},
Options: []cmdkit.Option{
cmdkit.BoolOption(humanOptionName, "Print sizes in human readable format (e.g., 1K 234M 2G)"),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env, req)
if err != nil {
Expand Down Expand Up @@ -338,11 +343,16 @@ var ObjectStatCmd = &cmds.Command{
fw := func(s string, n int) {
fmt.Fprintf(wtr, "%s:\t%d\n", s, n)
}
human, _ := req.Options[humanOptionName].(bool)
fw("NumLinks", out.NumLinks)
fw("BlockSize", out.BlockSize)
fw("LinksSize", out.LinksSize)
fw("DataSize", out.DataSize)
fw("CumulativeSize", out.CumulativeSize)
if human {
fmt.Fprintf(wtr, "%s:\t%s\n", "CumulativeSize", humanize.Bytes(uint64(out.CumulativeSize)))
} else {
fw("CumulativeSize", out.CumulativeSize)
}

return nil
}),
Expand Down
14 changes: 14 additions & 0 deletions test/sharness/t0051-object.sh
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,20 @@ test_object_cmd() {
test_cmp obj_stat_exp obj_stat_out
'

test_expect_success "'ipfs object stat --human' succeeds" '
ipfs object stat $(cat multi_patch)/a --human > obj_stat_human_out
'

test_expect_success "ipfs object stat --human output looks good" '
echo "NumLinks: 1" > obj_stat_human_exp &&
echo "BlockSize: 47" >> obj_stat_human_exp &&
echo "LinksSize: 45" >> obj_stat_human_exp &&
echo "DataSize: 2" >> obj_stat_human_exp &&
echo "CumulativeSize: 114 B" >> obj_stat_human_exp &&

test_cmp obj_stat_human_exp obj_stat_human_out
'

test_expect_success "should have created dir within a dir" '
ipfs ls $OUTPUT > patched_output
'
Expand Down