Skip to content

Commit

Permalink
block cmd: allow adding multiple blocks at once
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
  • Loading branch information
magik6k authored and Stebalien committed Jun 4, 2019
1 parent ff3efe8 commit 7be8475
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
42 changes: 26 additions & 16 deletions core/commands/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"io"
"os"

files "github.com/ipfs/go-ipfs-files"

util "github.com/ipfs/go-ipfs/blocks/blockstoreutil"
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"

Expand Down Expand Up @@ -129,7 +131,7 @@ than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
},

Arguments: []cmds.Argument{
cmds.FileArg("data", true, false, "The data to be stored as an IPFS block.").EnableStdin(),
cmds.FileArg("data", true, true, "The data to be stored as an IPFS block.").EnableStdin(),
},
Options: []cmds.Option{
cmds.StringOption(blockFormatOptionName, "f", "cid format for blocks to be created with."),
Expand All @@ -143,11 +145,6 @@ than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
return err
}

file, err := cmdenv.GetFileArg(req.Files.Entries())
if err != nil {
return err
}

mhtype, _ := req.Options[mhtypeOptionName].(string)
mhtval, ok := mh.Names[mhtype]
if !ok {
Expand All @@ -170,18 +167,31 @@ than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.

pin, _ := req.Options[pinOptionName].(bool)

p, err := api.Block().Put(req.Context, file,
options.Block.Hash(mhtval, mhlen),
options.Block.Format(format),
options.Block.Pin(pin))
if err != nil {
return err
it := req.Files.Entries()
for it.Next() {
file := files.FileFromEntry(it)
if file == nil {
return errors.New("expected a file")
}

p, err := api.Block().Put(req.Context, file,
options.Block.Hash(mhtval, mhlen),
options.Block.Format(format),
options.Block.Pin(pin))
if err != nil {
return err
}

err = res.Emit(&BlockStat{
Key: p.Path().Cid().String(),
Size: p.Size(),
})
if err != nil {
return err
}
}

return cmds.EmitOnce(res, &BlockStat{
Key: p.Path().Cid().String(),
Size: p.Size(),
})
return it.Err()
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, bs *BlockStat) error {
Expand Down
13 changes: 13 additions & 0 deletions test/sharness/t0050-block.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ test_description="Test block command"
test_init_ipfs

HASH="QmRKqGMAM6EZngbpjSqrvYzq5Qd8b1bSWymjSUY9zQSNDk"
HASHB="QmdnpnsaEj69isdw5sNzp3h3HkaDz7xKq7BmvFFBzNr5e7"

#
# "block put tests"
Expand All @@ -26,6 +27,18 @@ test_expect_success "'ipfs block put' output looks good" '
test_cmp expected_out actual_out
'

test_expect_success "'ipfs block put' with 2 files succeeds" '
echo "Hello Mars!" > a &&
echo "Hello Venus!" > b &&
ipfs block put a b >actual_out
'

test_expect_success "'ipfs block put' output looks good" '
echo "$HASH" >expected_out &&
echo "$HASHB" >>expected_out &&
test_cmp expected_out actual_out
'

#
# "block get" tests
#
Expand Down

0 comments on commit 7be8475

Please sign in to comment.