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 91b4728 commit 4cc16a8
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 33 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
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
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/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 4cc16a8

Please sign in to comment.