From 8d4de98ca4f53097960ac2cdbee3d59a08169173 Mon Sep 17 00:00:00 2001 From: Overbool Date: Sat, 15 Dec 2018 11:14:29 +0800 Subject: [PATCH] cmds/pin: modify test License: MIT Signed-off-by: Overbool --- core/commands/pin.go | 91 +++++++++------------------ core/coreapi/interface/options/pin.go | 1 - core/coreapi/pin.go | 2 - core/corerepo/pinning.go | 0 test/sharness/t0080-repo.sh | 4 +- 5 files changed, 30 insertions(+), 68 deletions(-) delete mode 100644 core/corerepo/pinning.go diff --git a/core/commands/pin.go b/core/commands/pin.go index baa04138549d..322a7f878136 100644 --- a/core/commands/pin.go +++ b/core/commands/pin.go @@ -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{ @@ -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 { @@ -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) @@ -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 { @@ -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 - }, + }), }, } @@ -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 { @@ -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 }), }, diff --git a/core/coreapi/interface/options/pin.go b/core/coreapi/interface/options/pin.go index 630b561de4c0..cc4a8ef29662 100644 --- a/core/coreapi/interface/options/pin.go +++ b/core/coreapi/interface/options/pin.go @@ -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 { diff --git a/core/coreapi/pin.go b/core/coreapi/pin.go index 9f39df4e44a8..8a44066c2aac 100644 --- a/core/coreapi/pin.go +++ b/core/coreapi/pin.go @@ -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 diff --git a/core/corerepo/pinning.go b/core/corerepo/pinning.go deleted file mode 100644 index e69de29bb2d1..000000000000 diff --git a/test/sharness/t0080-repo.sh b/test/sharness/t0080-repo.sh index 16e3042170a2..3606b5318c3c 100755 --- a/test/sharness/t0080-repo.sh +++ b/test/sharness/t0080-repo.sh @@ -93,8 +93,8 @@ test_expect_success "pinning directly should fail now" ' ' test_expect_success "'ipfs pin rm -r=false ' 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 '