提交 7a08cf9c 作者: Overbool 提交者: Steven Allen

cmds/pin: modify test

License: MIT
Signed-off-by: 's avatarOverbool <overbool.xu@gmail.com>
上级 99feecfd
...@@ -7,22 +7,22 @@ import ( ...@@ -7,22 +7,22 @@ import (
"os" "os"
"time" "time"
core "github.com/ipfs/go-ipfs/core" "github.com/ipfs/go-ipfs/core"
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv" "github.com/ipfs/go-ipfs/core/commands/cmdenv"
e "github.com/ipfs/go-ipfs/core/commands/e" "github.com/ipfs/go-ipfs/core/commands/e"
"github.com/ipfs/go-ipfs/core/coreapi/interface"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
iface "github.com/ipfs/go-ipfs/core/coreapi/interface" "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
options "github.com/ipfs/go-ipfs/core/coreapi/interface/options" "github.com/ipfs/go-ipfs/pin"
pin "github.com/ipfs/go-ipfs/pin"
cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid" "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
bserv "gx/ipfs/QmVKQHuzni68SWByzJgBUCwHvvr4TWiXfutNWWwpZpp4rE/go-blockservice" bserv "gx/ipfs/QmVKQHuzni68SWByzJgBUCwHvvr4TWiXfutNWWwpZpp4rE/go-blockservice"
cmds "gx/ipfs/QmWGm4AbZEbnmdgVTza52MSNpEmBdFVqzmAysRbjrRyGbH/go-ipfs-cmds" "gx/ipfs/QmWGm4AbZEbnmdgVTza52MSNpEmBdFVqzmAysRbjrRyGbH/go-ipfs-cmds"
"gx/ipfs/QmYMQuypUbgsdNHmuCBSUJV6wdQVsBHRivNAp3efHJwZJD/go-verifcid" "gx/ipfs/QmYMQuypUbgsdNHmuCBSUJV6wdQVsBHRivNAp3efHJwZJD/go-verifcid"
offline "gx/ipfs/QmYZwey1thDTynSrvd6qQkX24UpTka6TFhQ2v569UpoqxD/go-ipfs-exchange-offline" "gx/ipfs/QmYZwey1thDTynSrvd6qQkX24UpTka6TFhQ2v569UpoqxD/go-ipfs-exchange-offline"
dag "gx/ipfs/Qmb2UEG2TAeVrEJSjqsZF7Y2he7wRDkrdt6c3bECxwZf4k/go-merkledag" dag "gx/ipfs/Qmb2UEG2TAeVrEJSjqsZF7Y2he7wRDkrdt6c3bECxwZf4k/go-merkledag"
cidenc "gx/ipfs/QmdPQx9fvN5ExVwMhRmh7YpCQJzJrFhd1AjVBwJmRMFJeX/go-cidutil/cidenc" "gx/ipfs/QmdPQx9fvN5ExVwMhRmh7YpCQJzJrFhd1AjVBwJmRMFJeX/go-cidutil/cidenc"
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit" "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
) )
var PinCmd = &cmds.Command{ var PinCmd = &cmds.Command{
...@@ -68,18 +68,11 @@ var addPinCmd = &cmds.Command{ ...@@ -68,18 +68,11 @@ var addPinCmd = &cmds.Command{
}, },
Type: AddPinOutput{}, Type: AddPinOutput{},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
n, err := cmdenv.GetNode(env)
if err != nil {
return err
}
api, err := cmdenv.GetApi(env, req) api, err := cmdenv.GetApi(env, req)
if err != nil { if err != nil {
return err return err
} }
defer n.Blockstore.PinLock().Unlock()
// set recursive flag // set recursive flag
recursive, _ := req.Options[pinRecursiveOptionName].(bool) recursive, _ := req.Options[pinRecursiveOptionName].(bool)
showProgress, _ := req.Options[pinProgressOptionName].(bool) showProgress, _ := req.Options[pinProgressOptionName].(bool)
...@@ -88,8 +81,13 @@ var addPinCmd = &cmds.Command{ ...@@ -88,8 +81,13 @@ var addPinCmd = &cmds.Command{
return err return err
} }
enc, err := cmdenv.GetCidEncoder(req)
if err != nil {
return err
}
if !showProgress { if !showProgress {
added, err := pinAddMany(req.Context, api, req.Arguments, recursive) added, err := pinAddMany(req.Context, api, enc, req.Arguments, recursive)
if err != nil { if err != nil {
return err return err
} }
...@@ -107,7 +105,7 @@ var addPinCmd = &cmds.Command{ ...@@ -107,7 +105,7 @@ var addPinCmd = &cmds.Command{
ch := make(chan pinResult, 1) ch := make(chan pinResult, 1)
go func() { go func() {
added, err := pinAddMany(req.Context, api, req.Arguments, recursive) added, err := pinAddMany(ctx, api, enc, req.Arguments, recursive)
ch <- pinResult{pins: added, err: err} ch <- pinResult{pins: added, err: err}
}() }()
...@@ -183,7 +181,7 @@ var addPinCmd = &cmds.Command{ ...@@ -183,7 +181,7 @@ var addPinCmd = &cmds.Command{
}, },
} }
func pinAddMany(ctx context.Context, api coreiface.CoreAPI, paths []string, recursive bool) ([]string, error) { func pinAddMany(ctx context.Context, api coreiface.CoreAPI, enc cidenc.Encoder, paths []string, recursive bool) ([]string, error) {
added := make([]string, len(paths)) added := make([]string, len(paths))
for i, b := range paths { for i, b := range paths {
p, err := coreiface.ParsePath(b) p, err := coreiface.ParsePath(b)
...@@ -196,10 +194,10 @@ func pinAddMany(ctx context.Context, api coreiface.CoreAPI, paths []string, recu ...@@ -196,10 +194,10 @@ func pinAddMany(ctx context.Context, api coreiface.CoreAPI, paths []string, recu
return nil, err return nil, err
} }
if err := api.Pin().Add(ctx, p, options.Pin.Recursive(recursive)); err != nil { if err := api.Pin().Add(ctx, rp, options.Pin.Recursive(recursive)); err != nil {
return nil, err return nil, err
} }
added[i] = rp.Cid().String() added[i] = enc.Encode(rp.Cid())
} }
return added, nil return added, nil
...@@ -239,6 +237,7 @@ collected if needed. (By default, recursively. Use -r=false for direct pins.) ...@@ -239,6 +237,7 @@ collected if needed. (By default, recursively. Use -r=false for direct pins.)
return err return err
} }
pins := make([]string, 0, len(req.Arguments))
for _, b := range req.Arguments { for _, b := range req.Arguments {
p, err := coreiface.ParsePath(b) p, err := coreiface.ParsePath(b)
if err != nil { if err != nil {
...@@ -250,51 +249,23 @@ collected if needed. (By default, recursively. Use -r=false for direct pins.) ...@@ -250,51 +249,23 @@ collected if needed. (By default, recursively. Use -r=false for direct pins.)
return err return err
} }
id := enc.Encode(rp.Cid())
pins = append(pins, id)
if err := api.Pin().Rm(req.Context, rp, options.Pin.RmRecursive(recursive)); err != nil { if err := api.Pin().Rm(req.Context, rp, options.Pin.RmRecursive(recursive)); err != nil {
if err := res.Emit(&PinOutput{
Pins: []string{rp.Cid().String()},
Error: err.Error(),
}); err != nil {
return err
}
continue
}
if err := res.Emit(&PinOutput{
Pins: []string{rp.Cid().String()},
}); err != nil {
return err return err
} }
} }
return nil return cmds.EmitOnce(res, &PinOutput{pins})
}, },
PostRun: cmds.PostRunMap{ Encoders: cmds.EncoderMap{
cmds.CLI: func(res cmds.Response, re cmds.ResponseEmitter) error { cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *PinOutput) error {
failed := false for _, k := range out.Pins {
for { fmt.Fprintf(w, "unpinned %s\n", k)
out, err := res.Next()
if err == io.EOF {
break
} else if err != nil {
return err
}
r := out.(*PinOutput)
if r.Pins == nil && r.Error != "" {
return fmt.Errorf("aborted: %s", r.Error)
} else if r.Error != "" {
failed = true
fmt.Fprintf(os.Stderr, "cannot unpin %s: %s\n", r.Pins[0], r.Error)
} else {
fmt.Fprintf(os.Stdout, "unpinned %s\n", r.Pins[0])
}
} }
if failed {
return fmt.Errorf("some hash not unpinned")
}
return nil return nil
}, }),
}, },
} }
......
...@@ -11,7 +11,6 @@ type PinLsSettings struct { ...@@ -11,7 +11,6 @@ type PinLsSettings struct {
// PinRmSettings represents the settings of pin rm command // PinRmSettings represents the settings of pin rm command
type PinRmSettings struct { type PinRmSettings struct {
Recursive bool Recursive bool
Force bool
} }
type PinUpdateSettings struct { type PinUpdateSettings struct {
......
...@@ -6,13 +6,11 @@ import ( ...@@ -6,13 +6,11 @@ import (
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options" caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
corerepo "github.com/ipfs/go-ipfs/core/corerepo"
bserv "gx/ipfs/QmVKQHuzni68SWByzJgBUCwHvvr4TWiXfutNWWwpZpp4rE/go-blockservice" bserv "gx/ipfs/QmVKQHuzni68SWByzJgBUCwHvvr4TWiXfutNWWwpZpp4rE/go-blockservice"
merkledag "gx/ipfs/Qmb2UEG2TAeVrEJSjqsZF7Y2he7wRDkrdt6c3bECxwZf4k/go-merkledag" merkledag "gx/ipfs/Qmb2UEG2TAeVrEJSjqsZF7Y2he7wRDkrdt6c3bECxwZf4k/go-merkledag"
cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid" cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
offline "gx/ipfs/QmYZwey1thDTynSrvd6qQkX24UpTka6TFhQ2v569UpoqxD/go-ipfs-exchange-offline" offline "gx/ipfs/QmYZwey1thDTynSrvd6qQkX24UpTka6TFhQ2v569UpoqxD/go-ipfs-exchange-offline"
merkledag "gx/ipfs/QmdV35UHnL1FM52baPkeUo6u7Fxm2CRUkPTLRPxeF8a4Ap/go-merkledag"
) )
type PinAPI CoreAPI type PinAPI CoreAPI
......
...@@ -93,7 +93,7 @@ test_expect_success "pinning directly should fail now" ' ...@@ -93,7 +93,7 @@ test_expect_success "pinning directly should fail now" '
' '
test_expect_success "'ipfs pin rm -r=false <hash>' should fail" ' test_expect_success "'ipfs pin rm -r=false <hash>' should fail" '
echo "Error: $HASH is pinned recursively" >expected4 && echo "Error: $HASH is pinned recursively" >expected4
test_must_fail ipfs pin rm -r=false "$HASH" 2>actual4 && test_must_fail ipfs pin rm -r=false "$HASH" 2>actual4 &&
test_cmp expected4 actual4 test_cmp expected4 actual4
' '
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论