Skip to content

Commit

Permalink
make specification of root cid in get-dag command optional (#281)
Browse files Browse the repository at this point in the history
* make specification of root cid in get-dag cmd optional
add a test script
  • Loading branch information
willscott authored Jan 6, 2022
1 parent 91478d2 commit c9eb0b7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
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-dag.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
env SAMPLE_CID='bafy2bzaceaycv7jhaegckatnncu5yugzkrnzeqsppzegufr35lroxxnsnpspu'
car get-dag ${INPUTS}/sample-v1.car ${SAMPLE_CID} out.car
! stderr .
car list out.car
! stderr .
stdout -count=1 '^bafy2bzaceaycv7jhaegckatnncu5yugzkrnzeqsppzegufr35lroxxnsnpspu'

0 comments on commit c9eb0b7

Please sign in to comment.