提交 7be8475c 作者: Łukasz Magiera 提交者: Steven Allen

block cmd: allow adding multiple blocks at once

License: MIT
Signed-off-by: 's avatarŁukasz Magiera <magik6k@gmail.com>
上级 ff3efe84
...@@ -6,6 +6,8 @@ import ( ...@@ -6,6 +6,8 @@ import (
"io" "io"
"os" "os"
files "github.com/ipfs/go-ipfs-files"
util "github.com/ipfs/go-ipfs/blocks/blockstoreutil" util "github.com/ipfs/go-ipfs/blocks/blockstoreutil"
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv" cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
...@@ -129,7 +131,7 @@ than 'sha2-256' or format to anything other than 'v0' will result in CIDv1. ...@@ -129,7 +131,7 @@ than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
}, },
Arguments: []cmds.Argument{ 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{ Options: []cmds.Option{
cmds.StringOption(blockFormatOptionName, "f", "cid format for blocks to be created with."), 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. ...@@ -143,11 +145,6 @@ than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
return err return err
} }
file, err := cmdenv.GetFileArg(req.Files.Entries())
if err != nil {
return err
}
mhtype, _ := req.Options[mhtypeOptionName].(string) mhtype, _ := req.Options[mhtypeOptionName].(string)
mhtval, ok := mh.Names[mhtype] mhtval, ok := mh.Names[mhtype]
if !ok { if !ok {
...@@ -170,18 +167,31 @@ than 'sha2-256' or format to anything other than 'v0' will result in CIDv1. ...@@ -170,18 +167,31 @@ than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
pin, _ := req.Options[pinOptionName].(bool) pin, _ := req.Options[pinOptionName].(bool)
p, err := api.Block().Put(req.Context, file, it := req.Files.Entries()
options.Block.Hash(mhtval, mhlen), for it.Next() {
options.Block.Format(format), file := files.FileFromEntry(it)
options.Block.Pin(pin)) if file == nil {
if err != nil { return errors.New("expected a file")
return err }
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{ return it.Err()
Key: p.Path().Cid().String(),
Size: p.Size(),
})
}, },
Encoders: cmds.EncoderMap{ Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, bs *BlockStat) error { cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, bs *BlockStat) error {
......
...@@ -11,6 +11,7 @@ test_description="Test block command" ...@@ -11,6 +11,7 @@ test_description="Test block command"
test_init_ipfs test_init_ipfs
HASH="QmRKqGMAM6EZngbpjSqrvYzq5Qd8b1bSWymjSUY9zQSNDk" HASH="QmRKqGMAM6EZngbpjSqrvYzq5Qd8b1bSWymjSUY9zQSNDk"
HASHB="QmdnpnsaEj69isdw5sNzp3h3HkaDz7xKq7BmvFFBzNr5e7"
# #
# "block put tests" # "block put tests"
...@@ -26,6 +27,18 @@ test_expect_success "'ipfs block put' output looks good" ' ...@@ -26,6 +27,18 @@ test_expect_success "'ipfs block put' output looks good" '
test_cmp expected_out actual_out 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 # "block get" tests
# #
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论