@@ -10,9 +10,9 @@ import (
10
10
core "github.com/ipfs/go-ipfs/core"
11
11
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
12
12
e "github.com/ipfs/go-ipfs/core/commands/e"
13
+ coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
13
14
iface "github.com/ipfs/go-ipfs/core/coreapi/interface"
14
15
options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
15
- corerepo "github.com/ipfs/go-ipfs/core/corerepo"
16
16
pin "github.com/ipfs/go-ipfs/pin"
17
17
18
18
cmds "gx/ipfs/QmPdvMtgpnMuU68mWhGtzCxnddXJoV96tT9aPcNbQsqPaM/go-ipfs-cmds"
@@ -39,7 +39,14 @@ var PinCmd = &cmds.Command{
39
39
}
40
40
41
41
type PinOutput struct {
42
- Pins []string
42
+ Hash string
43
+ Error string
44
+ }
45
+
46
+ // PinUpdateOutput represents the pin update output
47
+ type PinUpdateOutput struct {
48
+ From string
49
+ To string
43
50
}
44
51
45
52
type AddPinOutput struct {
@@ -88,24 +95,25 @@ var addPinCmd = &cmds.Command{
88
95
}
89
96
90
97
if ! showProgress {
91
- added , err := corerepo . Pin ( n , api , req .Context , req .Arguments , recursive )
98
+ added , err := pinAddMany ( req .Context , api , req .Arguments , recursive )
92
99
if err != nil {
93
100
return err
94
101
}
95
- return cmds .EmitOnce (res , & AddPinOutput {Pins : cidsToStrings (added )})
102
+
103
+ return cmds .EmitOnce (res , & AddPinOutput {Pins : added })
96
104
}
97
105
98
106
v := new (dag.ProgressTracker )
99
107
ctx := v .DeriveContext (req .Context )
100
108
101
109
type pinResult struct {
102
- pins []cid. Cid
110
+ pins []string
103
111
err error
104
112
}
105
113
106
114
ch := make (chan pinResult , 1 )
107
115
go func () {
108
- added , err := corerepo . Pin ( n , api , ctx , req .Arguments , recursive )
116
+ added , err := pinAddMany ( req . Context , api , req .Arguments , recursive )
109
117
ch <- pinResult {pins : added , err : err }
110
118
}()
111
119
@@ -124,7 +132,7 @@ var addPinCmd = &cmds.Command{
124
132
return err
125
133
}
126
134
}
127
- return res .Emit (& AddPinOutput {Pins : cidsToStrings ( val .pins ) })
135
+ return res .Emit (& AddPinOutput {Pins : val .pins })
128
136
case <- ticker .C :
129
137
if err := res .Emit (& AddPinOutput {Progress : v .Value ()}); err != nil {
130
138
return err
@@ -181,6 +189,28 @@ var addPinCmd = &cmds.Command{
181
189
},
182
190
}
183
191
192
+ func pinAddMany (ctx context.Context , api coreiface.CoreAPI , paths []string , recursive bool ) ([]string , error ) {
193
+ added := make ([]string , len (paths ))
194
+ for i , b := range paths {
195
+ p , err := coreiface .ParsePath (b )
196
+ if err != nil {
197
+ return nil , err
198
+ }
199
+
200
+ rp , err := api .ResolvePath (ctx , p )
201
+ if err != nil {
202
+ return nil , err
203
+ }
204
+
205
+ if err := api .Pin ().Add (ctx , p , options .Pin .Recursive (recursive )); err != nil {
206
+ return nil , err
207
+ }
208
+ added [i ] = rp .Cid ().String ()
209
+ }
210
+
211
+ return added , nil
212
+ }
213
+
184
214
var rmPinCmd = & cmds.Command {
185
215
Helptext : cmdkit.HelpText {
186
216
Tagline : "Remove pinned objects from local storage." ,
@@ -198,11 +228,6 @@ collected if needed. (By default, recursively. Use -r=false for direct pins.)
198
228
},
199
229
Type : PinOutput {},
200
230
Run : func (req * cmds.Request , res cmds.ResponseEmitter , env cmds.Environment ) error {
201
- n , err := cmdenv .GetNode (env )
202
- if err != nil {
203
- return err
204
- }
205
-
206
231
api , err := cmdenv .GetApi (env )
207
232
if err != nil {
208
233
return err
@@ -215,20 +240,62 @@ collected if needed. (By default, recursively. Use -r=false for direct pins.)
215
240
return err
216
241
}
217
242
218
- removed , err := corerepo .Unpin (n , api , req .Context , req .Arguments , recursive )
219
- if err != nil {
220
- return err
243
+ for _ , b := range req .Arguments {
244
+ p , err := coreiface .ParsePath (b )
245
+ if err != nil {
246
+ return err
247
+ }
248
+
249
+ rp , err := api .ResolvePath (req .Context , p )
250
+ if err != nil {
251
+ return err
252
+ }
253
+
254
+ if err := api .Pin ().Rm (req .Context , rp , options .Pin .RmRecursive (recursive )); err != nil {
255
+ if err := res .Emit (& PinOutput {
256
+ Hash : rp .Cid ().String (),
257
+ Error : err .Error (),
258
+ }); err != nil {
259
+ return err
260
+ }
261
+ continue
262
+ }
263
+
264
+ if err := res .Emit (& PinOutput {
265
+ Hash : rp .Cid ().String (),
266
+ }); err != nil {
267
+ return err
268
+ }
221
269
}
222
270
223
- return cmds . EmitOnce ( res , & PinOutput { cidsToStrings ( removed )})
271
+ return nil
224
272
},
225
- Encoders : cmds.EncoderMap {
226
- cmds .Text : cmds .MakeTypedEncoder (func (req * cmds.Request , w io.Writer , out * PinOutput ) error {
227
- for _ , k := range out .Pins {
228
- fmt .Fprintf (w , "unpinned %s\n " , k )
273
+ PostRun : cmds.PostRunMap {
274
+ cmds .CLI : func (res cmds.Response , re cmds.ResponseEmitter ) error {
275
+ failed := false
276
+ for {
277
+ out , err := res .Next ()
278
+ if err == io .EOF {
279
+ break
280
+ } else if err != nil {
281
+ return err
282
+ }
283
+ r := out .(* PinOutput )
284
+ if r .Hash == "" && r .Error != "" {
285
+ return fmt .Errorf ("aborted: %s" , r .Error )
286
+ } else if r .Error != "" {
287
+ failed = true
288
+ fmt .Fprintf (os .Stderr , "cannot unpin %s: %s\n " , r .Hash , r .Error )
289
+ } else {
290
+ fmt .Fprintf (os .Stdout , "unpinned %s\n " , r .Hash )
291
+ }
292
+ }
293
+
294
+ if failed {
295
+ return fmt .Errorf ("some hash not unpinned" )
229
296
}
230
297
return nil
231
- }) ,
298
+ },
232
299
},
233
300
}
234
301
@@ -364,7 +431,7 @@ new pin and removing the old one.
364
431
Options : []cmdkit.Option {
365
432
cmdkit .BoolOption (pinUnpinOptionName , "Remove the old pin." ).WithDefault (true ),
366
433
},
367
- Type : PinOutput {},
434
+ Type : PinUpdateOutput {},
368
435
Run : func (req * cmds.Request , res cmds.ResponseEmitter , env cmds.Environment ) error {
369
436
api , err := cmdenv .GetApi (env )
370
437
if err != nil {
@@ -388,11 +455,11 @@ new pin and removing the old one.
388
455
return err
389
456
}
390
457
391
- return cmds .EmitOnce (res , & PinOutput { Pins : [] string { from .String (), to .String ()} })
458
+ return cmds .EmitOnce (res , & PinUpdateOutput { From : from .String (), To : to .String ()})
392
459
},
393
460
Encoders : cmds.EncoderMap {
394
- cmds .Text : cmds .MakeTypedEncoder (func (req * cmds.Request , w io.Writer , out * PinOutput ) error {
395
- fmt .Fprintf (w , "updated %s to %s\n " , out .Pins [ 0 ] , out .Pins [ 1 ] )
461
+ cmds .Text : cmds .MakeTypedEncoder (func (req * cmds.Request , w io.Writer , out * PinUpdateOutput ) error {
462
+ fmt .Fprintf (w , "updated %s to %s\n " , out .From , out .To )
396
463
return nil
397
464
}),
398
465
},
@@ -628,11 +695,3 @@ func (r PinVerifyRes) Format(out io.Writer) {
628
695
}
629
696
}
630
697
}
631
-
632
- func cidsToStrings (cs []cid.Cid ) []string {
633
- out := make ([]string , 0 , len (cs ))
634
- for _ , c := range cs {
635
- out = append (out , c .String ())
636
- }
637
- return out
638
- }
0 commit comments