Skip to content

Commit 1e2f0bd

Browse files
authored
Merge pull request #514 from ipld/feat/ls-unixfs-blocks
Add a `car ls --unixfs-blocks` to render two-column output
2 parents 4e07058 + 030e06f commit 1e2f0bd

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

cmd/car/car.go

+4
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ func main1() int {
207207
Name: "unixfs",
208208
Usage: "List unixfs filesystem from the root of the car",
209209
},
210+
&cli.BoolFlag{
211+
Name: "unixfs-blocks",
212+
Usage: "List blocks of unixfs objects in the car",
213+
},
210214
},
211215
},
212216
{

cmd/car/list.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func ListCar(c *cli.Context) error {
3232
}
3333
defer outStream.Close()
3434

35-
if c.Bool("unixfs") {
35+
if c.Bool("unixfs") || c.Bool("unixfs-blocks") {
3636
return listUnixfs(c, outStream)
3737
}
3838

@@ -180,7 +180,12 @@ func printUnixFSNode(c *cli.Context, prefix string, node cid.Cid, ls *ipld.LinkS
180180
for !i.Done() {
181181
_, l := i.Next()
182182
name := path.Join(prefix, l.Name.Must().String())
183-
fmt.Fprintf(outStream, "%s\n", name)
183+
if c.Bool("unixfs-blocks") {
184+
cidL, _ := l.Hash.AsLink()
185+
fmt.Fprintf(outStream, "%s %s\n", cidL.(cidlink.Link).Cid, name)
186+
} else {
187+
fmt.Fprintf(outStream, "%s\n", name)
188+
}
184189
// recurse into the file/directory
185190
cl, err := l.Hash.AsLink()
186191
if err != nil {
@@ -201,7 +206,12 @@ func printUnixFSNode(c *cli.Context, prefix string, node cid.Cid, ls *ipld.LinkS
201206
i := hn.Iterator()
202207
for !i.Done() {
203208
n, l := i.Next()
204-
fmt.Fprintf(outStream, "%s\n", path.Join(prefix, n.String()))
209+
if c.Bool("unixfs-blocks") {
210+
cl, _ := l.AsLink()
211+
fmt.Fprintf(outStream, "%s %s\n", cl.(cidlink.Link).Cid, path.Join(prefix, n.String()))
212+
} else {
213+
fmt.Fprintf(outStream, "%s\n", path.Join(prefix, n.String()))
214+
}
205215
// recurse into the file/directory
206216
cl, err := l.AsLink()
207217
if err != nil {

0 commit comments

Comments
 (0)