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

make specification of root cid in get-dag command optional #281

Merged
merged 2 commits into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
35 changes: 26 additions & 9 deletions cmd/car/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,27 +66,41 @@ func GetCarBlock(c *cli.Context) error {

// GetCarDag is a command to get a dag out of a car
func GetCarDag(c *cli.Context) error {
if c.Args().Len() < 3 {
return fmt.Errorf("usage: car get-dag [-s selector] <file.car> <root cid> <output file>")
if c.Args().Len() < 2 {
return fmt.Errorf("usage: car get-dag [-s selector] <file.car> [root cid] <output file>")
}

// string to CID for the root of the DAG to extract
rootCid, err := cid.Parse(c.Args().Get(1))
if err != nil {
return err
}
// if root cid is emitted we'll read it from the root of file.car.
output := c.Args().Get(1)
var rootCid cid.Cid

bs, err := blockstore.OpenReadOnly(c.Args().Get(0))
if err != nil {
return err
}

output := c.Args().Get(2)
if c.Args().Len() == 2 {
roots, err := bs.Roots()
if err != nil {
return err
}
if len(roots) != 1 {
return fmt.Errorf("car file has does not have exactly one root, dag root must be specified explicitly")
}
rootCid = roots[0]
} else {
rootCid, err = cid.Parse(output)
if err != nil {
return err
}
output = c.Args().Get(2)
}

strict := c.Bool("strict")

// selector traversal, default to ExploreAllRecursively which only explores the DAG blocks
// because we only care about the blocks loaded during the walk, not the nodes matched
sel := selectorParser.CommonSelector_ExploreAllRecursively
sel := selectorParser.CommonSelector_MatchAllRecursively
if c.IsSet("selector") {
sel, err = selectorParser.ParseJSONSelector(c.String("selector"))
if err != nil {
Expand Down Expand Up @@ -127,6 +141,9 @@ func writeCarV2(rootCid cid.Cid, output string, bs *blockstore.ReadOnly, strict
}
return nil, err
}
if err := outStore.Put(blk); err != nil {
return nil, err
}
return bytes.NewBuffer(blk.RawData()), nil
}
return nil, fmt.Errorf("unknown link type: %T", l)
Expand Down
6 changes: 6 additions & 0 deletions cmd/car/testdata/script/get.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
env SAMPLE_CID='bafy2bzaceaycv7jhaegckatnncu5yugzkrnzeqsppzegufr35lroxxnsnpspu'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

following the other files, shouldn't we call this get-dag.txt?

if you prefer get.txt, then I'd join get-block.txt here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the go file under test is get.go which has both get-block and get-dag. what do you think is more reasonable?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you expect the test scripts to grow further, I'd keep them separate

car get-dag ${INPUTS}/sample-v1.car ${SAMPLE_CID} out.car
! stderr .
car list out.car
! stderr .
stdout -count=1 '^bafy2bzaceaycv7jhaegckatnncu5yugzkrnzeqsppzegufr35lroxxnsnpspu'