Skip to content

Commit

Permalink
cmds/pin: modify test
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Overbool <overbool.xu@gmail.com>
  • Loading branch information
overbool committed Jan 22, 2019
1 parent 743817d commit c7fd7b2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 63 deletions.
93 changes: 35 additions & 58 deletions core/commands/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ import (
"os"
"time"

core "github.com/ipfs/go-ipfs/core"
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
e "github.com/ipfs/go-ipfs/core/commands/e"
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/commands/cmdenv"
"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"
iface "github.com/ipfs/go-ipfs/core/coreapi/interface"
options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
pin "github.com/ipfs/go-ipfs/pin"
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
"github.com/ipfs/go-ipfs/pin"

cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
"gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
dag "gx/ipfs/QmTQdH4848iTVCJmKXYyRiK72HufWTLYQQ8iN3JaQ8K1Hq/go-merkledag"
cmds "gx/ipfs/QmWGm4AbZEbnmdgVTza52MSNpEmBdFVqzmAysRbjrRyGbH/go-ipfs-cmds"
"gx/ipfs/QmWGm4AbZEbnmdgVTza52MSNpEmBdFVqzmAysRbjrRyGbH/go-ipfs-cmds"
"gx/ipfs/QmYMQuypUbgsdNHmuCBSUJV6wdQVsBHRivNAp3efHJwZJD/go-verifcid"
bserv "gx/ipfs/QmYPZzd9VqmJDwxUnThfeSbV1Y5o53aVPDijTB7j7rS9Ep/go-blockservice"
offline "gx/ipfs/QmYZwey1thDTynSrvd6qQkX24UpTka6TFhQ2v569UpoqxD/go-ipfs-exchange-offline"
cidenc "gx/ipfs/QmdPQx9fvN5ExVwMhRmh7YpCQJzJrFhd1AjVBwJmRMFJeX/go-cidutil/cidenc"
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
"gx/ipfs/QmYZwey1thDTynSrvd6qQkX24UpTka6TFhQ2v569UpoqxD/go-ipfs-exchange-offline"
"gx/ipfs/QmdPQx9fvN5ExVwMhRmh7YpCQJzJrFhd1AjVBwJmRMFJeX/go-cidutil/cidenc"
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
)

var PinCmd = &cmds.Command{
Expand All @@ -40,7 +40,8 @@ var PinCmd = &cmds.Command{
}

type PinOutput struct {
Pins []string
Pins []string
Error map[string]string `json:",omitempty"`
}

type AddPinOutput struct {
Expand Down Expand Up @@ -68,18 +69,11 @@ var addPinCmd = &cmds.Command{
},
Type: AddPinOutput{},
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)
if err != nil {
return err
}

defer n.Blockstore.PinLock().Unlock()

// set recursive flag
recursive, _ := req.Options[pinRecursiveOptionName].(bool)
showProgress, _ := req.Options[pinProgressOptionName].(bool)
Expand All @@ -88,8 +82,13 @@ var addPinCmd = &cmds.Command{
return err
}

enc, err := cmdenv.GetCidEncoder(req)
if err != nil {
return err
}

if !showProgress {
added, err := pinAddMany(req.Context, api, req.Arguments, recursive)
added, err := pinAddMany(req.Context, api, enc, req.Arguments, recursive)
if err != nil {
return err
}
Expand All @@ -107,7 +106,7 @@ var addPinCmd = &cmds.Command{

ch := make(chan pinResult, 1)
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}
}()

Expand Down Expand Up @@ -183,7 +182,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))
for i, b := range paths {
p, err := coreiface.ParsePath(b)
Expand All @@ -199,7 +198,7 @@ func pinAddMany(ctx context.Context, api coreiface.CoreAPI, paths []string, recu
if err := api.Pin().Add(ctx, p, options.Pin.Recursive(recursive)); err != nil {
return nil, err
}
added[i] = rp.Cid().String()
added[i] = enc.Encode(rp.Cid())
}

return added, nil
Expand Down Expand Up @@ -239,6 +238,8 @@ collected if needed. (By default, recursively. Use -r=false for direct pins.)
return err
}

pins := make([]string, 0, len(req.Arguments))
errs := make(map[string]string)
for _, b := range req.Arguments {
p, err := coreiface.ParsePath(b)
if err != nil {
Expand All @@ -250,51 +251,27 @@ collected if needed. (By default, recursively. Use -r=false for direct pins.)
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 := 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
errs[id] = err.Error()
}
}

return nil
return cmds.EmitOnce(res, &PinOutput{pins, errs})
},
PostRun: cmds.PostRunMap{
cmds.CLI: func(res cmds.Response, re cmds.ResponseEmitter) error {
failed := false
for {
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)
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *PinOutput) error {
for _, k := range out.Pins {
if err, ok := out.Error[k]; !ok {
fmt.Fprintf(w, "unpinned %s\n", k)
} else {
fmt.Fprintf(os.Stdout, "unpinned %s\n", r.Pins[0])
fmt.Fprintf(os.Stderr, "cannot unpin %s: %s\n", k, err)
}
}

if failed {
return fmt.Errorf("some hash not unpinned")
}
return nil
},
}),
},
}

Expand Down
1 change: 0 additions & 1 deletion core/coreapi/interface/options/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ type PinLsSettings struct {
// PinRmSettings represents the settings of pin rm command
type PinRmSettings struct {
Recursive bool
Force bool
}

type PinUpdateSettings struct {
Expand Down
2 changes: 0 additions & 2 deletions core/coreapi/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import (

coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
corerepo "github.com/ipfs/go-ipfs/core/corerepo"
merkledag "gx/ipfs/QmTQdH4848iTVCJmKXYyRiK72HufWTLYQQ8iN3JaQ8K1Hq/go-merkledag"
bserv "gx/ipfs/QmYPZzd9VqmJDwxUnThfeSbV1Y5o53aVPDijTB7j7rS9Ep/go-blockservice"

cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
offline "gx/ipfs/QmYZwey1thDTynSrvd6qQkX24UpTka6TFhQ2v569UpoqxD/go-ipfs-exchange-offline"
merkledag "gx/ipfs/QmdV35UHnL1FM52baPkeUo6u7Fxm2CRUkPTLRPxeF8a4Ap/go-merkledag"
)

type PinAPI CoreAPI
Expand Down
Empty file removed core/corerepo/pinning.go
Empty file.
4 changes: 2 additions & 2 deletions test/sharness/t0080-repo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ test_expect_success "pinning directly should fail now" '
'

test_expect_success "'ipfs pin rm -r=false <hash>' should fail" '
echo "Error: $HASH is pinned recursively" >expected4 &&
test_must_fail ipfs pin rm -r=false "$HASH" 2>actual4 &&
echo "cannot unpin $HASH: $HASH is pinned recursively" >expected4
ipfs pin rm -r=false "$HASH" 2>actual4 &&
test_cmp expected4 actual4
'

Expand Down

0 comments on commit c7fd7b2

Please sign in to comment.