Skip to content

Commit

Permalink
cmd/object/stat: add human flag for object stat
Browse files Browse the repository at this point in the history
	- add support for printing CumulativeSize in human
	  readable format.
	- add test to validate output.

License: MIT
Signed-off-by: Vikram Sreekumar <vikram1791@gmail.com>
  • Loading branch information
vikramsk committed Apr 21, 2019
1 parent 6b58b13 commit a7ed9d9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
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

0 comments on commit a7ed9d9

Please sign in to comment.