-
Notifications
You must be signed in to change notification settings - Fork 197
mage: Improve error messages and resource handling #10446
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import ( | |
"archive/zip" | ||
"bytes" | ||
"compress/gzip" | ||
"errors" | ||
"fmt" | ||
"hash/fnv" | ||
"io" | ||
|
@@ -666,27 +667,15 @@ func PackageTarGz(spec PackageSpec) error { | |
if err != nil { | ||
return err | ||
} | ||
defer func() { | ||
if err := outFile.Close(); err != nil { | ||
log.Printf("failed to close output file: %v", err) | ||
} | ||
}() | ||
defer closeOrLog(outFile, "output file") | ||
|
||
// Create a gzip writer to our output file | ||
gzWriter := gzip.NewWriter(outFile) | ||
defer func() { | ||
if err := gzWriter.Close(); err != nil { | ||
log.Printf("failed to close gzip writer: %v", err) | ||
} | ||
}() | ||
defer closeOrLog(gzWriter, "gzip writer") | ||
|
||
// Create a new tar archive. | ||
w := tar.NewWriter(gzWriter) | ||
defer func() { | ||
if err := w.Close(); err != nil { | ||
log.Printf("failed to close tar writer: %v", err) | ||
} | ||
}() | ||
defer closeOrLog(gzWriter, "tar writer") | ||
|
||
// // Replace the darwin-universal by darwin-x86_64 and darwin-arm64. Also | ||
// // keep the other files. | ||
|
@@ -759,6 +748,14 @@ func PackageTarGz(spec PackageSpec) error { | |
return nil | ||
} | ||
|
||
func closeOrLog(closer io.Closer, what string) { | ||
err := closer.Close() | ||
if err == nil || errors.Is(err, os.ErrClosed) { | ||
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. This is a change in behavior: you are explicitly ignoring 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. That can be done as well but I figured cleanliness is slightly preferred over having to set nil to the file pointers, at least for this part of the code. Other parts don't even log the |
||
return | ||
} | ||
log.Printf("failed to close %s: %v", what, err) | ||
} | ||
|
||
// PackageDeb packages a deb file. This requires Docker to execute FPM. | ||
func PackageDeb(spec PackageSpec) error { | ||
return runFPM(spec, Deb) | ||
|
@@ -953,7 +950,7 @@ func addFileToZip(ar *zip.Writer, baseDir string, pkgFile PackageFile) error { | |
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
defer closeOrLog(file, "zip input file") | ||
|
||
if _, err = io.Copy(w, file); err != nil { | ||
return err | ||
|
@@ -1034,7 +1031,7 @@ func addFileToTar(ar *tar.Writer, baseDir string, pkgFile PackageFile) error { | |
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
defer closeOrLog(file, "tar input file") | ||
|
||
if _, err = io.Copy(ar, file); err != nil { | ||
return err | ||
|
Uh oh!
There was an error while loading. Please reload this page.