-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add `get block` to car cli for extracting an individual block by CID from a car
- Loading branch information
Showing
2 changed files
with
69 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/ipfs/go-cid" | ||
"github.com/ipld/go-car/v2/blockstore" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
// GetCarBlock is a command to get a block out of a car | ||
func GetCarBlock(c *cli.Context) error { | ||
if c.Args().Len() < 2 { | ||
return fmt.Errorf("usage: car get-block <file.car> <block cid> [output file]") | ||
} | ||
|
||
bs, err := blockstore.OpenReadOnly(c.Args().Get(0)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// string to CID | ||
blkCid, err := cid.Parse(c.Args().Get(1)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
blk, err := bs.Get(blkCid) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
outStream := os.Stdout | ||
if c.Args().Len() >= 3 { | ||
outStream, err = os.Create(c.Args().Get(2)) | ||
if err != nil { | ||
return err | ||
} | ||
defer outStream.Close() | ||
} | ||
|
||
_, err = outStream.Write(blk.RawData()) | ||
return err | ||
} |