Skip to content

Commit

Permalink
Merge pull request #514 from ipld/feat/ls-unixfs-blocks
Browse files Browse the repository at this point in the history
Add a `car ls --unixfs-blocks` to render two-column output
  • Loading branch information
willscott committed Jan 6, 2024
2 parents 4e07058 + 030e06f commit 1e2f0bd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
4 changes: 4 additions & 0 deletions cmd/car/car.go
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
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

0 comments on commit 1e2f0bd

Please sign in to comment.