From 77d7c41c250a8d302b96523c1ce806690f25033a Mon Sep 17 00:00:00 2001 From: Michael Sverdlov Date: Tue, 19 Mar 2024 17:31:18 +0200 Subject: [PATCH] Simplify defer closer funcs (#242) --- build/build.go | 15 +++--------- build/maven.go | 5 +--- cli/cli.go | 45 +++++++---------------------------- go.mod | 4 ++-- go.sum | 8 +++---- utils/checksum.go | 10 +++----- utils/dependenciesutils.go | 6 ++--- utils/fileutils.go | 9 +++---- utils/goutils.go | 5 +--- utils/goutils_test.go | 4 ++-- utils/pythonutils/piputils.go | 5 +--- 11 files changed, 31 insertions(+), 85 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/cli/cli.go b/cli/cli.go index 1137164b..fc8628bd 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -44,10 +44,7 @@ func GetCommands(logger utils.Log) []*clitool.Command { return } defer func() { - e := bld.Clean() - if err == nil { - err = e - } + err = errors.Join(err, bld.Clean()) }() goModule, err := bld.AddGoModule("") if err != nil { @@ -73,10 +70,7 @@ func GetCommands(logger utils.Log) []*clitool.Command { return } defer func() { - e := bld.Clean() - if err == nil { - err = e - } + err = errors.Join(err, bld.Clean()) }() mavenModule, err := bld.AddMavenModule("") if err != nil { @@ -102,10 +96,7 @@ func GetCommands(logger utils.Log) []*clitool.Command { return } defer func() { - e := bld.Clean() - if err == nil { - err = e - } + err = errors.Join(err, bld.Clean()) }() gradleModule, err := bld.AddGradleModule("") if err != nil { @@ -131,10 +122,7 @@ func GetCommands(logger utils.Log) []*clitool.Command { return } defer func() { - e := bld.Clean() - if err == nil { - err = e - } + err = errors.Join(err, bld.Clean()) }() npmModule, err := bld.AddNpmModule("") if err != nil { @@ -164,10 +152,7 @@ func GetCommands(logger utils.Log) []*clitool.Command { return } defer func() { - e := bld.Clean() - if err == nil { - err = e - } + err = errors.Join(err, bld.Clean()) }() nugetModule, err := bld.AddNugetModules("") if err != nil { @@ -193,10 +178,7 @@ func GetCommands(logger utils.Log) []*clitool.Command { return } defer func() { - e := bld.Clean() - if err == nil { - err = e - } + err = errors.Join(err, bld.Clean()) }() dotnetModule, err := bld.AddDotnetModules("") if err != nil { @@ -223,10 +205,7 @@ func GetCommands(logger utils.Log) []*clitool.Command { return } defer func() { - e := bld.Clean() - if err == nil { - err = e - } + err = errors.Join(err, bld.Clean()) }() yarnModule, err := bld.AddYarnModule("") if err != nil { @@ -257,10 +236,7 @@ func GetCommands(logger utils.Log) []*clitool.Command { return } defer func() { - e := bld.Clean() - if err == nil { - err = e - } + err = errors.Join(err, bld.Clean()) }() pythonModule, err := bld.AddPythonModule("", pythonutils.Pip) if err != nil { @@ -291,10 +267,7 @@ func GetCommands(logger utils.Log) []*clitool.Command { return } defer func() { - e := bld.Clean() - if err == nil { - err = e - } + err = errors.Join(err, bld.Clean()) }() pythonModule, err := bld.AddPythonModule("", pythonutils.Pipenv) if err != nil { diff --git a/go.mod b/go.mod index 5f2f414c..909d30c7 100644 --- a/go.mod +++ b/go.mod @@ -8,10 +8,10 @@ require ( github.com/buger/jsonparser v1.1.1 github.com/jfrog/gofrog v1.6.3 github.com/minio/sha256-simd v1.0.1 - github.com/stretchr/testify v1.8.4 + github.com/stretchr/testify v1.9.0 github.com/urfave/cli/v2 v2.27.1 github.com/xeipuuv/gojsonschema v1.2.0 - golang.org/x/exp v0.0.0-20240213143201-ec583247a57a + golang.org/x/exp v0.0.0-20240318143956-a85f2c67cd81 ) require ( diff --git a/go.sum b/go.sum index 2817b1be..8550baf8 100644 --- a/go.sum +++ b/go.sum @@ -22,8 +22,8 @@ github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/terminalstatic/go-xsd-validate v0.1.5 h1:RqpJnf6HGE2CB/lZB1A8BYguk8uRtcvYAPLCF15qguo= github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho= github.com/urfave/cli/v2 v2.27.1/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ= @@ -35,8 +35,8 @@ github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17 github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= -golang.org/x/exp v0.0.0-20240213143201-ec583247a57a h1:HinSgX1tJRX3KsL//Gxynpw5CTOAIPhgL4W8PNiIpVE= -golang.org/x/exp v0.0.0-20240213143201-ec583247a57a/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc= +golang.org/x/exp v0.0.0-20240318143956-a85f2c67cd81 h1:6R2FC06FonbXQ8pK11/PDFY6N6LWlf9KlzibaCapmqc= +golang.org/x/exp v0.0.0-20240318143956-a85f2c67cd81/go.mod h1:CQ1k9gNrJ50XIzaKCRR2hssIjF07kZFEiieALBM/ARQ= golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e h1:CsOuNlbOuf0mzxJIefr6Q4uAUetRUwZE4qt7VfzP+xo= golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= 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/dependenciesutils.go b/utils/dependenciesutils.go index 47052184..7327a712 100644 --- a/utils/dependenciesutils.go +++ b/utils/dependenciesutils.go @@ -1,6 +1,7 @@ package utils import ( + "errors" "fmt" "os" "path" @@ -59,10 +60,7 @@ func CreateExtractorPropsFile(extractorConfPath, buildInfoPath, buildName, build return "", err } defer func() { - deferErr := propertiesFile.Close() - if err == nil { - err = deferErr - } + err = errors.Join(err, propertiesFile.Close()) }() var buildProperties = map[string]string{ buildInfoPathKey: buildInfoPath, 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/goutils.go b/utils/goutils.go index ceef4b23..1b988dbd 100644 --- a/utils/goutils.go +++ b/utils/goutils.go @@ -143,10 +143,7 @@ func runDependenciesCmd(projectDir string, commandArgs []string, log Log) (outpu } if err == nil { defer func() { - e := os.WriteFile(filepath.Join(projectDir, "go.sum"), sumFileContent, sumFileStat.Mode()) - if err == nil { - err = e - } + err = errors.Join(err, os.WriteFile(filepath.Join(projectDir, "go.sum"), sumFileContent, sumFileStat.Mode())) }() } goCmd := io.NewCommand("go", "", commandArgs) diff --git a/utils/goutils_test.go b/utils/goutils_test.go index 11cf6d82..857b3b7f 100644 --- a/utils/goutils_test.go +++ b/utils/goutils_test.go @@ -97,7 +97,7 @@ func testGetDependenciesList(t *testing.T, testDir string) { err := os.Rename(filepath.Join(goModPath, "go.mod.txt"), filepath.Join(goModPath, "go.mod")) assert.NoError(t, err) defer func() { - err := os.Rename(filepath.Join(goModPath, "go.mod"), filepath.Join(goModPath, "go.mod.txt")) + err = os.Rename(filepath.Join(goModPath, "go.mod"), filepath.Join(goModPath, "go.mod.txt")) assert.NoError(t, err) }() err = os.Rename(filepath.Join(goModPath, "go.sum.txt"), filepath.Join(goModPath, "go.sum")) @@ -110,7 +110,7 @@ func testGetDependenciesList(t *testing.T, testDir string) { err = os.Rename(filepath.Join(goModPath, "test.go.txt"), filepath.Join(goModPath, "test.go")) assert.NoError(t, err) defer func() { - err := os.Rename(filepath.Join(goModPath, "test.go"), filepath.Join(goModPath, "test.go.txt")) + err = os.Rename(filepath.Join(goModPath, "test.go"), filepath.Join(goModPath, "test.go.txt")) assert.NoError(t, err) }() actual, err := GetDependenciesList(goModPath, log) 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.