Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a car ls --unixfs-blocks to render two-column output #514

Merged
merged 1 commit into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cmd/car/car.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ func main1() int {
Name: "unixfs",
Usage: "List unixfs filesystem from the root of the car",
},
&cli.BoolFlag{
Name: "unixfs-blocks",
Usage: "List blocks of unixfs objects in the car",
},
},
},
{
Expand Down
16 changes: 13 additions & 3 deletions cmd/car/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func ListCar(c *cli.Context) error {
}
defer outStream.Close()

if c.Bool("unixfs") {
if c.Bool("unixfs") || c.Bool("unixfs-blocks") {
return listUnixfs(c, outStream)
}

Expand Down Expand Up @@ -180,7 +180,12 @@ func printUnixFSNode(c *cli.Context, prefix string, node cid.Cid, ls *ipld.LinkS
for !i.Done() {
_, l := i.Next()
name := path.Join(prefix, l.Name.Must().String())
fmt.Fprintf(outStream, "%s\n", name)
if c.Bool("unixfs-blocks") {
cidL, _ := l.Hash.AsLink()
fmt.Fprintf(outStream, "%s %s\n", cidL.(cidlink.Link).Cid, name)
} else {
fmt.Fprintf(outStream, "%s\n", name)
}
// recurse into the file/directory
cl, err := l.Hash.AsLink()
if err != nil {
Expand All @@ -201,7 +206,12 @@ func printUnixFSNode(c *cli.Context, prefix string, node cid.Cid, ls *ipld.LinkS
i := hn.Iterator()
for !i.Done() {
n, l := i.Next()
fmt.Fprintf(outStream, "%s\n", path.Join(prefix, n.String()))
if c.Bool("unixfs-blocks") {
cl, _ := l.AsLink()
fmt.Fprintf(outStream, "%s %s\n", cl.(cidlink.Link).Cid, path.Join(prefix, n.String()))
} else {
fmt.Fprintf(outStream, "%s\n", path.Join(prefix, n.String()))
}
// recurse into the file/directory
cl, err := l.AsLink()
if err != nil {
Expand Down