@cryptotaxi247 / kubo / commits / a7ed9d9fe

cmd/object/stat: add human flag for object stat

- add support for printing CumulativeSize in human readable format. - add test to validate output. License: MIT Signed-off-by: Vikram Sreekumar <vikram1791@gmail.com>

Vikram committed Apr 20, 2019 at 17:47 UTC a7ed9d9fe809a63d66efa0169bc03cb927b5f566
2 files changed +25 -1
core/commands/object/object.go
+11 -1
@@ -10,6 +10,7 @@ import (
10
11 "github.com/ipfs/go-ipfs/core/commands/cmdenv"
12
13 + humanize "github.com/dustin/go-humanize"
14 "github.com/ipfs/go-cid"
15 "github.com/ipfs/go-ipfs-cmdkit"
16 "github.com/ipfs/go-ipfs-cmds"
@@ -43,6 +44,7 @@ const (
44 datafieldencOptionName = "datafieldenc"
45 pinOptionName = "pin"
46 quietOptionName = "quiet"
47 + humanOptionName = "human"
48 )
49
50 var ObjectCmd = &cmds.Command{
@@ -303,6 +305,9 @@ var ObjectStatCmd = &cmds.Command{
305 Arguments: []cmdkit.Argument{
306 cmdkit.StringArg("key", true, false, "Key of the object to retrieve, in base58-encoded multihash format.").EnableStdin(),
307 },
308 + Options: []cmdkit.Option{
309 + cmdkit.BoolOption(humanOptionName, "Print sizes in human readable format (e.g., 1K 234M 2G)"),
310 + },
311 Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
312 api, err := cmdenv.GetApi(env, req)
313 if err != nil {
@@ -338,11 +343,16 @@ var ObjectStatCmd = &cmds.Command{
343 fw := func(s string, n int) {
344 fmt.Fprintf(wtr, "%s:\t%d\n", s, n)
345 }
346 + human, _ := req.Options[humanOptionName].(bool)
347 fw("NumLinks", out.NumLinks)
348 fw("BlockSize", out.BlockSize)
349 fw("LinksSize", out.LinksSize)
350 fw("DataSize", out.DataSize)
345 - fw("CumulativeSize", out.CumulativeSize)
351 + if human {
352 + fmt.Fprintf(wtr, "%s:\t%s\n", "CumulativeSize", humanize.Bytes(uint64(out.CumulativeSize)))
353 + } else {
354 + fw("CumulativeSize", out.CumulativeSize)
355 + }
356
357 return nil
358 }),
test/sharness/t0051-object.sh
+14
@@ -285,6 +285,20 @@ test_object_cmd() {
285 test_cmp obj_stat_exp obj_stat_out
286 '
287
288 + test_expect_success "'ipfs object stat --human' succeeds" '
289 + ipfs object stat $(cat multi_patch)/a --human > obj_stat_human_out
290 + '
291 +
292 + test_expect_success "ipfs object stat --human output looks good" '
293 + echo "NumLinks: 1" > obj_stat_human_exp &&
294 + echo "BlockSize: 47" >> obj_stat_human_exp &&
295 + echo "LinksSize: 45" >> obj_stat_human_exp &&
296 + echo "DataSize: 2" >> obj_stat_human_exp &&
297 + echo "CumulativeSize: 114 B" >> obj_stat_human_exp &&
298 +
299 + test_cmp obj_stat_human_exp obj_stat_human_out
300 + '
301 +
302 test_expect_success "should have created dir within a dir" '
303 ipfs ls $OUTPUT > patched_output
304 '