Skip to content

Commit

Permalink
feat: plumb context changes through CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
guseggert committed Nov 22, 2021
1 parent 8b1ed04 commit 8ad68a4
Show file tree
Hide file tree
Showing 7 changed files with 555 additions and 62 deletions.
4 changes: 2 additions & 2 deletions cmd/car/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func writeFiles(ctx context.Context, bs *blockstore.ReadWrite, paths ...string)
if !ok {
return nil, fmt.Errorf("not a cidlink")
}
blk, err := bs.Get(cl.Cid)
blk, err := bs.Get(ctx, cl.Cid)
if err != nil {
return nil, err
}
Expand All @@ -86,7 +86,7 @@ func writeFiles(ctx context.Context, bs *blockstore.ReadWrite, paths ...string)
if err != nil {
return err
}
bs.Put(blk)
bs.Put(ctx, blk)
return nil
}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/car/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func ExtractCar(c *cli.Context) error {
if !ok {
return nil, fmt.Errorf("not a cidlink")
}
blk, err := bs.Get(cl.Cid)
blk, err := bs.Get(c.Context, cl.Cid)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/car/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func FilterCar(c *cli.Context) error {
return err
}
if _, ok := cidMap[blk.Cid()]; ok {
if err := bs.Put(blk); err != nil {
if err := bs.Put(c.Context, blk); err != nil {
return err
}
}
Expand Down
14 changes: 7 additions & 7 deletions cmd/car/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func GetCarBlock(c *cli.Context) error {
return err
}

blk, err := bs.Get(blkCid)
blk, err := bs.Get(c.Context, blkCid)
if err != nil {
return err
}
Expand Down Expand Up @@ -97,15 +97,15 @@ func GetCarDag(c *cli.Context) error {

switch c.Int("version") {
case 2:
return writeCarV2(rootCid, output, bs, strict, sel, linkVisitOnlyOnce)
return writeCarV2(c.Context, rootCid, output, bs, strict, sel, linkVisitOnlyOnce)
case 1:
return writeCarV1(rootCid, output, bs, strict, sel, linkVisitOnlyOnce)
return writeCarV1(c.Context, rootCid, output, bs, strict, sel, linkVisitOnlyOnce)
default:
return fmt.Errorf("invalid CAR version %d", c.Int("version"))
}
}

func writeCarV2(rootCid cid.Cid, output string, bs *blockstore.ReadOnly, strict bool, sel datamodel.Node, linkVisitOnlyOnce bool) error {
func writeCarV2(ctx context.Context, rootCid cid.Cid, output string, bs *blockstore.ReadOnly, strict bool, sel datamodel.Node, linkVisitOnlyOnce bool) error {
_ = os.Remove(output)

outStore, err := blockstore.OpenReadWrite(output, []cid.Cid{rootCid}, blockstore.AllowDuplicatePuts(false))
Expand All @@ -117,7 +117,7 @@ func writeCarV2(rootCid cid.Cid, output string, bs *blockstore.ReadOnly, strict
ls.TrustedStorage = true
ls.StorageReadOpener = func(_ linking.LinkContext, l datamodel.Link) (io.Reader, error) {
if cl, ok := l.(cidlink.Link); ok {
blk, err := bs.Get(cl.Cid)
blk, err := bs.Get(ctx, cl.Cid)
if err != nil {
if err == ipfsbs.ErrNotFound {
if strict {
Expand Down Expand Up @@ -167,12 +167,12 @@ func writeCarV2(rootCid cid.Cid, output string, bs *blockstore.ReadOnly, strict
return outStore.Finalize()
}

func writeCarV1(rootCid cid.Cid, output string, bs *blockstore.ReadOnly, strict bool, sel datamodel.Node, linkVisitOnlyOnce bool) error {
func writeCarV1(ctx context.Context, rootCid cid.Cid, output string, bs *blockstore.ReadOnly, strict bool, sel datamodel.Node, linkVisitOnlyOnce bool) error {
opts := make([]car.Option, 0)
if linkVisitOnlyOnce {
opts = append(opts, car.TraverseLinksOnlyOnce())
}
sc := car.NewSelectiveCar(context.Background(), bs, []car.Dag{{Root: rootCid, Selector: sel}}, opts...)
sc := car.NewSelectiveCar(ctx, bs, []car.Dag{{Root: rootCid, Selector: sel}}, opts...)
f, err := os.Create(output)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion cmd/car/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func listUnixfs(c *cli.Context, outStream io.Writer) error {
if !ok {
return nil, fmt.Errorf("not a cidlink")
}
blk, err := bs.Get(cl.Cid)
blk, err := bs.Get(c.Context, cl.Cid)
if err != nil {
return nil, err
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ require (
github.com/dustin/go-humanize v1.0.0
github.com/ipfs/go-block-format v0.0.3
github.com/ipfs/go-cid v0.1.0
github.com/ipfs/go-ipfs-blockstore v1.0.3
github.com/ipfs/go-ipfs-blockstore v1.1.1
github.com/ipfs/go-unixfsnode v1.1.4-0.20211105121048-b9b6e9dc571e
github.com/ipld/go-car v0.3.2-0.20211001222544-c93f5367a75c
github.com/ipld/go-car/v2 v2.1.0
github.com/ipld/go-car v0.3.3-0.20211122222057-8a25087f0f84
github.com/ipld/go-car/v2 v2.1.1-0.20211122222057-8a25087f0f84
github.com/ipld/go-codec-dagpb v1.3.0
github.com/ipld/go-ipld-prime v0.12.4-0.20211014180653-3ba656a3bc6b
github.com/multiformats/go-multicodec v0.3.1-0.20210902112759-1539a079fd61
Expand All @@ -18,3 +18,4 @@ require (
github.com/rogpeppe/go-internal v1.8.1-0.20210923151022-86f73c517451
github.com/urfave/cli/v2 v2.3.0
)

Loading

0 comments on commit 8ad68a4

Please sign in to comment.