Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

block cmd: allow adding multiple blocks at once #6331

Merged
merged 1 commit into from
Jun 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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