Skip to content

Commit

Permalink
Add a car ls --unixfs-blocks to render two-column output like
Browse files Browse the repository at this point in the history
bafy... foo.txt
  • Loading branch information
willscott committed Jan 5, 2024
1 parent 4e07058 commit 030e06f
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)

Check warning on line 185 in cmd/car/list.go

View check run for this annotation

Codecov / codecov/patch

cmd/car/list.go#L184-L185

Added lines #L184 - L185 were not covered by tests
} 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()))
}

Check warning on line 214 in cmd/car/list.go

View check run for this annotation

Codecov / codecov/patch

cmd/car/list.go#L209-L214

Added lines #L209 - L214 were not covered by tests
// recurse into the file/directory
cl, err := l.AsLink()
if err != nil {
Expand Down

0 comments on commit 030e06f

Please sign in to comment.