Skip to content

Commit b10dd5b

Browse files
committed
cmds/pin: modify test
License: MIT Signed-off-by: Overbool <[email protected]>
1 parent 743817d commit b10dd5b

File tree

5 files changed

+38
-64
lines changed

5 files changed

+38
-64
lines changed

core/commands/pin.go

+36-59
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@ import (
77
"os"
88
"time"
99

10-
core "github.com/ipfs/go-ipfs/core"
11-
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
12-
e "github.com/ipfs/go-ipfs/core/commands/e"
10+
"github.com/ipfs/go-ipfs/core"
11+
"github.com/ipfs/go-ipfs/core/commands/cmdenv"
12+
"github.com/ipfs/go-ipfs/core/commands/e"
13+
"github.com/ipfs/go-ipfs/core/coreapi/interface"
1314
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
14-
iface "github.com/ipfs/go-ipfs/core/coreapi/interface"
15-
options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
16-
pin "github.com/ipfs/go-ipfs/pin"
15+
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
16+
"github.com/ipfs/go-ipfs/pin"
1717

18-
cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
18+
"gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
1919
dag "gx/ipfs/QmTQdH4848iTVCJmKXYyRiK72HufWTLYQQ8iN3JaQ8K1Hq/go-merkledag"
20-
cmds "gx/ipfs/QmWGm4AbZEbnmdgVTza52MSNpEmBdFVqzmAysRbjrRyGbH/go-ipfs-cmds"
20+
"gx/ipfs/QmWGm4AbZEbnmdgVTza52MSNpEmBdFVqzmAysRbjrRyGbH/go-ipfs-cmds"
2121
"gx/ipfs/QmYMQuypUbgsdNHmuCBSUJV6wdQVsBHRivNAp3efHJwZJD/go-verifcid"
2222
bserv "gx/ipfs/QmYPZzd9VqmJDwxUnThfeSbV1Y5o53aVPDijTB7j7rS9Ep/go-blockservice"
23-
offline "gx/ipfs/QmYZwey1thDTynSrvd6qQkX24UpTka6TFhQ2v569UpoqxD/go-ipfs-exchange-offline"
24-
cidenc "gx/ipfs/QmdPQx9fvN5ExVwMhRmh7YpCQJzJrFhd1AjVBwJmRMFJeX/go-cidutil/cidenc"
25-
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
23+
"gx/ipfs/QmYZwey1thDTynSrvd6qQkX24UpTka6TFhQ2v569UpoqxD/go-ipfs-exchange-offline"
24+
"gx/ipfs/QmdPQx9fvN5ExVwMhRmh7YpCQJzJrFhd1AjVBwJmRMFJeX/go-cidutil/cidenc"
25+
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
2626
)
2727

