chore: defer file closes#1180
Conversation
|
I need to change this Am I right? |
I dont think so, although there could be a Windows-specific issue. |
|
Anyway, I'll change it for consistency. |
|
i'll check it out later (possibly tomorrow) @Haroka-74 |
andrinoff
left a comment
There was a problem hiding this comment.
@Haroka-74 fix the comments
| } | ||
|
|
||
| func runUpdateCLI() error { | ||
| func runUpdateCLI() (err error) { |
There was a problem hiding this comment.
Not sure what is the reason for this implementation? this is heavily unnecessary
| } | ||
| }() | ||
|
|
||
| if _, err = io.Copy(out, in); err != nil { |
There was a problem hiding this comment.
if removed from the above, this has to be a declaration (:=)
|
Hey @andrinoff, just to clarify why the named return is necessary. The original issue asked for two things:
To satisfy point func runUpdateCLI() (err error) {
// ...
defer func() {
if cerr := out.Close(); cerr != nil && err == nil {
err = fmt.Errorf("could not flush new binary to disk: %w", cerr)
}
}()
// ...
}Without the named return, the Let me know what you think, happy to discuss if you have a different approach in mind! |
|
An IIFE removes the need for a named return entirely. Each file gets its own defer scoped to the closure, and errors are returned through a normal local variable: example: var copyErr error
func() {
defer in.Close()
defer func() {
if cerr := out.Close(); copyErr == nil && cerr != nil {
copyErr = fmt.Errorf("could not flush new binary to disk: %w", cerr)
}
}()
if _, copyErr = io.Copy(out, in); copyErr != nil {
copyErr = fmt.Errorf("could not write new binary to disk: %w", copyErr)
}
}()
if copyErr != nil {
return copyErr
} |
|
i'll merge the PR, it is good enough and pretty clean, though i do not like the implementation entirely |
|
/approve |
floatpanebot
left a comment
There was a problem hiding this comment.
Approved on behalf of @andrinoff via /approve command.
|
I can change the implementation if you’d like. |
already merged and releasing, if the issue comes up, i'll link this PR |
What?
Replaced manual
Close()calls on every error path in the binary copy block ofrunUpdateCLIwithdefer, and report close errors onoutvia the named return value.Why?
The previous manual
Close()calls on every return path were fragile and silently discarded flush errors onout.Closes #1056