cmds/pin: modify test
License: MIT Signed-off-by: Overbool <overbool.xu@gmail.com>
Overbool committed
Dec 15, 2018 at 11:14 UTC
7a08cf9cf3947df9089de8725b30e951287db086
5 files changed
+31
-63
core/commands/pin.go
+30
-59
@@ -7,22 +7,22 @@ import (
7
"os"
8
"time"
9
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"
14
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"
17
18
- cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
18
+ "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
19
bserv "gx/ipfs/QmVKQHuzni68SWByzJgBUCwHvvr4TWiXfutNWWwpZpp4rE/go-blockservice"
20
- cmds "gx/ipfs/QmWGm4AbZEbnmdgVTza52MSNpEmBdFVqzmAysRbjrRyGbH/go-ipfs-cmds"
20
+ "gx/ipfs/QmWGm4AbZEbnmdgVTza52MSNpEmBdFVqzmAysRbjrRyGbH/go-ipfs-cmds"
21
"gx/ipfs/QmYMQuypUbgsdNHmuCBSUJV6wdQVsBHRivNAp3efHJwZJD/go-verifcid"
22
- offline "gx/ipfs/QmYZwey1thDTynSrvd6qQkX24UpTka6TFhQ2v569UpoqxD/go-ipfs-exchange-offline"
22
+ "gx/ipfs/QmYZwey1thDTynSrvd6qQkX24UpTka6TFhQ2v569UpoqxD/go-ipfs-exchange-offline"
23
dag "gx/ipfs/Qmb2UEG2TAeVrEJSjqsZF7Y2he7wRDkrdt6c3bECxwZf4k/go-merkledag"
24
- cidenc "gx/ipfs/QmdPQx9fvN5ExVwMhRmh7YpCQJzJrFhd1AjVBwJmRMFJeX/go-cidutil/cidenc"
25
- cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
24
+ "gx/ipfs/QmdPQx9fvN5ExVwMhRmh7YpCQJzJrFhd1AjVBwJmRMFJeX/go-cidutil/cidenc"
25
+ "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
26
)
27
28
var PinCmd = &cmds.Command{
@@ -68,18 +68,11 @@ var addPinCmd = &cmds.Command{
68
},
69
Type: AddPinOutput{},
70
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
-
71
api, err := cmdenv.GetApi(env, req)
72
if err != nil {
73
return err
74
}
75
81
- defer n.Blockstore.PinLock().Unlock()
82
-
76
// set recursive flag
77
recursive, _ := req.Options[pinRecursiveOptionName].(bool)
78
showProgress, _ := req.Options[pinProgressOptionName].(bool)
@@ -88,8 +81,13 @@ var addPinCmd = &cmds.Command{
81
return err
82
}
83
84
+ enc, err := cmdenv.GetCidEncoder(req)
85
+ if err != nil {
86
+ return err
87
+ }
88
+
89
if !showProgress {
92
- added, err := pinAddMany(req.Context, api, req.Arguments, recursive)
90
+ added, err := pinAddMany(req.Context, api, enc, req.Arguments, recursive)
91
if err != nil {
92
return err
93
}
@@ -107,7 +105,7 @@ var addPinCmd = &cmds.Command{
105
106
ch := make(chan pinResult, 1)
107
go func() {
110
- added, err := pinAddMany(req.Context, api, req.Arguments, recursive)
108
+ added, err := pinAddMany(ctx, api, enc, req.Arguments, recursive)
109
ch <- pinResult{pins: added, err: err}
110
}()
111
@@ -183,7 +181,7 @@ var addPinCmd = &cmds.Command{
181
},
182
}
183
186
-func pinAddMany(ctx context.Context, api coreiface.CoreAPI, paths []string, recursive bool) ([]string, error) {
184
+func pinAddMany(ctx context.Context, api coreiface.CoreAPI, enc cidenc.Encoder, paths []string, recursive bool) ([]string, error) {
185
added := make([]string, len(paths))
186
for i, b := range paths {
187
p, err := coreiface.ParsePath(b)
@@ -196,10 +194,10 @@ func pinAddMany(ctx context.Context, api coreiface.CoreAPI, paths []string, recu
194
return nil, err
195
}
196
199
- if err := api.Pin().Add(ctx, p, options.Pin.Recursive(recursive)); err != nil {
197
+ if err := api.Pin().Add(ctx, rp, options.Pin.Recursive(recursive)); err != nil {
198
return nil, err
199
}
202
- added[i] = rp.Cid().String()
200
+ added[i] = enc.Encode(rp.Cid())
201
}
202
203
return added, nil
@@ -239,6 +237,7 @@ collected if needed. (By default, recursively. Use -r=false for direct pins.)
237
return err
238
}
239
240
+ pins := make([]string, 0, len(req.Arguments))
241
for _, b := range req.Arguments {
242
p, err := coreiface.ParsePath(b)
243
if err != nil {
@@ -250,51 +249,23 @@ collected if needed. (By default, recursively. Use -r=false for direct pins.)
249
return err
250
}
251
252
+ id := enc.Encode(rp.Cid())
253
+ pins = append(pins, id)
254
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 {
255
return err
256
}
257
}
258
270
- return nil
259
+ return cmds.EmitOnce(res, &PinOutput{pins})
260
},
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)
288
- } else {
289
- fmt.Fprintf(os.Stdout, "unpinned %s\n", r.Pins[0])
290
- }
261
+ Encoders: cmds.EncoderMap{
262
+ cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *PinOutput) error {
263
+ for _, k := range out.Pins {
264
+ fmt.Fprintf(w, "unpinned %s\n", k)
265
}
266
293
- if failed {
294
- return fmt.Errorf("some hash not unpinned")
295
- }
267
return nil
297
- },
268
+ }),
269
},
270
}
271
core/coreapi/interface/options/pin.go
-1
@@ -11,7 +11,6 @@ type PinLsSettings struct {
11
// PinRmSettings represents the settings of pin rm command
12
type PinRmSettings struct {
13
Recursive bool
14
- Force bool
14
}
15
16
type PinUpdateSettings struct {
core/coreapi/pin.go
-2
@@ -6,13 +6,11 @@ import (
6
7
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
8
caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
9
- corerepo "github.com/ipfs/go-ipfs/core/corerepo"
9
bserv "gx/ipfs/QmVKQHuzni68SWByzJgBUCwHvvr4TWiXfutNWWwpZpp4rE/go-blockservice"
10
merkledag "gx/ipfs/Qmb2UEG2TAeVrEJSjqsZF7Y2he7wRDkrdt6c3bECxwZf4k/go-merkledag"
11
12
cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
13
offline "gx/ipfs/QmYZwey1thDTynSrvd6qQkX24UpTka6TFhQ2v569UpoqxD/go-ipfs-exchange-offline"
15
- merkledag "gx/ipfs/QmdV35UHnL1FM52baPkeUo6u7Fxm2CRUkPTLRPxeF8a4Ap/go-merkledag"
14
)
15
16
type PinAPI CoreAPI
core/corerepo/pinning.go
test/sharness/t0080-repo.sh
+1
-1
@@ -93,7 +93,7 @@ test_expect_success "pinning directly should fail now" '
93
'
94
95
test_expect_success "'ipfs pin rm -r=false <hash>' should fail" '
96
- echo "Error: $HASH is pinned recursively" >expected4 &&
96
+ echo "Error: $HASH is pinned recursively" >expected4
97
test_must_fail ipfs pin rm -r=false "$HASH" 2>actual4 &&
98
test_cmp expected4 actual4
99
'