Skip to content

Commit f835127

Browse files
committed
feat: support for optional pin names
1 parent 765cffe commit f835127

File tree

22 files changed

+147
-63
lines changed

22 files changed

+147
-63
lines changed

client/rpc/pin.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type pinRefKeyList struct {
2626
type pin struct {
2727
path path.ImmutablePath
2828
typ string
29+
name string
2930
err error
3031
}
3132

@@ -37,6 +38,10 @@ func (p pin) Path() path.ImmutablePath {
3738
return p.path
3839
}
3940

41+
func (p pin) Name() string {
42+
return p.name
43+
}
44+
4045
func (p pin) Type() string {
4146
return p.typ
4247
}
@@ -53,6 +58,7 @@ func (api *PinAPI) Add(ctx context.Context, p path.Path, opts ...caopts.PinAddOp
5358

5459
type pinLsObject struct {
5560
Cid string
61+
Name string
5662
Type string
5763
}
5864

@@ -102,7 +108,7 @@ func (api *PinAPI) Ls(ctx context.Context, opts ...caopts.PinLsOption) (<-chan i
102108
}
103109

104110
select {
105-
case ch <- pin{typ: out.Type, path: path.FromCid(c)}:
111+
case ch <- pin{typ: out.Type, name: out.Name, path: path.FromCid(c)}:
106112
case <-ctx.Done():
107113
return
108114
}

cmd/ipfs/kubo/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ func initializeIpnsKeyspace(repoRoot string) error {
252252

253253
// pin recursively because this might already be pinned
254254
// and doing a direct pin would throw an error in that case
255-
err = nd.Pinning.Pin(ctx, emptyDir, true)
255+
err = nd.Pinning.Pin(ctx, emptyDir, true, "")
256256
if err != nil {
257257
return err
258258
}

core/commands/dag/import.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func dagImport(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment
152152
ret.PinErrorMsg = err.Error()
153153
} else if nd, err := blockDecoder.DecodeNode(req.Context, block); err != nil {
154154
ret.PinErrorMsg = err.Error()
155-
} else if err := node.Pinning.Pin(req.Context, nd, true); err != nil {
155+
} else if err := node.Pinning.Pin(req.Context, nd, true, ""); err != nil {
156156
ret.PinErrorMsg = err.Error()
157157
} else if err := node.Pinning.Flush(req.Context); err != nil {
158158
ret.PinErrorMsg = err.Error()

core/commands/pin/pin.go

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ var addPinCmd = &cmds.Command{
6565
Options: []cmds.Option{
6666
cmds.BoolOption(pinRecursiveOptionName, "r", "Recursively pin the object linked to by the specified object(s).").WithDefault(true),
6767
cmds.BoolOption(pinProgressOptionName, "Show progress"),
68+
cmds.StringOption(pinNameOptionName, "n", "An optional name for the pin(s)."),
6869
},
6970
Type: AddPinOutput{},
7071
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
@@ -75,6 +76,7 @@ var addPinCmd = &cmds.Command{
7576

7677
// set recursive flag
7778
recursive, _ := req.Options[pinRecursiveOptionName].(bool)
79+
name, _ := req.Options[pinNameOptionName].(string)
7880
showProgress, _ := req.Options[pinProgressOptionName].(bool)
7981

8082
if err := req.ParseBodyArgs(); err != nil {
@@ -87,7 +89,7 @@ var addPinCmd = &cmds.Command{
8789
}
8890

8991
if !showProgress {
90-
added, err := pinAddMany(req.Context, api, enc, req.Arguments, recursive)
92+
added, err := pinAddMany(req.Context, api, enc, req.Arguments, recursive, name)
9193
if err != nil {
9294
return err
9395
}
@@ -105,7 +107,7 @@ var addPinCmd = &cmds.Command{
105107

106108
ch := make(chan pinResult, 1)
107109
go func() {
108-
added, err := pinAddMany(ctx, api, enc, req.Arguments, recursive)
110+
added, err := pinAddMany(ctx, api, enc, req.Arguments, recursive, name)
109111
ch <- pinResult{pins: added, err: err}
110112
}()
111113

@@ -181,7 +183,7 @@ var addPinCmd = &cmds.Command{
181183
},
182184
}
183185

184-
func pinAddMany(ctx context.Context, api coreiface.CoreAPI, enc cidenc.Encoder, paths []string, recursive bool) ([]string, error) {
186+
func pinAddMany(ctx context.Context, api coreiface.CoreAPI, enc cidenc.Encoder, paths []string, recursive bool, name string) ([]string, error) {
185187
added := make([]string, len(paths))
186188
for i, b := range paths {
187189
p, err := cmdutils.PathOrCidPath(b)
@@ -194,7 +196,7 @@ func pinAddMany(ctx context.Context, api coreiface.CoreAPI, enc cidenc.Encoder,
194196
return nil, err
195197
}
196198

197-
if err := api.Pin().Add(ctx, rp, options.Pin.Recursive(recursive)); err != nil {
199+
if err := api.Pin().Add(ctx, rp, options.Pin.Recursive(recursive), options.Pin.Name(name)); err != nil {
198200
return nil, err
199201
}
200202
added[i] = enc.Encode(rp.RootCid())
@@ -278,9 +280,10 @@ ipfs pin ls -t indirect <cid>
278280
}
279281

280282
const (
281-
pinTypeOptionName = "type"
282-
pinQuietOptionName = "quiet"
283-
pinStreamOptionName = "stream"
283+
pinTypeOptionName = "type"
284+
pinQuietOptionName = "quiet"
285+
pinStreamOptionName = "stream"
286+
pinDetailedOptionName = "detailed"
284287
)
285288

286289
var listPinCmd = &cmds.Command{
@@ -334,6 +337,7 @@ Example:
334337
cmds.StringOption(pinTypeOptionName, "t", "The type of pinned keys to list. Can be \"direct\", \"indirect\", \"recursive\", or \"all\".").WithDefault("all"),
335338
cmds.BoolOption(pinQuietOptionName, "q", "Write just hashes of objects."),
336339
cmds.BoolOption(pinStreamOptionName, "s", "Enable streaming of pins as they are discovered."),
340+
cmds.BoolOption(pinDetailedOptionName, "d", "Enable displaying additional information, such as pin names (slower)."),
337341
},
338342
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
339343
api, err := cmdenv.GetApi(env, req)
@@ -343,6 +347,7 @@ Example:
343347

344348
typeStr, _ := req.Options[pinTypeOptionName].(string)
345349
stream, _ := req.Options[pinStreamOptionName].(bool)
350+
detailed, _ := req.Options[pinDetailedOptionName].(bool)
346351

347352
switch typeStr {
348353
case "all", "direct", "indirect", "recursive":
@@ -356,7 +361,7 @@ Example:
356361
lgcList := map[string]PinLsType{}
357362
if !stream {
358363
emit = func(v PinLsOutputWrapper) error {
359-
lgcList[v.PinLsObject.Cid] = PinLsType{Type: v.PinLsObject.Type}
364+
lgcList[v.PinLsObject.Cid] = PinLsType{Type: v.PinLsObject.Type, Label: v.PinLsObject.Name}
360365
return nil
361366
}
362367
} else {
@@ -368,7 +373,7 @@ Example:
368373
if len(req.Arguments) > 0 {
369374
err = pinLsKeys(req, typeStr, api, emit)
370375
} else {
371-
err = pinLsAll(req, typeStr, api, emit)
376+
err = pinLsAll(req, typeStr, detailed, api, emit)
372377
}
373378
if err != nil {
374379
return err
@@ -402,17 +407,21 @@ Example:
402407
if stream {
403408
if quiet {
404409
fmt.Fprintf(w, "%s\n", out.PinLsObject.Cid)
405-
} else {
410+
} else if out.PinLsObject.Name == "" {
406411
fmt.Fprintf(w, "%s %s\n", out.PinLsObject.Cid, out.PinLsObject.Type)
412+
} else {
413+
fmt.Fprintf(w, "%s %s %s\n", out.PinLsObject.Cid, out.PinLsObject.Type, out.PinLsObject.Name)
407414
}
408415
return nil
409416
}
410417

411418
for k, v := range out.PinLsList.Keys {
412419
if quiet {
413420
fmt.Fprintf(w, "%s\n", k)
414-
} else {
421+
} else if v.Label == "" {
415422
fmt.Fprintf(w, "%s %s\n", k, v.Type)
423+
} else {
424+
fmt.Fprintf(w, "%s %s %s\n", k, v.Type, v.Label)
416425
}
417426
}
418427

@@ -436,12 +445,14 @@ type PinLsList struct {
436445

437446
// PinLsType contains the type of a pin
438447
type PinLsType struct {
439-
Type string
448+
Type string
449+
Label string
440450
}
441451

442452
// PinLsObject contains the description of a pin
443453
type PinLsObject struct {
444454
Cid string `json:",omitempty"`
455+
Name string `json:",omitempty"`
445456
Type string `json:",omitempty"`
446457
}
447458

@@ -502,7 +513,7 @@ func pinLsKeys(req *cmds.Request, typeStr string, api coreiface.CoreAPI, emit fu
502513
return nil
503514
}
504515

505-
func pinLsAll(req *cmds.Request, typeStr string, api coreiface.CoreAPI, emit func(value PinLsOutputWrapper) error) error {
516+
func pinLsAll(req *cmds.Request, typeStr string, detailed bool, api coreiface.CoreAPI, emit func(value PinLsOutputWrapper) error) error {
506517
enc, err := cmdenv.GetCidEncoder(req)
507518
if err != nil {
508519
return err
@@ -520,7 +531,7 @@ func pinLsAll(req *cmds.Request, typeStr string, api coreiface.CoreAPI, emit fun
520531
panic("unhandled pin type")
521532
}
522533

523-
pins, err := api.Pin().Ls(req.Context, opt)
534+
pins, err := api.Pin().Ls(req.Context, opt, options.Pin.Ls.Detailed(detailed))
524535
if err != nil {
525536
return err
526537
}
@@ -532,6 +543,7 @@ func pinLsAll(req *cmds.Request, typeStr string, api coreiface.CoreAPI, emit fun
532543
err = emit(PinLsOutputWrapper{
533544
PinLsObject: PinLsObject{
534545
Type: p.Type(),
546+
Name: p.Name(),
535547
Cid: enc.Encode(p.Path().RootCid()),
536548
},
537549
})
@@ -748,15 +760,15 @@ func pinVerify(ctx context.Context, n *core.IpfsNode, opts pinVerifyOpts, enc ci
748760
out := make(chan any)
749761
go func() {
750762
defer close(out)
751-
for p := range n.Pinning.RecursiveKeys(ctx) {
763+
for p := range n.Pinning.RecursiveKeys(ctx, false) {
752764
if p.Err != nil {
753765
out <- PinVerifyRes{Err: p.Err.Error()}
754766
return
755767
}
756-
pinStatus := checkPin(p.C)
768+
pinStatus := checkPin(p.Pin.Key)
757769
if !pinStatus.Ok || opts.includeOk {
758770
select {
759-
case out <- PinVerifyRes{Cid: enc.Encode(p.C), PinStatus: pinStatus}:
771+
case out <- PinVerifyRes{Cid: enc.Encode(p.Pin.Key), PinStatus: pinStatus}:
760772
case <-ctx.Done():
761773
return
762774
}

core/coreapi/block.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Bloc
6060
}
6161

6262
if settings.Pin {
63-
if err = api.pinning.PinWithMode(ctx, b.Cid(), pin.Recursive); err != nil {
63+
if err = api.pinning.PinWithMode(ctx, b.Cid(), pin.Recursive, ""); err != nil {
6464
return nil, err
6565
}
6666
if err := api.pinning.Flush(ctx); err != nil {

core/coreapi/dag.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (adder *pinningAdder) Add(ctx context.Context, nd ipld.Node) error {
3030
return err
3131
}
3232

33-
if err := adder.pinning.PinWithMode(ctx, nd.Cid(), pin.Recursive); err != nil {
33+
if err := adder.pinning.PinWithMode(ctx, nd.Cid(), pin.Recursive, ""); err != nil {
3434
return err
3535
}
3636

@@ -51,7 +51,7 @@ func (adder *pinningAdder) AddMany(ctx context.Context, nds []ipld.Node) error {
5151
for _, nd := range nds {
5252
c := nd.Cid()
5353
if cids.Visit(c) {
54-
if err := adder.pinning.PinWithMode(ctx, c, pin.Recursive); err != nil {
54+
if err := adder.pinning.PinWithMode(ctx, c, pin.Recursive, ""); err != nil {
5555
return err
5656
}
5757
}

core/coreapi/object.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func (api *ObjectAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Obj
133133
}
134134

135135
if options.Pin {
136-
if err := api.pinning.PinWithMode(ctx, dagnode.Cid(), pin.Recursive); err != nil {
136+
if err := api.pinning.PinWithMode(ctx, dagnode.Cid(), pin.Recursive, ""); err != nil {
137137
return path.ImmutablePath{}, err
138138
}
139139

0 commit comments

Comments
 (0)