mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-24 03:47:45 +08:00
object put --pin option
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
parent
8ad3b11b91
commit
e1bc025e38
@ -16,6 +16,7 @@ import (
|
||||
core "github.com/ipfs/go-ipfs/core"
|
||||
dag "github.com/ipfs/go-ipfs/merkledag"
|
||||
path "github.com/ipfs/go-ipfs/path"
|
||||
pin "github.com/ipfs/go-ipfs/pin"
|
||||
ft "github.com/ipfs/go-ipfs/unixfs"
|
||||
|
||||
cid "gx/ipfs/QmTprEaAA2A9bst5XH7exuyi5KzNMK3SEDNN8rBDnKWcUS/go-cid"
|
||||
@ -355,6 +356,7 @@ And then run:
|
||||
Options: []cmds.Option{
|
||||
cmds.StringOption("inputenc", "Encoding type of input data. One of: {\"protobuf\", \"json\"}.").Default("json"),
|
||||
cmds.StringOption("datafieldenc", "Encoding type of the data field, either \"text\" or \"base64\".").Default("text"),
|
||||
cmds.BoolOption("pin", "Pin this object when adding.").Default(false),
|
||||
},
|
||||
Run: func(req cmds.Request, res cmds.Response) {
|
||||
n, err := req.InvocContext().GetNode()
|
||||
@ -381,6 +383,16 @@ And then run:
|
||||
return
|
||||
}
|
||||
|
||||
dopin, _, err := req.Option("pin").Bool()
|
||||
if err != nil {
|
||||
res.SetError(err, cmds.ErrNormal)
|
||||
return
|
||||
}
|
||||
|
||||
if dopin {
|
||||
defer n.Blockstore.PinLock().Unlock()
|
||||
}
|
||||
|
||||
output, err := objectPut(n, input, inputenc, datafieldenc)
|
||||
if err != nil {
|
||||
errType := cmds.ErrNormal
|
||||
@ -391,6 +403,21 @@ And then run:
|
||||
return
|
||||
}
|
||||
|
||||
if dopin {
|
||||
c, err := cid.Decode(output.Hash)
|
||||
if err != nil {
|
||||
res.SetError(err, cmds.ErrNormal)
|
||||
return
|
||||
}
|
||||
|
||||
n.Pinning.PinWithMode(c, pin.Recursive)
|
||||
err = n.Pinning.Flush()
|
||||
if err != nil {
|
||||
res.SetError(err, cmds.ErrNormal)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
res.SetOutput(output)
|
||||
},
|
||||
Marshalers: cmds.MarshalerMap{
|
||||
|
||||
@ -168,7 +168,23 @@ test_object_cmd() {
|
||||
test_cmp expected actual
|
||||
'
|
||||
|
||||
test_expect_success "'ipfs object patch' should work (no unixfs-dir)" '
|
||||
test_expect_success "'ipfs object put --pin' succeeds" '
|
||||
HASH="QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V" &&
|
||||
echo "added $HASH" >expected &&
|
||||
echo "{ \"Data\": \"abc\" }" | ipfs object put --pin >actual
|
||||
'
|
||||
|
||||
test_expect_success "'ipfs object put --pin' output looks good" '
|
||||
echo "added $HASH" >expected &&
|
||||
test_cmp expected actual
|
||||
'
|
||||
|
||||
test_expect_success "after gc, objects still acessible" '
|
||||
ipfs repo gc > /dev/null &&
|
||||
ipfs refs -r --timeout=2s $HASH > /dev/null
|
||||
'
|
||||
|
||||
test_expect_success "'ipfs object patch' should work (no unixfs-dir)" '
|
||||
EMPTY_DIR=$(ipfs object new) &&
|
||||
OUTPUT=$(ipfs object patch $EMPTY_DIR add-link foo $EMPTY_DIR) &&
|
||||
ipfs object stat $OUTPUT
|
||||
|
||||
Loading…
Reference in New Issue
Block a user