From 4cc16a863e332d0b15315e333b3ce8746d084bdf Mon Sep 17 00:00:00 2001 From: Michael Sverdlov Date: Tue, 19 Mar 2024 17:03:31 +0200 Subject: [PATCH] simplify defer closers Signed-off-by: Michael Sverdlov --- build/build.go | 15 +++------------ build/maven.go | 5 +---- utils/checksum.go | 10 +++------- utils/fileutils.go | 9 +++------ utils/pythonutils/piputils.go | 5 +---- 5 files changed, 11 insertions(+), 33 deletions(-) diff --git a/build/build.go b/build/build.go index d6987db3..10b3aaef 100644 --- a/build/build.go +++ b/build/build.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + ioutils "github.com/jfrog/gofrog/io" "os" "path/filepath" "sort" @@ -225,12 +226,7 @@ func (b *Build) SaveBuildInfo(buildInfo *entities.BuildInfo) (err error) { if err != nil { return } - defer func() { - e := tempFile.Close() - if err == nil { - err = e - } - }() + defer ioutils.Close(tempFile, &err) _, err = tempFile.Write(content.Bytes()) return } @@ -257,12 +253,7 @@ func (b *Build) SavePartialBuildInfo(partial *entities.Partial) (err error) { if err != nil { return } - defer func() { - e := tempFile.Close() - if err == nil { - err = e - } - }() + defer ioutils.Close(tempFile, &err) _, err = tempFile.Write(content.Bytes()) return } diff --git a/build/maven.go b/build/maven.go index 091c28f0..36bc29ab 100644 --- a/build/maven.go +++ b/build/maven.go @@ -173,10 +173,7 @@ func (mm *MavenModule) CalcDependencies() (err error) { defer func() { fileExist, e := utils.IsFileExists(mvnRunConfig.buildInfoProperties, false) if fileExist && e == nil { - e = os.Remove(mvnRunConfig.buildInfoProperties) - } - if err == nil { - err = e + err = errors.Join(err, os.Remove(mvnRunConfig.buildInfoProperties)) } }() mvnRunConfig.SetOutputWriter(mm.outputWriter) diff --git a/utils/checksum.go b/utils/checksum.go index 8508404b..b748fa54 100644 --- a/utils/checksum.go +++ b/utils/checksum.go @@ -2,18 +2,16 @@ package utils import ( "bufio" - "errors" - //#nosec G501 -- md5 is supported by Artifactory. "crypto/md5" //#nosec G505 -- sha1 is supported by Artifactory. "crypto/sha1" "fmt" + ioutils "github.com/jfrog/gofrog/io" + "github.com/minio/sha256-simd" "hash" "io" "os" - - "github.com/minio/sha256-simd" ) type Algorithm int @@ -37,9 +35,7 @@ func GetFileChecksums(filePath string, checksumType ...Algorithm) (checksums map if err != nil { return } - defer func() { - err = errors.Join(err, file.Close()) - }() + defer ioutils.Close(file, &err) return CalcChecksums(file, checksumType...) } diff --git a/utils/fileutils.go b/utils/fileutils.go index a5bee074..b21cc9be 100644 --- a/utils/fileutils.go +++ b/utils/fileutils.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + ioutils "github.com/jfrog/gofrog/io" "golang.org/x/exp/slices" "io" "net/http" @@ -576,9 +577,7 @@ func GetFileDetails(filePath string, includeChecksums bool) (details *FileDetail } file, err := os.Open(filePath) - defer func() { - err = errors.Join(err, file.Close()) - }() + defer ioutils.Close(file, &err) if err != nil { return } @@ -595,9 +594,7 @@ func calcChecksumDetails(filePath string) (checksum entities.Checksum, err error if err != nil { return } - defer func() { - err = errors.Join(err, file.Close()) - }() + defer ioutils.Close(file, &err) checksums, err := CalcChecksums(file) if err != nil { diff --git a/utils/pythonutils/piputils.go b/utils/pythonutils/piputils.go index 5953c64b..b420aa19 100644 --- a/utils/pythonutils/piputils.go +++ b/utils/pythonutils/piputils.go @@ -115,10 +115,7 @@ func getEgginfoPkginfoContent(setuppyFilePath string) (output []byte, err error) return nil, err } defer func() { - e := utils.RemoveTempDir(eggBase) - if err == nil { - err = e - } + err = errors.Join(err, utils.RemoveTempDir(eggBase)) }() // Run python 'egg_info --egg-base ' command.