Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add car root command #283

Merged
merged 7 commits into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmd/car/car.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ func main1() int {
},
},
},
{
Name: "root",
Usage: "Get the root CID of a car",
Action: CarRoot,
},
{
Name: "verify",
Aliases: []string{"v"},
Expand Down
30 changes: 30 additions & 0 deletions cmd/car/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"fmt"
"os"

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

// CarRoot prints the root CID in a car
func CarRoot(c *cli.Context) (err error) {
inStream := os.Stdin
if c.Args().Len() >= 1 {
inStream, err = os.Open(c.Args().First())
if err != nil {
return err
}
}

rd, err := carv2.NewBlockReader(inStream)
if err != nil {
return err
}
for _, r := range rd.Roots {
fmt.Printf("%s\n", r.String())
}

return nil
}
15 changes: 15 additions & 0 deletions cmd/car/testdata/script/root.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
car root ${INPUTS}/sample-v1.car
cmp stdout v1root.txt

car root ${INPUTS}/sample-wrapped-v2.car
cmp stdout v2root.txt

stop stdin_test_needs_car_fix
stdin ${INPUTS}/sample-wrapped-v2.car
car root
cmp stdout v2root.txt

-- v1root.txt --
bafy2bzaced4ueelaegfs5fqu4tzsh6ywbbpfk3cxppupmxfdhbpbhzawfw5oy
-- v2root.txt --
bafy2bzaced4ueelaegfs5fqu4tzsh6ywbbpfk3cxppupmxfdhbpbhzawfw5oy