@@ -7,22 +7,22 @@ import (
7
7
"os"
8
8
"time"
9
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"
13
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
17
18
- cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
18
+ "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
19
19
dag "gx/ipfs/QmTQdH4848iTVCJmKXYyRiK72HufWTLYQQ8iN3JaQ8K1Hq/go-merkledag"
20
- cmds "gx/ipfs/QmWGm4AbZEbnmdgVTza52MSNpEmBdFVqzmAysRbjrRyGbH/go-ipfs-cmds"
20
+ "gx/ipfs/QmWGm4AbZEbnmdgVTza52MSNpEmBdFVqzmAysRbjrRyGbH/go-ipfs-cmds"
21
21
"gx/ipfs/QmYMQuypUbgsdNHmuCBSUJV6wdQVsBHRivNAp3efHJwZJD/go-verifcid"
22
22
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"
26
26
)
27
27
28
28
var PinCmd = & cmds.Command {
@@ -40,7 +40,8 @@ var PinCmd = &cmds.Command{
40
40
}
41
41
42
42
type PinOutput struct {
43
- Pins []string
43
+ Pins []string
44
+ Error map [string ]string `json:",omitempty"`
44
45
}
45
46
46
47
type AddPinOutput struct {
@@ -68,18 +69,11 @@ var addPinCmd = &cmds.Command{
68
69
},
69
70
Type : AddPinOutput {},
70
71
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
-
76
72
api , err := cmdenv .GetApi (env , req )
77
73
if err != nil {
78
74
return err
79
75
}
80
76
81
- defer n .Blockstore .PinLock ().Unlock ()
82
-
83
77
// set recursive flag
84
78
recursive , _ := req .Options [pinRecursiveOptionName ].(bool )
85
79
showProgress , _ := req .Options [pinProgressOptionName ].(bool )
@@ -88,8 +82,13 @@ var addPinCmd = &cmds.Command{
88
82
return err
89
83
}
90
84
85
+ enc , err := cmdenv .GetCidEncoder (req )
86
+ if err != nil {
87
+ return err
88
+ }
89
+
91
90
if ! showProgress {
92
- added , err := pinAddMany (req .Context , api , req .Arguments , recursive )
91
+ added , err := pinAddMany (req .Context , api , enc , req .Arguments , recursive )
93
92
if err != nil {
94
93
return err
95
94
}
@@ -107,7 +106,7 @@ var addPinCmd = &cmds.Command{
107
106
108
107
ch := make (chan pinResult , 1 )
109
108
go func () {
110
- added , err := pinAddMany (req . Context , api , req .Arguments , recursive )
109
+ added , err := pinAddMany (ctx , api , enc , req .Arguments , recursive )
111
110
ch <- pinResult {pins : added , err : err }
112
111
}()
113
112
@@ -183,7 +182,7 @@ var addPinCmd = &cmds.Command{
183
182
},
184
183
}
185
184
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 ) {
187
186
added := make ([]string , len (paths ))
188
187
for i , b := range paths {
189
188
p , err := coreiface .ParsePath (b )
@@ -196,10 +195,10 @@ func pinAddMany(ctx context.Context, api coreiface.CoreAPI, paths []string, recu
196
195
return nil , err
197
196
}
198
197
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 {
200
199
return nil , err
201
200
}
202
- added [i ] = rp .Cid (). String ( )
201
+ added [i ] = enc . Encode ( rp .Cid ())
203
202
}
204
203
205
204
return added , nil
@@ -239,6 +238,8 @@ collected if needed. (By default, recursively. Use -r=false for direct pins.)
239
238
return err
240
239
}
241
240
241
+ pins := make ([]string , 0 , len (req .Arguments ))
242
+ errs := make (map [string ]string )
242
243
for _ , b := range req .Arguments {
243
244
p , err := coreiface .ParsePath (b )
244
245
if err != nil {
@@ -250,51 +251,27 @@ collected if needed. (By default, recursively. Use -r=false for direct pins.)
250
251
return err
251
252
}
252
253
254
+ id := enc .Encode (rp .Cid ())
255
+ pins = append (pins , id )
253
256
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 ()
267
258
}
268
259
}
269
260
270
- return nil
261
+ return cmds . EmitOnce ( res , & PinOutput { pins , errs })
271
262
},
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 )
288
268
} else {
289
- fmt .Fprintf (os .Stdout , "unpinned %s \n " , r . Pins [ 0 ] )
269
+ fmt .Fprintf (os .Stderr , "cannot unpin %s: %s \n " , k , err )
290
270
}
291
271
}
292
272
293
- if failed {
294
- return fmt .Errorf ("some hash not unpinned" )
295
- }
296
273
return nil
297
- },
274
+ }) ,
298
275
},
299
276
}
300
277
0 commit comments