ipfs block stat cmd
Juan Batiz-Benet committed
Jan 7, 2015 at 00:52 UTC
07b923d7a4bd2561d1a38496a4f751ca92dc2dca
2 files changed
+85
-30
core/commands/block.go
+75
-30
@@ -2,10 +2,10 @@ package commands
2
3
import (
4
"bytes"
5
+ "fmt"
6
"io"
7
"io/ioutil"
8
"strings"
8
- "time"
9
10
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
11
@@ -15,9 +15,13 @@ import (
15
u "github.com/jbenet/go-ipfs/util"
16
)
17
18
-type Block struct {
19
- Key string
20
- Length int
18
+type BlockStat struct {
19
+ Key string
20
+ Size int
21
+}
22
+
23
+func (bs BlockStat) String() string {
24
+ return fmt.Sprintf("Key: %s\nSize: %d\n", bs.Key, bs.Size)
25
}
26
27
var BlockCmd = &cmds.Command{
@@ -31,17 +35,22 @@ multihash.
35
},
36
37
Subcommands: map[string]*cmds.Command{
34
- "get": blockGetCmd,
35
- "put": blockPutCmd,
38
+ "stat": blockStatCmd,
39
+ "get": blockGetCmd,
40
+ "put": blockPutCmd,
41
},
42
}
43
39
-var blockGetCmd = &cmds.Command{
44
+var blockStatCmd = &cmds.Command{
45
Helptext: cmds.HelpText{
41
- Tagline: "Get a raw IPFS block",
46
+ Tagline: "Print information of a raw IPFS block",
47
ShortDescription: `
43
-'ipfs block get' is a plumbing command for retreiving raw ipfs blocks.
44
-It outputs to stdout, and <key> is a base58 encoded multihash.
48
+'ipfs block stat' is a plumbing command for retreiving information
49
+on raw ipfs blocks. It outputs the following to stdout:
50
+
51
+ Key - the base58 encoded multihash
52
+ Size - the size of the block in bytes
53
+
54
`,
55
},
56
@@ -49,29 +58,42 @@ It outputs to stdout, and <key> is a base58 encoded multihash.
58
cmds.StringArg("key", true, false, "The base58 multihash of an existing block to get"),
59
},
60
Run: func(req cmds.Request) (interface{}, error) {
52
- n, err := req.Context().GetNode()
61
+ b, err := getBlockForKey(req, req.Arguments()[0])
62
if err != nil {
63
return nil, err
64
}
65
57
- key := req.Arguments()[0]
58
-
59
- if !u.IsValidHash(key) {
60
- return nil, cmds.Error{"Not a valid hash", cmds.ErrClient}
61
- }
66
+ return &BlockStat{
67
+ Key: b.Key().Pretty(),
68
+ Size: len(b.Data),
69
+ }, nil
70
+ },
71
+ Type: BlockStat{},
72
+ Marshalers: cmds.MarshalerMap{
73
+ cmds.Text: func(res cmds.Response) (io.Reader, error) {
74
+ bs := res.Output().(*BlockStat)
75
+ return strings.NewReader(bs.String()), nil
76
+ },
77
+ },
78
+}
79
63
- h, err := mh.FromB58String(key)
64
- if err != nil {
65
- return nil, err
66
- }
80
+var blockGetCmd = &cmds.Command{
81
+ Helptext: cmds.HelpText{
82
+ Tagline: "Get a raw IPFS block",
83
+ ShortDescription: `
84
+'ipfs block get' is a plumbing command for retreiving raw ipfs blocks.
85
+It outputs to stdout, and <key> is a base58 encoded multihash.
86
+`,
87
+ },
88
68
- k := u.Key(h)
69
- ctx, _ := context.WithTimeout(context.TODO(), time.Second*5)
70
- b, err := n.Blocks.GetBlock(ctx, k)
89
+ Arguments: []cmds.Argument{
90
+ cmds.StringArg("key", true, false, "The base58 multihash of an existing block to get"),
91
+ },
92
+ Run: func(req cmds.Request) (interface{}, error) {
93
+ b, err := getBlockForKey(req, req.Arguments()[0])
94
if err != nil {
95
return nil, err
96
}
74
- log.Debugf("BlockGet key: '%q'", b.Key())
97
98
return bytes.NewReader(b.Data), nil
99
},
@@ -118,16 +140,39 @@ It reads from stdin, and <key> is a base58 encoded multihash.
140
return nil, err
141
}
142
121
- return &Block{
122
- Key: k.String(),
123
- Length: len(data),
143
+ return &BlockStat{
144
+ Key: k.String(),
145
+ Size: len(data),
146
}, nil
147
},
126
- Type: Block{},
148
Marshalers: cmds.MarshalerMap{
149
cmds.Text: func(res cmds.Response) (io.Reader, error) {
129
- block := res.Output().(*Block)
130
- return strings.NewReader(block.Key + "\n"), nil
150
+ bs := res.Output().(*BlockStat)
151
+ return strings.NewReader(bs.Key + "\n"), nil
152
},
153
},
154
}
155
+
156
+func getBlockForKey(req cmds.Request, key string) (*blocks.Block, error) {
157
+ n, err := req.Context().GetNode()
158
+ if err != nil {
159
+ return nil, err
160
+ }
161
+
162
+ if !u.IsValidHash(key) {
163
+ return nil, cmds.Error{"Not a valid hash", cmds.ErrClient}
164
+ }
165
+
166
+ h, err := mh.FromB58String(key)
167
+ if err != nil {
168
+ return nil, err
169
+ }
170
+
171
+ k := u.Key(h)
172
+ b, err := n.Blocks.GetBlock(context.TODO(), k)
173
+ if err != nil {
174
+ return nil, err
175
+ }
176
+ log.Debugf("ipfs block: got block with key: %q", b.Key())
177
+ return b, nil
178
+}
test/t0050-block.sh
+10
@@ -29,4 +29,14 @@ test_expect_success "'ipfs block get' output looks good" '
29
test_cmp expected_in actual_in
30
'
31
32
+test_expect_success "'ipfs block stat' succeeds" '
33
+ ipfs block stat $HASH >actual_stat
34
+'
35
+
36
+test_expect_success "'ipfs block get' output looks good" '
37
+ echo "Key: $HASH" >expected_stat &&
38
+ echo "Size: 12" >>expected_stat &&
39
+ test_cmp expected_stat actual_stat
40
+'
41
+
42
test_done