Skip to content

Commit

Permalink
add car detach-index list to list detached index contents (#287)
Browse files Browse the repository at this point in the history
* add `car detach-index list` to list detached index contents

Co-authored-by: Masih H. Derkani <[email protected]>
  • Loading branch information
willscott and masih authored Jan 20, 2022
1 parent 3c99491 commit b2c65c2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cmd/car/car.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ func main1() int {
Name: "detach-index",
Usage: "Detach an index to a detached file",
Action: DetachCar,
Subcommands: []*cli.Command{{
Name: "list",
Usage: "List a detached index",
Action: DetachCarList,
}},
},
{
Name: "extract",
Expand Down
34 changes: 34 additions & 0 deletions cmd/car/detach.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"

carv2 "github.com/ipld/go-car/v2"
"github.com/ipld/go-car/v2/index"
"github.com/multiformats/go-multihash"
"github.com/urfave/cli/v2"
)

Expand Down Expand Up @@ -33,3 +35,35 @@ func DetachCar(c *cli.Context) error {
_, err = io.Copy(outStream, r.IndexReader())
return err
}

// DetachCarList prints a list of what's found in a detached index.
func DetachCarList(c *cli.Context) error {
var err error

inStream := os.Stdin
if c.Args().Len() >= 1 {
inStream, err = os.Open(c.Args().First())
if err != nil {
return err
}
defer inStream.Close()
}

idx, err := index.ReadFrom(inStream)
if err != nil {
return err
}

if iidx, ok := idx.(index.IterableIndex); ok {
err := iidx.ForEach(func(mh multihash.Multihash, offset uint64) error {
fmt.Printf("%s %d\n", mh, offset)
return nil
})
if err != nil {
return err
}
return nil
}

return fmt.Errorf("index of codec %s is not iterable", idx.Codec())
}

0 comments on commit b2c65c2

Please sign in to comment.