Skip to content

Commit

Permalink
Simplify defer closer funcs (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
sverdlov93 authored Mar 19, 2024
1 parent 91b4728 commit 77d7c41
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 85 deletions.
15 changes: 3 additions & 12 deletions build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
ioutils "github.com/jfrog/gofrog/io"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
5 changes: 1 addition & 4 deletions build/maven.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
45 changes: 9 additions & 36 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down
10 changes: 3 additions & 7 deletions utils/checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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...)
}

Expand Down
6 changes: 2 additions & 4 deletions utils/dependenciesutils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"errors"
"fmt"
"os"
"path"
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 3 additions & 6 deletions utils/fileutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
ioutils "github.com/jfrog/gofrog/io"
"golang.org/x/exp/slices"
"io"
"net/http"
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
5 changes: 1 addition & 4 deletions utils/goutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions utils/goutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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)
Expand Down
5 changes: 1 addition & 4 deletions utils/pythonutils/piputils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <eggBase>' command.
Expand Down

0 comments on commit 77d7c41

Please sign in to comment.