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 <[email protected]>
  • Loading branch information
overbool committed Jan 16, 2019
1 parent 12fc856 commit 8d4de98
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 68 deletions.
91 changes: 28 additions & 63 deletions core/commands/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ 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"
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
"gx/ipfs/QmYZwey1thDTynSrvd6qQkX24UpTka6TFhQ2v569UpoqxD/go-ipfs-exchange-offline"
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
)

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

type PinOutput struct {
Pins []string
Error string
}

// PinUpdateOutput represents the pin update output
type PinUpdateOutput struct {
From string
To string
Error map[string]string `json:",omitempty"`
}

type AddPinOutput struct {
Expand Down Expand Up @@ -74,18 +68,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 Down Expand Up @@ -240,6 +227,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 @@ -251,51 +240,27 @@ collected if needed. (By default, recursively. Use -r=false for direct pins.)
return err
}

id := rp.Cid().String()
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 Expand Up @@ -431,7 +396,7 @@ new pin and removing the old one.
Options: []cmdkit.Option{
cmdkit.BoolOption(pinUnpinOptionName, "Remove the old pin.").WithDefault(true),
},
Type: PinUpdateOutput{},
Type: PinOutput{},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env, req)
if err != nil {
Expand All @@ -455,11 +420,11 @@ new pin and removing the old one.
return err
}

return cmds.EmitOnce(res, &PinUpdateOutput{From: from.String(), To: to.String()})
return cmds.EmitOnce(res, &PinOutput{Pins: []string{from.String(), to.String()}})
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *PinUpdateOutput) error {
fmt.Fprintf(w, "updated %s to %s\n", out.From, out.To)
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *PinOutput) error {
fmt.Fprintf(w, "updated %s to %s\n", out.Pins[0], out.Pins[1])
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 8d4de98

Please sign in to comment.