Add replaceraw#451
Conversation
I wanted to replace raw files like Intel VBT in firmware images. Very similar to replacepe32 Signed-off-by: Daniel Schaefer <dhs@frame.work>
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #451 +/- ##
==========================================
+ Coverage 44.78% 44.79% +0.01%
==========================================
Files 170 171 +1
Lines 12233 12273 +40
==========================================
+ Hits 5478 5498 +20
- Misses 5933 5949 +16
- Partials 822 826 +4 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
rminnich
left a comment
There was a problem hiding this comment.
This is great, you just need to change some of the error handling in code and test.
| name string | ||
| newRaw []byte | ||
| match string | ||
| err string |
There was a problem hiding this comment.
since we first wrote fiano, Go error handling picked up some of the nice bits from Rust.
SO: this err string should be err error
| NewRaw: test.newRaw, | ||
| } | ||
| err = replace.Run(f) | ||
| if err == nil { |
There was a problem hiding this comment.
This should be ! errors.Is(err, test.err) and you can avoid the else in that case.
| return errors.New("no matches found for replacement") | ||
| } | ||
| if len(find.Matches) > 1 { | ||
| return errors.New("multiple matches found! There can be only one. Use find to list all matches") |
There was a problem hiding this comment.
This errors.New should be
return ErrMultilpleMatches, which you can declare as an exported variable, with a commend, right before this function:
var ErrMultipleMatches = errors.New("multiple matches found! There can be only one. Use find to list all matches")
|
|
||
| v.Matches = find.Matches | ||
| if len(find.Matches) == 0 { | ||
| return errors.New("no matches found for replacement") |
There was a problem hiding this comment.
You want to avoid errors.New in anything but a variable declaration, and you want to wrap standard errors for tests. And you might was well be as informative as possible.
return fmt.Errorf("no matches found for %v:%w", theThingYouAreTryingToMatchWhichICan'tSee, os.ErrNotexist)
then in your test, below, you can test for errors.Is(test.err), and test.err can be initialized to os.ErrNotExist, and now you're not string matching any more.
Thanks for this useful contribution!
I wanted to replace raw files like Intel VBT in firmware images. Very similar to replacepe32