Skip to content

Commit

Permalink
Merge pull request ipld/go-car#350 from ipld/feat/cmd-create-external…
Browse files Browse the repository at this point in the history
…-index

feat(cmd): add index create subcommand to create an external carv2 index

This commit was moved from ipld/go-car@31cce71
  • Loading branch information
willscott authored Feb 1, 2023
2 parents f17129c + 3706bb9 commit b813f4f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ipld/car/cmd/car/car.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ func main1() int {
Usage: "Write output as a v1 or v2 format car",
},
},
Subcommands: []*cli.Command{{
Name: "create",
Usage: "Write out a detached index",
Action: CreateIndex,
}},
},
{
Name: "inspect",
Expand Down
42 changes: 42 additions & 0 deletions ipld/car/cmd/car/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,45 @@ func IndexCar(c *cli.Context) error {
_, err = index.WriteTo(idx, outStream)
return err
}

// CreateIndex is a command to write out an index of the CAR file
func CreateIndex(c *cli.Context) error {
r, err := carv2.OpenReader(c.Args().Get(0))
if err != nil {
return err
}
defer r.Close()

outStream := os.Stdout
if c.Args().Len() >= 2 {
outStream, err = os.Create(c.Args().Get(1))
if err != nil {
return err
}
}
defer outStream.Close()

var mc multicodec.Code
if err := mc.Set(c.String("codec")); err != nil {
return err
}
idx, err := index.New(mc)
if err != nil {
return err
}

dr, err := r.DataReader()
if err != nil {
return err
}

if err := carv2.LoadIndex(idx, dr); err != nil {
return err
}

if _, err := index.WriteTo(idx, outStream); err != nil {
return err
}

return nil
}
3 changes: 3 additions & 0 deletions ipld/car/cmd/car/testdata/script/index-create.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
car index create ${INPUTS}/sample-v1.car sample-v1.car.idx
car detach-index ${INPUTS}/sample-wrapped-v2.car sample-wrapped-v2.car.idx
cmp sample-v1.car.idx sample-wrapped-v2.car.idx

0 comments on commit b813f4f

Please sign in to comment.