Skip to content

Commit

Permalink
simplify defer closers
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Sverdlov <[email protected]>
  • Loading branch information
sverdlov93 committed Mar 19, 2024
1 parent 7d4f30f commit a9ddc19
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 30 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
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
6 changes: 2 additions & 4 deletions utils/checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package utils

import (
"bufio"
"errors"
ioutils "github.com/jfrog/gofrog/io"

//#nosec G501 -- md5 is supported by Artifactory.
"crypto/md5"
Expand Down Expand Up @@ -37,9 +37,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
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 a9ddc19

Please sign in to comment.