@@ -57,13 +57,36 @@ var addPinCmd = &cmds.Command{
57
57
Helptext : cmds.HelpText {
58
58
Tagline : "Pin objects to local storage." ,
59
59
ShortDescription : "Stores an IPFS object(s) from a given path locally to disk." ,
60
+ LongDescription : `
61
+ Create a pin for the given object, protecting resolved CID from being garbage
62
+ collected.
63
+
64
+ An optional name can be provided, and read back via 'ipfs pin ls --names'.
65
+
66
+ Be mindful of defaults:
67
+
68
+ Default pin type is 'recursive' (entire DAG).
69
+ Pass -r=false to create a direct pin for a single block.
70
+ Use 'pin ls -t recursive' to only list roots of recursively pinned DAGs
71
+ (significantly faster when many big DAGs are pinned recursively)
72
+
73
+ Default pin name is empty. Pass '--name' to 'pin add' to set one
74
+ and use 'pin ls --names' to see it.
75
+ Pin add is idempotent: pinning CID which is already pinned won't change
76
+ the name, value passed with '--name' with the original pin is preserved.
77
+ To rename pin, use 'pin rm' and 'pin add --name'.
78
+
79
+ If daemon is running, any missing blocks will be retrieved from the network.
80
+ It may take some time. Pass '--progress' to track the progress.
81
+ ` ,
60
82
},
61
83
62
84
Arguments : []cmds.Argument {
63
85
cmds .StringArg ("ipfs-path" , true , true , "Path to object(s) to be pinned." ).EnableStdin (),
64
86
},
65
87
Options : []cmds.Option {
66
88
cmds .BoolOption (pinRecursiveOptionName , "r" , "Recursively pin the object linked to by the specified object(s)." ).WithDefault (true ),
89
+ cmds .StringOption (pinNameOptionName , "n" , "An optional name for created pin(s)." ),
67
90
cmds .BoolOption (pinProgressOptionName , "Show progress" ),
68
91
},
69
92
Type : AddPinOutput {},
@@ -75,6 +98,7 @@ var addPinCmd = &cmds.Command{
75
98
76
99
// set recursive flag
77
100
recursive , _ := req .Options [pinRecursiveOptionName ].(bool )
101
+ name , _ := req .Options [pinNameOptionName ].(string )
78
102
showProgress , _ := req .Options [pinProgressOptionName ].(bool )
79
103
80
104
if err := req .ParseBodyArgs (); err != nil {
@@ -87,7 +111,7 @@ var addPinCmd = &cmds.Command{
87
111
}
88
112
89
113
if ! showProgress {
90
- added , err := pinAddMany (req .Context , api , enc , req .Arguments , recursive )
114
+ added , err := pinAddMany (req .Context , api , enc , req .Arguments , recursive , name )
91
115
if err != nil {
92
116
return err
93
117
}
@@ -105,7 +129,7 @@ var addPinCmd = &cmds.Command{
105
129
106
130
ch := make (chan pinResult , 1 )
107
131
go func () {
108
- added , err := pinAddMany (ctx , api , enc , req .Arguments , recursive )
132
+ added , err := pinAddMany (ctx , api , enc , req .Arguments , recursive , name )
109
133
ch <- pinResult {pins : added , err : err }
110
134
}()
111
135
@@ -181,7 +205,7 @@ var addPinCmd = &cmds.Command{
181
205
},
182
206
}
183
207
184
- func pinAddMany (ctx context.Context , api coreiface.CoreAPI , enc cidenc.Encoder , paths []string , recursive bool ) ([]string , error ) {
208
+ func pinAddMany (ctx context.Context , api coreiface.CoreAPI , enc cidenc.Encoder , paths []string , recursive bool , name string ) ([]string , error ) {
185
209
added := make ([]string , len (paths ))
186
210
for i , b := range paths {
187
211
p , err := cmdutils .PathOrCidPath (b )
@@ -194,7 +218,7 @@ func pinAddMany(ctx context.Context, api coreiface.CoreAPI, enc cidenc.Encoder,
194
218
return nil , err
195
219
}
196
220
197
- if err := api .Pin ().Add (ctx , rp , options .Pin .Recursive (recursive )); err != nil {
221
+ if err := api .Pin ().Add (ctx , rp , options .Pin .Recursive (recursive ), options . Pin . Name ( name ) ); err != nil {
198
222
return nil , err
199
223
}
200
224
added [i ] = enc .Encode (rp .RootCid ())
@@ -281,6 +305,7 @@ const (
281
305
pinTypeOptionName = "type"
282
306
pinQuietOptionName = "quiet"
283
307
pinStreamOptionName = "stream"
308
+ pinNamesOptionName = "names"
284
309
)
285
310
286
311
var listPinCmd = & cmds.Command {
@@ -294,6 +319,7 @@ respectively.
294
319
` ,
295
320
LongDescription : `
296
321
Returns a list of objects that are pinned locally.
322
+
297
323
By default, all pinned objects are returned, but the '--type' flag or
298
324
arguments can restrict that to a specific pin type or to some specific objects
299
325
respectively.
@@ -302,10 +328,13 @@ Use --type=<type> to specify the type of pinned keys to list.
302
328
Valid values are:
303
329
* "direct": pin that specific object.
304
330
* "recursive": pin that specific object, and indirectly pin all its
305
- descendants
331
+ descendants
306
332
* "indirect": pinned indirectly by an ancestor (like a refcount)
307
333
* "all"
308
334
335
+ By default, pin names are not included (returned as empty).
336
+ Pass '--names' flag to return pin names (set with '--name' from 'pin add').
337
+
309
338
With arguments, the command fails if any of the arguments is not a pinned
310
339
object. And if --type=<type> is additionally used, the command will also fail
311
340
if any of the arguments is not of the specified type.
@@ -334,6 +363,7 @@ Example:
334
363
cmds .StringOption (pinTypeOptionName , "t" , "The type of pinned keys to list. Can be \" direct\" , \" indirect\" , \" recursive\" , or \" all\" ." ).WithDefault ("all" ),
335
364
cmds .BoolOption (pinQuietOptionName , "q" , "Write just hashes of objects." ),
336
365
cmds .BoolOption (pinStreamOptionName , "s" , "Enable streaming of pins as they are discovered." ),
366
+ cmds .BoolOption (pinNamesOptionName , "n" , "Enable displaying pin names (slower)." ),
337
367
},
338
368
Run : func (req * cmds.Request , res cmds.ResponseEmitter , env cmds.Environment ) error {
339
369
api , err := cmdenv .GetApi (env , req )
@@ -343,6 +373,7 @@ Example:
343
373
344
374
typeStr , _ := req .Options [pinTypeOptionName ].(string )
345
375
stream , _ := req .Options [pinStreamOptionName ].(bool )
376
+ displayNames , _ := req .Options [pinNamesOptionName ].(bool )
346
377
347
378
switch typeStr {
348
379
case "all" , "direct" , "indirect" , "recursive" :
@@ -356,7 +387,7 @@ Example:
356
387
lgcList := map [string ]PinLsType {}
357
388
if ! stream {
358
389
emit = func (v PinLsOutputWrapper ) error {
359
- lgcList [v .PinLsObject .Cid ] = PinLsType {Type : v .PinLsObject .Type }
390
+ lgcList [v .PinLsObject .Cid ] = PinLsType {Type : v .PinLsObject .Type , Name : v . PinLsObject . Name }
360
391
return nil
361
392
}
362
393
} else {
@@ -368,7 +399,7 @@ Example:
368
399
if len (req .Arguments ) > 0 {
369
400
err = pinLsKeys (req , typeStr , api , emit )
370
401
} else {
371
- err = pinLsAll (req , typeStr , api , emit )
402
+ err = pinLsAll (req , typeStr , displayNames , api , emit )
372
403
}
373
404
if err != nil {
374
405
return err
@@ -402,17 +433,21 @@ Example:
402
433
if stream {
403
434
if quiet {
404
435
fmt .Fprintf (w , "%s\n " , out .PinLsObject .Cid )
405
- } else {
436
+ } else if out . PinLsObject . Name == "" {
406
437
fmt .Fprintf (w , "%s %s\n " , out .PinLsObject .Cid , out .PinLsObject .Type )
438
+ } else {
439
+ fmt .Fprintf (w , "%s %s %s\n " , out .PinLsObject .Cid , out .PinLsObject .Type , out .PinLsObject .Name )
407
440
}
408
441
return nil
409
442
}
410
443
411
444
for k , v := range out .PinLsList .Keys {
412
445
if quiet {
413
446
fmt .Fprintf (w , "%s\n " , k )
414
- } else {
447
+ } else if v . Name == "" {
415
448
fmt .Fprintf (w , "%s %s\n " , k , v .Type )
449
+ } else {
450
+ fmt .Fprintf (w , "%s %s %s\n " , k , v .Type , v .Name )
416
451
}
417
452
}
418
453
@@ -437,11 +472,13 @@ type PinLsList struct {
437
472
// PinLsType contains the type of a pin
438
473
type PinLsType struct {
439
474
Type string
475
+ Name string
440
476
}
441
477
442
478
// PinLsObject contains the description of a pin
443
479
type PinLsObject struct {
444
480
Cid string `json:",omitempty"`
481
+ Name string `json:",omitempty"`
445
482
Type string `json:",omitempty"`
446
483
}
447
484
@@ -502,7 +539,7 @@ func pinLsKeys(req *cmds.Request, typeStr string, api coreiface.CoreAPI, emit fu
502
539
return nil
503
540
}
504
541
505
- func pinLsAll (req * cmds.Request , typeStr string , api coreiface.CoreAPI , emit func (value PinLsOutputWrapper ) error ) error {
542
+ func pinLsAll (req * cmds.Request , typeStr string , detailed bool , api coreiface.CoreAPI , emit func (value PinLsOutputWrapper ) error ) error {
506
543
enc , err := cmdenv .GetCidEncoder (req )
507
544
if err != nil {
508
545
return err
@@ -520,7 +557,7 @@ func pinLsAll(req *cmds.Request, typeStr string, api coreiface.CoreAPI, emit fun
520
557
panic ("unhandled pin type" )
521
558
}
522
559
523
- pins , err := api .Pin ().Ls (req .Context , opt )
560
+ pins , err := api .Pin ().Ls (req .Context , opt , options . Pin . Ls . Detailed ( detailed ) )
524
561
if err != nil {
525
562
return err
526
563
}
@@ -532,6 +569,7 @@ func pinLsAll(req *cmds.Request, typeStr string, api coreiface.CoreAPI, emit fun
532
569
err = emit (PinLsOutputWrapper {
533
570
PinLsObject : PinLsObject {
534
571
Type : p .Type (),
572
+ Name : p .Name (),
535
573
Cid : enc .Encode (p .Path ().RootCid ()),
536
574
},
537
575
})
@@ -748,15 +786,15 @@ func pinVerify(ctx context.Context, n *core.IpfsNode, opts pinVerifyOpts, enc ci
748
786
out := make (chan any )
749
787
go func () {
750
788
defer close (out )
751
- for p := range n .Pinning .RecursiveKeys (ctx ) {
789
+ for p := range n .Pinning .RecursiveKeys (ctx , false ) {
752
790
if p .Err != nil {
753
791
out <- PinVerifyRes {Err : p .Err .Error ()}
754
792
return
755
793
}
756
- pinStatus := checkPin (p .C )
794
+ pinStatus := checkPin (p .Pin . Key )
757
795
if ! pinStatus .Ok || opts .includeOk {
758
796
select {
759
- case out <- PinVerifyRes {Cid : enc .Encode (p .C ), PinStatus : pinStatus }:
797
+ case out <- PinVerifyRes {Cid : enc .Encode (p .Pin . Key ), PinStatus : pinStatus }:
760
798
case <- ctx .Done ():
761
799
return
762
800
}
0 commit comments