@cryptotaxi247 / kubo / commits / 7be8475c9

block cmd: allow adding multiple blocks at once

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>

Łukasz Magiera committed May 14, 2019 at 23:28 UTC 7be8475c9b4045c5ad9ac54308ed679344507b64
2 files changed +39 -16
core/commands/block.go
+26 -16
@@ -6,6 +6,8 @@ import (
6 "io"
7 "os"
8
9 + files "github.com/ipfs/go-ipfs-files"
10 +
11 util "github.com/ipfs/go-ipfs/blocks/blockstoreutil"
12 cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
13
@@ -129,7 +131,7 @@ than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
131 },
132
133 Arguments: []cmds.Argument{
132 - cmds.FileArg("data", true, false, "The data to be stored as an IPFS block.").EnableStdin(),
134 + cmds.FileArg("data", true, true, "The data to be stored as an IPFS block.").EnableStdin(),
135 },
136 Options: []cmds.Option{
137 cmds.StringOption(blockFormatOptionName, "f", "cid format for blocks to be created with."),
@@ -143,11 +145,6 @@ than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
145 return err
146 }
147
146 - file, err := cmdenv.GetFileArg(req.Files.Entries())
147 - if err != nil {
148 - return err
149 - }
150 -
148 mhtype, _ := req.Options[mhtypeOptionName].(string)
149 mhtval, ok := mh.Names[mhtype]
150 if !ok {
@@ -170,18 +167,31 @@ than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
167
168 pin, _ := req.Options[pinOptionName].(bool)
169
173 - p, err := api.Block().Put(req.Context, file,
174 - options.Block.Hash(mhtval, mhlen),
175 - options.Block.Format(format),
176 - options.Block.Pin(pin))
177 - if err != nil {
178 - return err
170 + it := req.Files.Entries()
171 + for it.Next() {
172 + file := files.FileFromEntry(it)
173 + if file == nil {
174 + return errors.New("expected a file")
175 + }
176 +
177 + p, err := api.Block().Put(req.Context, file,
178 + options.Block.Hash(mhtval, mhlen),
179 + options.Block.Format(format),
180 + options.Block.Pin(pin))
181 + if err != nil {
182 + return err
183 + }
184 +
185 + err = res.Emit(&BlockStat{
186 + Key: p.Path().Cid().String(),
187 + Size: p.Size(),
188 + })
189 + if err != nil {
190 + return err
191 + }
192 }
193
181 - return cmds.EmitOnce(res, &BlockStat{
182 - Key: p.Path().Cid().String(),
183 - Size: p.Size(),
184 - })
194 + return it.Err()
195 },
196 Encoders: cmds.EncoderMap{
197 cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, bs *BlockStat) error {
test/sharness/t0050-block.sh
+13
@@ -11,6 +11,7 @@ test_description="Test block command"
11 test_init_ipfs
12
13 HASH="QmRKqGMAM6EZngbpjSqrvYzq5Qd8b1bSWymjSUY9zQSNDk"
14 +HASHB="QmdnpnsaEj69isdw5sNzp3h3HkaDz7xKq7BmvFFBzNr5e7"
15
16 #
17 # "block put tests"
@@ -26,6 +27,18 @@ test_expect_success "'ipfs block put' output looks good" '
27 test_cmp expected_out actual_out
28 '
29
30 +test_expect_success "'ipfs block put' with 2 files succeeds" '
31 + echo "Hello Mars!" > a &&
32 + echo "Hello Venus!" > b &&
33 + ipfs block put a b >actual_out
34 +'
35 +
36 +test_expect_success "'ipfs block put' output looks good" '
37 + echo "$HASH" >expected_out &&
38 + echo "$HASHB" >>expected_out &&
39 + test_cmp expected_out actual_out
40 +'
41 +
42 #
43 # "block get" tests
44 #