Unverified 提交 cc2d66ff 作者: Steven Allen 提交者: GitHub

Merge pull request #6241 from vikramsk/fix/cmds/object-stat-human-flag

Support --human flag in cmd/object-stat
...@@ -10,6 +10,7 @@ import ( ...@@ -10,6 +10,7 @@ import (
"github.com/ipfs/go-ipfs/core/commands/cmdenv" "github.com/ipfs/go-ipfs/core/commands/cmdenv"
humanize "github.com/dustin/go-humanize"
"github.com/ipfs/go-cid" "github.com/ipfs/go-cid"
"github.com/ipfs/go-ipfs-cmdkit" "github.com/ipfs/go-ipfs-cmdkit"
"github.com/ipfs/go-ipfs-cmds" "github.com/ipfs/go-ipfs-cmds"
...@@ -43,6 +44,7 @@ const ( ...@@ -43,6 +44,7 @@ const (
datafieldencOptionName = "datafieldenc" datafieldencOptionName = "datafieldenc"
pinOptionName = "pin" pinOptionName = "pin"
quietOptionName = "quiet" quietOptionName = "quiet"
humanOptionName = "human"
) )
var ObjectCmd = &cmds.Command{ var ObjectCmd = &cmds.Command{
...@@ -303,6 +305,9 @@ var ObjectStatCmd = &cmds.Command{ ...@@ -303,6 +305,9 @@ var ObjectStatCmd = &cmds.Command{
Arguments: []cmdkit.Argument{ Arguments: []cmdkit.Argument{
cmdkit.StringArg("key", true, false, "Key of the object to retrieve, in base58-encoded multihash format.").EnableStdin(), 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 { Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env, req) api, err := cmdenv.GetApi(env, req)
if err != nil { if err != nil {
...@@ -338,11 +343,16 @@ var ObjectStatCmd = &cmds.Command{ ...@@ -338,11 +343,16 @@ var ObjectStatCmd = &cmds.Command{
fw := func(s string, n int) { fw := func(s string, n int) {
fmt.Fprintf(wtr, "%s:\t%d\n", s, n) fmt.Fprintf(wtr, "%s:\t%d\n", s, n)
} }
human, _ := req.Options[humanOptionName].(bool)
fw("NumLinks", out.NumLinks) fw("NumLinks", out.NumLinks)
fw("BlockSize", out.BlockSize) fw("BlockSize", out.BlockSize)
fw("LinksSize", out.LinksSize) fw("LinksSize", out.LinksSize)
fw("DataSize", out.DataSize) 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 return nil
}), }),
......
...@@ -285,6 +285,20 @@ test_object_cmd() { ...@@ -285,6 +285,20 @@ test_object_cmd() {
test_cmp obj_stat_exp obj_stat_out 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" ' test_expect_success "should have created dir within a dir" '
ipfs ls $OUTPUT > patched_output ipfs ls $OUTPUT > patched_output
' '
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论