-
Notifications
You must be signed in to change notification settings - Fork 44
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 list
and filter
commands
#227
Merged
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,100 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
"github.com/ipfs/go-cid" | ||
carv2 "github.com/ipld/go-car/v2" | ||
"github.com/ipld/go-car/v2/blockstore" | ||
icarv1 "github.com/ipld/go-car/v2/internal/carv1" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
// FilterCar is a command to select a subset of a car by CID. | ||
func FilterCar(c *cli.Context) error { | ||
r, err := carv2.OpenReader(c.Args().Get(0)) | ||
if err != nil { | ||
return err | ||
} | ||
defer r.Close() | ||
|
||
if c.Args().Len() < 2 { | ||
return fmt.Errorf("an output filename must be provided") | ||
} | ||
roots, err := r.Roots() | ||
if err != nil { | ||
return err | ||
} | ||
bs, err := blockstore.OpenReadWrite(c.Args().Get(1), roots) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Get the set of CIDs from stdin. | ||
inStream := os.Stdin | ||
if c.IsSet("cidFile") { | ||
inStream, err = os.Open(c.String("cidFile")) | ||
if err != nil { | ||
return err | ||
} | ||
defer inStream.Close() | ||
} | ||
cidList, err := parseCIDS(inStream) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("filtering to %d cids\n", len(cidList)) | ||
|
||
cidMap := make(map[cid.Cid]struct{}) | ||
for _, e := range cidList { | ||
cidMap[e] = struct{}{} | ||
} | ||
|
||
rd, err := icarv1.NewCarReader(r.DataReader()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for { | ||
blk, err := rd.Next() | ||
if err != nil { | ||
if err == io.EOF { | ||
break | ||
} | ||
return err | ||
} | ||
if _, ok := cidMap[blk.Cid()]; ok { | ||
if err := bs.Put(blk); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return bs.Finalize() | ||
} | ||
|
||
func parseCIDS(r io.Reader) ([]cid.Cid, error) { | ||
cb := make([]cid.Cid, 0) | ||
br := bufio.NewReader(r) | ||
for { | ||
line, _, err := br.ReadLine() | ||
if err != nil { | ||
if err == io.EOF { | ||
return cb, nil | ||
} | ||
return nil, err | ||
} | ||
trimLine := strings.TrimSpace(string(line)) | ||
if len(trimLine) == 0 { | ||
continue | ||
} | ||
c, err := cid.Parse(trimLine) | ||
if err != nil { | ||
return nil, err | ||
} | ||
cb = append(cb, c) | ||
} | ||
} |
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,47 @@ | ||||||
package main | ||||||
|
||||||
import ( | ||||||
"fmt" | ||||||
"io" | ||||||
"os" | ||||||
|
||||||
carv2 "github.com/ipld/go-car/v2" | ||||||
icarv1 "github.com/ipld/go-car/v2/internal/carv1" | ||||||
"github.com/urfave/cli/v2" | ||||||
) | ||||||
|
||||||
// ListCar is a command to output the cids in a car. | ||||||
func ListCar(c *cli.Context) error { | ||||||
r, err := carv2.OpenReader(c.Args().Get(0)) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
defer r.Close() | ||||||
|
||||||
outStream := os.Stdout | ||||||
if c.Args().Len() >= 2 { | ||||||
outStream, err = os.Create(c.Args().Get(1)) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
} | ||||||
defer outStream.Close() | ||||||
|
||||||
rd, err := icarv1.NewCarReader(r.DataReader()) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use https://pkg.go.dev/github.com/ipld/go-car/v2@master#BlockReader? it was written precisely for this :) and it even supports both carv1 and carv2. |
||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
for { | ||||||
blk, err := rd.Next() | ||||||
if err != nil { | ||||||
if err == io.EOF { | ||||||
break | ||||||
} | ||||||
return err | ||||||
} | ||||||
outStream.WriteString(fmt.Sprintf("%s\n", blk.Cid())) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
return err | ||||||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider making parseCIDs build a map directly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also I wonder if you should at least print a warning on duplicate CIDs