retrieval: Support listing ipld links in ls

This commit is contained in:
Łukasz Magiera 2021-11-17 17:39:27 +01:00
parent 46ee3a0c46
commit c6101aa02f

View File

@ -5,6 +5,10 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/filecoin-project/lotus/markets/utils"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/traversal"
selectorparse "github.com/ipld/go-ipld-prime/traversal/selector/parse"
"io" "io"
"net/http" "net/http"
"net/url" "net/url"
@ -373,7 +377,12 @@ var clientRetrieveLsCmd = &cli.Command{
Name: "ls", Name: "ls",
Usage: "Show object links", Usage: "Show object links",
ArgsUsage: "[dataCid]", ArgsUsage: "[dataCid]",
Flags: retrFlagsCommon, Flags: append([]cli.Flag{
&cli.BoolFlag{
Name: "ipld",
Usage: "list IPLD-level links",
},
}, retrFlagsCommon...),
Action: func(cctx *cli.Context) error { Action: func(cctx *cli.Context) error {
if cctx.NArg() != 1 { if cctx.NArg() != 1 {
return ShowHelp(cctx, fmt.Errorf("incorrect number of arguments")) return ShowHelp(cctx, fmt.Errorf("incorrect number of arguments"))
@ -432,16 +441,35 @@ var clientRetrieveLsCmd = &cli.Command{
if len(roots) != 1 { if len(roots) != 1 {
return xerrors.Errorf("expected 1 car root, got %d", len(roots)) return xerrors.Errorf("expected 1 car root, got %d", len(roots))
} }
dserv := merkledag.NewDAGService(blockservice.New(cbs, offline.Exchange(cbs))) dserv := merkledag.NewDAGService(blockservice.New(cbs, offline.Exchange(cbs)))
links, err := dserv.GetLinks(ctx, roots[0]) if !cctx.Bool("ipld") {
if err != nil {
return xerrors.Errorf("getting links: %w", err)
}
for _, link := range links { links, err := dserv.GetLinks(ctx, roots[0])
fmt.Printf("%s %s\t%d\n", link.Cid, link.Name, link.Size) if err != nil {
return xerrors.Errorf("getting links: %w", err)
}
for _, link := range links {
fmt.Printf("%s %s\t%d\n", link.Cid, link.Name, link.Size)
}
} else {
sel, _ := selectorparse.ParseJSONSelector(`{"a":{">":{".":{}}}}`)
if err := utils.TraverseDag(
ctx,
dserv,
roots[0],
sel,
func(p traversal.Progress, n ipld.Node, r traversal.VisitReason) error {
if r == traversal.VisitReason_SelectionMatch {
fmt.Println(p.Path)
}
return nil
},
); err != nil {
return err
}
} }
return err return err