2828
var PinCmd = &cmds.Command{
@@ -40,7 +40,8 @@ var PinCmd = &cmds.Command{
4040
}
4141

4242
type PinOutput struct {
43-
Pins []string
43+
Pins []string
44+
Error map[string]string `json:",omitempty"`
4445
}
4546

4647
type AddPinOutput struct {
@@ -68,18 +69,11 @@ var addPinCmd = &cmds.Command{
6869
},
6970
Type: AddPinOutput{},
7071
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
71-
n, err := cmdenv.GetNode(env)
72-
if err != nil {
73-
return err
74-
}
75-
7672
api, err := cmdenv.GetApi(env, req)
7773
if err != nil {
7874
return err
7975
}
8076

81-
defer n.Blockstore.PinLock().Unlock()
82-
8377
// set recursive flag
8478
recursive, _ := req.Options[pinRecursiveOptionName].(bool)
8579
showProgress, _ := req.Options[pinProgressOptionName].(bool)
@@ -88,8 +82,13 @@ var addPinCmd = &cmds.Command{
8882
return err
8983
}
9084

85+
enc, err := cmdenv.GetCidEncoder(req)
86+
if err != nil {
87+
return err
88+
}
89+
9190
if !showProgress {
92-
added, err := pinAddMany(req.Context, api, req.Arguments, recursive)
91+
added, err := pinAddMany(req.Context, api, enc, req.Arguments, recursive)
9392
if err != nil {
9493
return err
9594
}
@@ -107,7 +106,7 @@ var addPinCmd = &cmds.Command{
107106

108107
ch := make(chan pinResult, 1)
109108
go func() {
110-
added, err := pinAddMany(req.Context, api, req.Arguments, recursive)
109+
added, err := pinAddMany(ctx, api, enc, req.Arguments, recursive)
111110
ch <- pinResult{pins: added, err: err}
112111
}()
113112

@@ -183,7 +182,7 @@ var addPinCmd = &cmds.Command{
183182
},
184183
}
185184

186-
func pinAddMany(ctx context.Context, api coreiface.CoreAPI, paths []string, recursive bool) ([]string, error) {
185+
func pinAddMany(ctx context.Context, api coreiface.CoreAPI, enc cidenc.Encoder, paths []string, recursive bool) ([]string, error) {
187186
added := make([]string, len(paths))
188187
for i, b := range paths {
189188
p, err := coreiface.ParsePath(b)
@@ -196,10 +195,10 @@ func pinAddMany(ctx context.Context, api coreiface.CoreAPI, paths []string, recu
196195
return nil, err
197196
}
198197

199-
if err := api.Pin().Add(ctx, p, options.Pin.Recursive(recursive)); err != nil {
198+
if err := api.Pin().Add(ctx, rp, options.Pin.Recursive(recursive)); err != nil {
200199
return nil, err
201200
}
202-
added[i] = rp.Cid().String()
201+
added[i] = enc.Encode(rp.Cid())
203202
}
204203

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

241+
pins := make([]string, 0, len(req.Arguments))
242+
errs := make(map[string]string)
242243
for _, b := range req.Arguments {
243244
p, err := coreiface.ParsePath(b)
244245
if err != nil {
@@ -250,51 +251,27 @@ collected if needed. (By default, recursively. Use -r=false for direct pins.)
250251
return err
251252
}
252253

254+
id := enc.Encode(rp.Cid())
255+
pins = append(pins, id)
253256
if err := api.Pin().Rm(req.Context, rp, options.Pin.RmRecursive(recursive)); err != nil {
254-
if err := res.Emit(&PinOutput{
255-
Pins: []string{rp.Cid().String()},
256-
Error: err.Error(),
257-
}); err != nil {
258-
return err
259-
}
260-
continue
261-
}
262-
263-
if err := res.Emit(&PinOutput{
264-
Pins: []string{rp.Cid().String()},
265-
}); err != nil {
266-
return err
257+
errs[id] = err.Error()
267258
}
268259
}
269260

270-
return nil
261+
return cmds.EmitOnce(res, &PinOutput{pins, errs})
271262
},
272-
PostRun: cmds.PostRunMap{
273-
cmds.CLI: func(res cmds.Response, re cmds.ResponseEmitter) error {
274-
failed := false
275-
for {
276-
out, err := res.Next()
277-
if err == io.EOF {
278-
break
279-
} else if err != nil {
280-
return err
281-
}
282-
r := out.(*PinOutput)
283-
if r.Pins == nil && r.Error != "" {
284-
return fmt.Errorf("aborted: %s", r.Error)
285-
} else if r.Error != "" {
286-
failed = true
287-
fmt.Fprintf(os.Stderr, "cannot unpin %s: %s\n", r.Pins[0], r.Error)
263+
Encoders: cmds.EncoderMap{
264+
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *PinOutput) error {
265+
for _, k := range out.Pins {
266+
if err, ok := out.Error[k]; !ok {
267+
fmt.Fprintf(w, "unpinned %s\n", k)
288268
} else {
289-
fmt.Fprintf(os.Stdout, "unpinned %s\n", r.Pins[0])
269+
fmt.Fprintf(os.Stderr, "cannot unpin %s: %s\n", k, err)
290270
}
291271
}
292272

293-
if failed {
294-
return fmt.Errorf("some hash not unpinned")
295-
}
296273
return nil
297-
},
274+
}),
298275
},
299276
}
300277

core/coreapi/interface/options/pin.go

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ type PinLsSettings struct {
1111
// PinRmSettings represents the settings of pin rm command
1212
type PinRmSettings struct {
1313
Recursive bool
14-
Force bool
1514
}
1615

1716
type PinUpdateSettings struct {

core/coreapi/pin.go

-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ import (
66

77
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
88
caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
9-
corerepo "github.com/ipfs/go-ipfs/core/corerepo"
109
merkledag "gx/ipfs/QmTQdH4848iTVCJmKXYyRiK72HufWTLYQQ8iN3JaQ8K1Hq/go-merkledag"
1110
bserv "gx/ipfs/QmYPZzd9VqmJDwxUnThfeSbV1Y5o53aVPDijTB7j7rS9Ep/go-blockservice"
1211

1312
cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
1413
offline "gx/ipfs/QmYZwey1thDTynSrvd6qQkX24UpTka6TFhQ2v569UpoqxD/go-ipfs-exchange-offline"
15-
merkledag "gx/ipfs/QmdV35UHnL1FM52baPkeUo6u7Fxm2CRUkPTLRPxeF8a4Ap/go-merkledag"
1614
)
1715

1816
type PinAPI CoreAPI

core/corerepo/pinning.go

Whitespace-only changes.

test/sharness/t0080-repo.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ test_expect_success "pinning directly should fail now" '
9393
'
9494

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

0 commit comments

Comments
 (0)