Skip to content

Commit

Permalink
feat: support for optional pin names
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Jan 3, 2024
1 parent 765cffe commit f835127
Show file tree
Hide file tree
Showing 22 changed files with 147 additions and 63 deletions.
8 changes: 7 additions & 1 deletion client/rpc/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type pinRefKeyList struct {
type pin struct {
path path.ImmutablePath
typ string
name string
err error
}

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

func (p pin) Name() string {
return p.name

Check warning on line 42 in client/rpc/pin.go

View check run for this annotation

Codecov / codecov/patch

client/rpc/pin.go#L41-L42

Added lines #L41 - L42 were not covered by tests
}

func (p pin) Type() string {
return p.typ
}
Expand All @@ -53,6 +58,7 @@ func (api *PinAPI) Add(ctx context.Context, p path.Path, opts ...caopts.PinAddOp

type pinLsObject struct {
Cid string
Name string
Type string
}

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

select {
case ch <- pin{typ: out.Type, path: path.FromCid(c)}:
case ch <- pin{typ: out.Type, name: out.Name, path: path.FromCid(c)}:
case <-ctx.Done():
return
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/ipfs/kubo/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func initializeIpnsKeyspace(repoRoot string) error {

// pin recursively because this might already be pinned
// and doing a direct pin would throw an error in that case
err = nd.Pinning.Pin(ctx, emptyDir, true)
err = nd.Pinning.Pin(ctx, emptyDir, true, "")

Check warning on line 255 in cmd/ipfs/kubo/init.go

View check run for this annotation

Codecov / codecov/patch

cmd/ipfs/kubo/init.go#L255

Added line #L255 was not covered by tests
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion core/commands/dag/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func dagImport(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment
ret.PinErrorMsg = err.Error()
} else if nd, err := blockDecoder.DecodeNode(req.Context, block); err != nil {
ret.PinErrorMsg = err.Error()
} else if err := node.Pinning.Pin(req.Context, nd, true); err != nil {
} else if err := node.Pinning.Pin(req.Context, nd, true, ""); err != nil {
ret.PinErrorMsg = err.Error()
} else if err := node.Pinning.Flush(req.Context); err != nil {
ret.PinErrorMsg = err.Error()
Expand Down
46 changes: 29 additions & 17 deletions core/commands/pin/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ var addPinCmd = &cmds.Command{
Options: []cmds.Option{
cmds.BoolOption(pinRecursiveOptionName, "r", "Recursively pin the object linked to by the specified object(s).").WithDefault(true),
cmds.BoolOption(pinProgressOptionName, "Show progress"),
cmds.StringOption(pinNameOptionName, "n", "An optional name for the pin(s)."),
},
Type: AddPinOutput{},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
Expand All @@ -75,6 +76,7 @@ var addPinCmd = &cmds.Command{

// set recursive flag
recursive, _ := req.Options[pinRecursiveOptionName].(bool)
name, _ := req.Options[pinNameOptionName].(string)

Check warning on line 79 in core/commands/pin/pin.go

View check run for this annotation

Codecov / codecov/patch

core/commands/pin/pin.go#L79

Added line #L79 was not covered by tests
showProgress, _ := req.Options[pinProgressOptionName].(bool)

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

if !showProgress {
added, err := pinAddMany(req.Context, api, enc, req.Arguments, recursive)
added, err := pinAddMany(req.Context, api, enc, req.Arguments, recursive, name)

Check warning on line 92 in core/commands/pin/pin.go

View check run for this annotation

Codecov / codecov/patch

core/commands/pin/pin.go#L92

Added line #L92 was not covered by tests
if err != nil {
return err
}
Expand All @@ -105,7 +107,7 @@ var addPinCmd = &cmds.Command{

ch := make(chan pinResult, 1)
go func() {
added, err := pinAddMany(ctx, api, enc, req.Arguments, recursive)
added, err := pinAddMany(ctx, api, enc, req.Arguments, recursive, name)

Check warning on line 110 in core/commands/pin/pin.go

View check run for this annotation

Codecov / codecov/patch

core/commands/pin/pin.go#L110

Added line #L110 was not covered by tests
ch <- pinResult{pins: added, err: err}
}()

Expand Down Expand Up @@ -181,7 +183,7 @@ var addPinCmd = &cmds.Command{
},
}

func pinAddMany(ctx context.Context, api coreiface.CoreAPI, enc cidenc.Encoder, paths []string, recursive bool) ([]string, error) {
func pinAddMany(ctx context.Context, api coreiface.CoreAPI, enc cidenc.Encoder, paths []string, recursive bool, name string) ([]string, error) {

Check warning on line 186 in core/commands/pin/pin.go

View check run for this annotation

Codecov / codecov/patch

core/commands/pin/pin.go#L186

Added line #L186 was not covered by tests
added := make([]string, len(paths))
for i, b := range paths {
p, err := cmdutils.PathOrCidPath(b)
Expand All @@ -194,7 +196,7 @@ func pinAddMany(ctx context.Context, api coreiface.CoreAPI, enc cidenc.Encoder,
return nil, err
}

if err := api.Pin().Add(ctx, rp, options.Pin.Recursive(recursive)); err != nil {
if err := api.Pin().Add(ctx, rp, options.Pin.Recursive(recursive), options.Pin.Name(name)); err != nil {

Check warning on line 199 in core/commands/pin/pin.go

View check run for this annotation

Codecov / codecov/patch

core/commands/pin/pin.go#L199

Added line #L199 was not covered by tests
return nil, err
}
added[i] = enc.Encode(rp.RootCid())
Expand Down Expand Up @@ -278,9 +280,10 @@ ipfs pin ls -t indirect <cid>
}

const (
pinTypeOptionName = "type"
pinQuietOptionName = "quiet"
pinStreamOptionName = "stream"
pinTypeOptionName = "type"
pinQuietOptionName = "quiet"
pinStreamOptionName = "stream"
pinDetailedOptionName = "detailed"
)

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

typeStr, _ := req.Options[pinTypeOptionName].(string)
stream, _ := req.Options[pinStreamOptionName].(bool)
detailed, _ := req.Options[pinDetailedOptionName].(bool)

Check warning on line 350 in core/commands/pin/pin.go

View check run for this annotation

Codecov / codecov/patch

core/commands/pin/pin.go#L350

Added line #L350 was not covered by tests

switch typeStr {
case "all", "direct", "indirect", "recursive":
Expand All @@ -356,7 +361,7 @@ Example:
lgcList := map[string]PinLsType{}
if !stream {
emit = func(v PinLsOutputWrapper) error {
lgcList[v.PinLsObject.Cid] = PinLsType{Type: v.PinLsObject.Type}
lgcList[v.PinLsObject.Cid] = PinLsType{Type: v.PinLsObject.Type, Label: v.PinLsObject.Name}

Check warning on line 364 in core/commands/pin/pin.go

View check run for this annotation

Codecov / codecov/patch

core/commands/pin/pin.go#L364

Added line #L364 was not covered by tests
return nil
}
} else {
Expand All @@ -368,7 +373,7 @@ Example:
if len(req.Arguments) > 0 {
err = pinLsKeys(req, typeStr, api, emit)
} else {
err = pinLsAll(req, typeStr, api, emit)
err = pinLsAll(req, typeStr, detailed, api, emit)

Check warning on line 376 in core/commands/pin/pin.go

View check run for this annotation

Codecov / codecov/patch

core/commands/pin/pin.go#L376

Added line #L376 was not covered by tests
}
if err != nil {
return err
Expand Down Expand Up @@ -402,17 +407,21 @@ Example:
if stream {
if quiet {
fmt.Fprintf(w, "%s\n", out.PinLsObject.Cid)
} else {
} else if out.PinLsObject.Name == "" {

Check warning on line 410 in core/commands/pin/pin.go

View check run for this annotation

Codecov / codecov/patch

core/commands/pin/pin.go#L410

Added line #L410 was not covered by tests
fmt.Fprintf(w, "%s %s\n", out.PinLsObject.Cid, out.PinLsObject.Type)
} else {
fmt.Fprintf(w, "%s %s %s\n", out.PinLsObject.Cid, out.PinLsObject.Type, out.PinLsObject.Name)

Check warning on line 413 in core/commands/pin/pin.go

View check run for this annotation

Codecov / codecov/patch

core/commands/pin/pin.go#L412-L413

Added lines #L412 - L413 were not covered by tests
}
return nil
}

for k, v := range out.PinLsList.Keys {
if quiet {
fmt.Fprintf(w, "%s\n", k)
} else {
} else if v.Label == "" {

Check warning on line 421 in core/commands/pin/pin.go

View check run for this annotation

Codecov / codecov/patch

core/commands/pin/pin.go#L421

Added line #L421 was not covered by tests
fmt.Fprintf(w, "%s %s\n", k, v.Type)
} else {
fmt.Fprintf(w, "%s %s %s\n", k, v.Type, v.Label)

Check warning on line 424 in core/commands/pin/pin.go

View check run for this annotation

Codecov / codecov/patch

core/commands/pin/pin.go#L423-L424

Added lines #L423 - L424 were not covered by tests
}
}

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

// PinLsType contains the type of a pin
type PinLsType struct {
Type string
Type string
Label string
}

// PinLsObject contains the description of a pin
type PinLsObject struct {
Cid string `json:",omitempty"`
Name string `json:",omitempty"`
Type string `json:",omitempty"`
}

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

func pinLsAll(req *cmds.Request, typeStr string, api coreiface.CoreAPI, emit func(value PinLsOutputWrapper) error) error {
func pinLsAll(req *cmds.Request, typeStr string, detailed bool, api coreiface.CoreAPI, emit func(value PinLsOutputWrapper) error) error {

Check warning on line 516 in core/commands/pin/pin.go

View check run for this annotation

Codecov / codecov/patch

core/commands/pin/pin.go#L516

Added line #L516 was not covered by tests
enc, err := cmdenv.GetCidEncoder(req)
if err != nil {
return err
Expand All @@ -520,7 +531,7 @@ func pinLsAll(req *cmds.Request, typeStr string, api coreiface.CoreAPI, emit fun
panic("unhandled pin type")
}

pins, err := api.Pin().Ls(req.Context, opt)
pins, err := api.Pin().Ls(req.Context, opt, options.Pin.Ls.Detailed(detailed))

Check warning on line 534 in core/commands/pin/pin.go

View check run for this annotation

Codecov / codecov/patch

core/commands/pin/pin.go#L534

Added line #L534 was not covered by tests
if err != nil {
return err
}
Expand All @@ -532,6 +543,7 @@ func pinLsAll(req *cmds.Request, typeStr string, api coreiface.CoreAPI, emit fun
err = emit(PinLsOutputWrapper{
PinLsObject: PinLsObject{
Type: p.Type(),
Name: p.Name(),

Check warning on line 546 in core/commands/pin/pin.go

View check run for this annotation

Codecov / codecov/patch

core/commands/pin/pin.go#L546

Added line #L546 was not covered by tests
Cid: enc.Encode(p.Path().RootCid()),
},
})
Expand Down Expand Up @@ -748,15 +760,15 @@ func pinVerify(ctx context.Context, n *core.IpfsNode, opts pinVerifyOpts, enc ci
out := make(chan any)
go func() {
defer close(out)
for p := range n.Pinning.RecursiveKeys(ctx) {
for p := range n.Pinning.RecursiveKeys(ctx, false) {

Check warning on line 763 in core/commands/pin/pin.go

View check run for this annotation

Codecov / codecov/patch

core/commands/pin/pin.go#L763

Added line #L763 was not covered by tests
if p.Err != nil {
out <- PinVerifyRes{Err: p.Err.Error()}
return
}
pinStatus := checkPin(p.C)
pinStatus := checkPin(p.Pin.Key)

Check warning on line 768 in core/commands/pin/pin.go

View check run for this annotation

Codecov / codecov/patch

core/commands/pin/pin.go#L768

Added line #L768 was not covered by tests
if !pinStatus.Ok || opts.includeOk {
select {
case out <- PinVerifyRes{Cid: enc.Encode(p.C), PinStatus: pinStatus}:
case out <- PinVerifyRes{Cid: enc.Encode(p.Pin.Key), PinStatus: pinStatus}:

Check warning on line 771 in core/commands/pin/pin.go

View check run for this annotation

Codecov / codecov/patch

core/commands/pin/pin.go#L771

Added line #L771 was not covered by tests
case <-ctx.Done():
return
}
Expand Down
2 changes: 1 addition & 1 deletion core/coreapi/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Bloc
}

if settings.Pin {
if err = api.pinning.PinWithMode(ctx, b.Cid(), pin.Recursive); err != nil {
if err = api.pinning.PinWithMode(ctx, b.Cid(), pin.Recursive, ""); err != nil {
return nil, err
}
if err := api.pinning.Flush(ctx); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions core/coreapi/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (adder *pinningAdder) Add(ctx context.Context, nd ipld.Node) error {
return err
}

if err := adder.pinning.PinWithMode(ctx, nd.Cid(), pin.Recursive); err != nil {
if err := adder.pinning.PinWithMode(ctx, nd.Cid(), pin.Recursive, ""); err != nil {
return err
}

Expand All @@ -51,7 +51,7 @@ func (adder *pinningAdder) AddMany(ctx context.Context, nds []ipld.Node) error {
for _, nd := range nds {
c := nd.Cid()
if cids.Visit(c) {
if err := adder.pinning.PinWithMode(ctx, c, pin.Recursive); err != nil {
if err := adder.pinning.PinWithMode(ctx, c, pin.Recursive, ""); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/coreapi/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (api *ObjectAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Obj
}

if options.Pin {
if err := api.pinning.PinWithMode(ctx, dagnode.Cid(), pin.Recursive); err != nil {
if err := api.pinning.PinWithMode(ctx, dagnode.Cid(), pin.Recursive, ""); err != nil {
return path.ImmutablePath{}, err
}

Expand Down
Loading

0 comments on commit f835127

Please sign in to comment.