From bc5cbba1542b0c94f4ff0bf7fb90feb6404b27d8 Mon Sep 17 00:00:00 2001 From: Michael Sverdlov Date: Tue, 19 Mar 2024 16:59:27 +0200 Subject: [PATCH 1/2] Simplify file closer funcs (#1158) --- .github/workflows/analysis.yml | 10 +++--- .github/workflows/test.yml | 5 +-- artifactory/commands/generic/delete.go | 15 ++------ artifactory/commands/generic/upload.go | 13 +++---- artifactory/commands/golang/publish.go | 6 ++-- artifactory/commands/npm/publish.go | 8 ++--- .../commands/python/dependencies/cache.go | 15 ++------ .../python/dependencies/dependencies.go | 8 ++--- .../commands/terraform/terraformpublish.go | 8 ++--- .../commands/transferfiles/errorshandler.go | 11 ++---- .../commands/transferfiles/transfer.go | 10 ++---- artifactory/commands/utils/result_test.go | 8 ++--- artifactory/commands/utils/transfer.go | 8 ++--- artifactory/commands/utils/yarnutils.go | 8 ++--- artifactory/utils/container/buildinfo.go | 8 ++--- artifactory/utils/npm/config-list.go | 6 ++-- artifactory/utils/search.go | 15 ++------ artifactory/utils/transfersettings.go | 11 ++---- common/commands/config.go | 5 +-- go.mod | 18 +++++----- go.sum | 35 ++++++++++--------- utils/config/encryption.go | 8 ++--- utils/config/tokenrefresh.go | 11 ++---- utils/coreutils/tableutils.go | 6 ++-- utils/plugins/utils.go | 6 ++-- 25 files changed, 85 insertions(+), 177 deletions(-) diff --git a/.github/workflows/analysis.yml b/.github/workflows/analysis.yml index 23a98a6e9..a8a77bee8 100644 --- a/.github/workflows/analysis.yml +++ b/.github/workflows/analysis.yml @@ -13,10 +13,11 @@ jobs: - name: Checkout Source uses: actions/checkout@v4 - - name: Install Go - uses: actions/setup-go@v3 + - name: Setup Go + uses: actions/setup-go@v5 with: go-version: 1.20.x + cache: false - name: Static Code Analysis uses: golangci/golangci-lint-action@v3 @@ -31,10 +32,11 @@ jobs: - name: Checkout Source uses: actions/checkout@v4 - - name: Install Go - uses: actions/setup-go@v3 + - name: Setup Go + uses: actions/setup-go@v5 with: go-version: 1.20.x + cache: false - name: Run Gosec Security Scanner # Temporarily set version 2.18.0 to workaround https://github.com/securego/gosec/issues/1046 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a3026f842..1693490dd 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -28,10 +28,11 @@ jobs: - name: Install pipenv & poetry run: python -m pip install pipenv poetry - - name: Install Go - uses: actions/setup-go@v3 + - name: Setup Go + uses: actions/setup-go@v5 with: go-version: 1.20.x + cache: false - name: Install NuGet uses: nuget/setup-nuget@v1 diff --git a/artifactory/commands/generic/delete.go b/artifactory/commands/generic/delete.go index 38f93af67..ede3965c8 100644 --- a/artifactory/commands/generic/delete.go +++ b/artifactory/commands/generic/delete.go @@ -1,6 +1,7 @@ package generic import ( + ioutils "github.com/jfrog/gofrog/io" "github.com/jfrog/jfrog-cli-core/v2/artifactory/utils" "github.com/jfrog/jfrog-cli-core/v2/common/spec" "github.com/jfrog/jfrog-client-go/artifactory/services" @@ -36,12 +37,7 @@ func (dc *DeleteCommand) Run() (err error) { if err != nil { return } - defer func() { - e := reader.Close() - if err == nil { - err = e - } - }() + defer ioutils.Close(reader, &err) allowDelete := true if !dc.quiet { allowDelete, err = utils.ConfirmDelete(reader) @@ -95,12 +91,7 @@ func (dc *DeleteCommand) GetPathsToDelete() (contentReader *content.ContentReade if err != nil { return nil, err } - defer func() { - e := tempMergedReader.Close() - if err == nil { - err = e - } - }() + defer ioutils.Close(tempMergedReader, &err) // After merge, remove top chain dirs as we may encounter duplicates and collisions between files and directories to delete. // For example: // Reader1: {"a"} diff --git a/artifactory/commands/generic/upload.go b/artifactory/commands/generic/upload.go index 9fa2a0831..c89cd9f8b 100644 --- a/artifactory/commands/generic/upload.go +++ b/artifactory/commands/generic/upload.go @@ -5,9 +5,7 @@ import ( buildInfo "github.com/jfrog/build-info-go/entities" - "strconv" - "time" - + ioutils "github.com/jfrog/gofrog/io" "github.com/jfrog/jfrog-cli-core/v2/artifactory/utils" "github.com/jfrog/jfrog-cli-core/v2/common/build" "github.com/jfrog/jfrog-cli-core/v2/common/spec" @@ -18,6 +16,8 @@ import ( ioUtils "github.com/jfrog/jfrog-client-go/utils/io" "github.com/jfrog/jfrog-client-go/utils/io/content" "github.com/jfrog/jfrog-client-go/utils/log" + "strconv" + "time" ) type UploadCommand struct { @@ -263,12 +263,7 @@ func (uc *UploadCommand) handleSyncDeletes(syncDeletesProp string) (err error) { if err != nil { return err } - defer func() { - e := resultItems.Close() - if err == nil { - err = e - } - }() + defer ioutils.Close(resultItems, &err) _, err = servicesManager.DeleteFiles(resultItems) return err } diff --git a/artifactory/commands/golang/publish.go b/artifactory/commands/golang/publish.go index ba123c901..c9a73a3c8 100644 --- a/artifactory/commands/golang/publish.go +++ b/artifactory/commands/golang/publish.go @@ -4,6 +4,7 @@ import ( "archive/zip" "bytes" "encoding/json" + "errors" "fmt" buildinfo "github.com/jfrog/build-info-go/entities" biutils "github.com/jfrog/build-info-go/utils" @@ -57,10 +58,7 @@ func publishPackage(packageVersion, targetRepo, buildName, buildNumber, projectK return nil, nil, err } defer func() { - e := fileutils.RemoveTempDir(tempDirPath) - if err == nil { - err = e - } + err = errors.Join(err, fileutils.RemoveTempDir(tempDirPath)) }() var zipArtifact *buildinfo.Artifact diff --git a/artifactory/commands/npm/publish.go b/artifactory/commands/npm/publish.go index 764fc1b73..333655bba 100644 --- a/artifactory/commands/npm/publish.go +++ b/artifactory/commands/npm/publish.go @@ -5,6 +5,7 @@ import ( "compress/gzip" "errors" "fmt" + ioutils "github.com/jfrog/gofrog/io" "io" "os" "path/filepath" @@ -198,12 +199,7 @@ func (npc *NpmPublishCommand) Run() (err error) { if err != nil { return err } - defer func() { - e := npc.artifactsDetailsReader.Close() - if err == nil { - err = e - } - }() + defer ioutils.Close(npc.artifactsDetailsReader, &err) err = npmModule.AddArtifacts(buildArtifacts...) if err != nil { return errorutils.CheckError(err) diff --git a/artifactory/commands/python/dependencies/cache.go b/artifactory/commands/python/dependencies/cache.go index 3a15ad8df..71b5dd215 100644 --- a/artifactory/commands/python/dependencies/cache.go +++ b/artifactory/commands/python/dependencies/cache.go @@ -3,6 +3,7 @@ package dependencies import ( "encoding/json" buildinfo "github.com/jfrog/build-info-go/entities" + ioutils "github.com/jfrog/gofrog/io" "github.com/jfrog/jfrog-client-go/utils/errorutils" "github.com/jfrog/jfrog-client-go/utils/io/fileutils" "io" @@ -31,12 +32,7 @@ func GetProjectDependenciesCache(cacheDir string) (cache *DependenciesCache, err if errorutils.CheckError(err) != nil { return nil, err } - defer func() { - e := jsonFile.Close() - if err == nil { - err = e - } - }() + defer ioutils.Close(jsonFile, &err) byteValue, err := io.ReadAll(jsonFile) if errorutils.CheckError(err) != nil { return nil, err @@ -66,12 +62,7 @@ func UpdateDependenciesCache(updatedMap map[string]buildinfo.Dependency, cacheDi if err != nil { return errorutils.CheckError(err) } - defer func() { - e := cacheFile.Close() - if err == nil { - err = e - } - }() + defer ioutils.Close(cacheFile, &err) _, err = cacheFile.Write(content) if err != nil { return errorutils.CheckError(err) diff --git a/artifactory/commands/python/dependencies/dependencies.go b/artifactory/commands/python/dependencies/dependencies.go index 8ab970851..142855d7c 100644 --- a/artifactory/commands/python/dependencies/dependencies.go +++ b/artifactory/commands/python/dependencies/dependencies.go @@ -3,6 +3,7 @@ package dependencies import ( "encoding/json" "fmt" + ioutils "github.com/jfrog/gofrog/io" "io" "strings" @@ -96,12 +97,7 @@ func getDependencyChecksumFromArtifactory(servicesManager artifactory.Artifactor if err != nil { return } - defer func() { - e := stream.Close() - if err == nil { - err = e - } - }() + defer ioutils.Close(stream, &err) result, err := io.ReadAll(stream) if err != nil { return diff --git a/artifactory/commands/terraform/terraformpublish.go b/artifactory/commands/terraform/terraformpublish.go index 6dbdefd36..517160a8e 100644 --- a/artifactory/commands/terraform/terraformpublish.go +++ b/artifactory/commands/terraform/terraformpublish.go @@ -2,6 +2,7 @@ package terraform import ( buildInfo "github.com/jfrog/build-info-go/entities" + ioutils "github.com/jfrog/gofrog/io" "github.com/jfrog/gofrog/parallel" commandsUtils "github.com/jfrog/jfrog-cli-core/v2/artifactory/commands/utils" "github.com/jfrog/jfrog-cli-core/v2/artifactory/utils" @@ -304,12 +305,7 @@ func readArtifactsFromSummary(summary *servicesUtils.OperationSummary) (artifact if artifactsDetailsReader == nil { return []buildInfo.Artifact{}, nil } - defer func() { - e := artifactsDetailsReader.Close() - if err == nil { - err = e - } - }() + defer ioutils.Close(artifactsDetailsReader, &err) return servicesUtils.ConvertArtifactsDetailsToBuildInfoArtifacts(artifactsDetailsReader) } diff --git a/artifactory/commands/transferfiles/errorshandler.go b/artifactory/commands/transferfiles/errorshandler.go index db9a8cb3e..b075d03b9 100644 --- a/artifactory/commands/transferfiles/errorshandler.go +++ b/artifactory/commands/transferfiles/errorshandler.go @@ -2,6 +2,7 @@ package transferfiles import ( "encoding/json" + "errors" "fmt" "github.com/jfrog/jfrog-cli-core/v2/artifactory/commands/transferfiles/api" "github.com/jfrog/jfrog-cli-core/v2/artifactory/commands/transferfiles/state" @@ -119,10 +120,7 @@ func (mng *TransferErrorsMng) start() (err error) { return err } defer func() { - e := mng.errorWriterMng.retryable.closeWriter() - if err == nil { - err = e - } + err = errors.Join(err, mng.errorWriterMng.retryable.closeWriter()) }() writerMng.retryable = errorWriter{writer: writerRetry, filePath: retryFilePath} // Init the content writer which is responsible for writing 'skipped errors' into files. @@ -136,10 +134,7 @@ func (mng *TransferErrorsMng) start() (err error) { return err } defer func() { - e := mng.errorWriterMng.skipped.closeWriter() - if err == nil { - err = e - } + err = errors.Join(err, mng.errorWriterMng.skipped.closeWriter()) }() writerMng.skipped = errorWriter{writer: writerSkip, filePath: skipFilePath} mng.errorWriterMng = writerMng diff --git a/artifactory/commands/transferfiles/transfer.go b/artifactory/commands/transferfiles/transfer.go index ed696c1d1..92840e75f 100644 --- a/artifactory/commands/transferfiles/transfer.go +++ b/artifactory/commands/transferfiles/transfer.go @@ -390,10 +390,7 @@ func (tdc *TransferFilesCommand) transferSingleRepo(sourceRepoKey string, target return } defer func() { - e := restoreFunc() - if err == nil { - err = e - } + err = errors.Join(err, restoreFunc()) }() if err = tdc.initCurThreads(buildInfoRepo); err != nil { @@ -643,10 +640,7 @@ func (tdc *TransferFilesCommand) cleanup(originalErr error, sourceRepos []string err = originalErr // Quit progress bar (before printing logs) if tdc.progressbar != nil { - e := tdc.progressbar.Quit() - if err == nil { - err = e - } + err = errors.Join(err, tdc.progressbar.Quit()) } // Transferring finished successfully if originalErr == nil { diff --git a/artifactory/commands/utils/result_test.go b/artifactory/commands/utils/result_test.go index 81423a2ff..380fdbe76 100644 --- a/artifactory/commands/utils/result_test.go +++ b/artifactory/commands/utils/result_test.go @@ -2,6 +2,7 @@ package utils import ( biutils "github.com/jfrog/build-info-go/utils" + ioutils "github.com/jfrog/gofrog/io" testsutils "github.com/jfrog/jfrog-client-go/utils/tests" "os" "path" @@ -43,12 +44,7 @@ func createTempDeployableArtifactFile() (filePath string, err error) { if errorutils.CheckError(err) != nil { return } - defer func() { - e := summary.Close() - if err == nil { - err = e - } - }() + defer ioutils.Close(summary, &err) tmpDir, err := fileutils.CreateTempDir() if err != nil { return diff --git a/artifactory/commands/utils/transfer.go b/artifactory/commands/utils/transfer.go index 71fabd4b1..ae9117b25 100644 --- a/artifactory/commands/utils/transfer.go +++ b/artifactory/commands/utils/transfer.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "github.com/gocarina/gocsv" + ioutils "github.com/jfrog/gofrog/io" logutils "github.com/jfrog/jfrog-cli-core/v2/utils/log" "github.com/jfrog/jfrog-client-go/http/jfroghttpclient" "github.com/jfrog/jfrog-client-go/utils/errorutils" @@ -54,12 +55,7 @@ func CreateCSVFile(filePrefix string, items interface{}, timeStarted time.Time) return } csvPath = summaryCsv.Name() - defer func() { - e := summaryCsv.Close() - if err == nil { - err = e - } - }() + defer ioutils.Close(summaryCsv, &err) // Marshal JSON typed items array to CSV file err = errorutils.CheckError(gocsv.MarshalFile(items, summaryCsv)) return diff --git a/artifactory/commands/utils/yarnutils.go b/artifactory/commands/utils/yarnutils.go index cde89b031..6d7880152 100644 --- a/artifactory/commands/utils/yarnutils.go +++ b/artifactory/commands/utils/yarnutils.go @@ -2,6 +2,7 @@ package utils import ( "encoding/json" + ioutils "github.com/jfrog/gofrog/io" "io" "strconv" "strings" @@ -54,12 +55,7 @@ func getDependencyInfo(name, ver string, previousBuildDependencies map[string]*e if err != nil { return } - defer func() { - e := stream.Close() - if err == nil { - err = e - } - }() + defer ioutils.Close(stream, &err) var result []byte result, err = io.ReadAll(stream) if err != nil { diff --git a/artifactory/utils/container/buildinfo.go b/artifactory/utils/container/buildinfo.go index 7a42f4ac4..89a543494 100644 --- a/artifactory/utils/container/buildinfo.go +++ b/artifactory/utils/container/buildinfo.go @@ -2,6 +2,7 @@ package container import ( "encoding/json" + ioutils "github.com/jfrog/gofrog/io" "os" "path" "strings" @@ -94,12 +95,7 @@ func setBuildProperties(buildName, buildNumber, project string, imageLayers []ut return } reader := content.NewContentReader(pathToFile, content.DefaultKey) - defer func() { - e := reader.Close() - if err == nil { - err = e - } - }() + defer ioutils.Close(reader, &err) _, err = serviceManager.SetProps(services.PropsParams{Reader: reader, Props: props}) return } diff --git a/artifactory/utils/npm/config-list.go b/artifactory/utils/npm/config-list.go index 477648dbf..d129ef53c 100644 --- a/artifactory/utils/npm/config-list.go +++ b/artifactory/utils/npm/config-list.go @@ -1,6 +1,7 @@ package npm import ( + "errors" gofrogcmd "github.com/jfrog/gofrog/io" npmutils "github.com/jfrog/jfrog-cli-core/v2/utils/npm" "github.com/jfrog/jfrog-client-go/utils/errorutils" @@ -12,10 +13,7 @@ import ( func GetConfigList(npmFlags []string, executablePath string) (data []byte, err error) { pipeReader, pipeWriter := io.Pipe() defer func(pipeReader *io.PipeReader) { - e := pipeReader.Close() - if err == nil { - err = e - } + err = errors.Join(err, pipeReader.Close()) }(pipeReader) npmFlags = append(npmFlags, "--json=false") diff --git a/artifactory/utils/search.go b/artifactory/utils/search.go index 1ed5823a5..c72d68d76 100644 --- a/artifactory/utils/search.go +++ b/artifactory/utils/search.go @@ -3,6 +3,7 @@ package utils import ( "encoding/json" "errors" + ioutils "github.com/jfrog/gofrog/io" "github.com/jfrog/jfrog-client-go/artifactory" "github.com/jfrog/jfrog-cli-core/v2/common/spec" @@ -70,12 +71,7 @@ func AqlResultToSearchResult(readers []*content.ContentReader) (contentReader *c if err != nil { return nil, err } - defer func() { - e := writer.Close() - if err == nil { - err = e - } - }() + defer ioutils.Close(writer, &err) for _, reader := range readers { for searchResult := new(utils.ResultItem); reader.NextRecord(searchResult) == nil; searchResult = new(utils.ResultItem) { if err != nil { @@ -149,12 +145,7 @@ func SearchResultNoDate(reader *content.ContentReader) (contentReader *content.C if err != nil { return nil, err } - defer func() { - e := writer.Close() - if err == nil { - err = e - } - }() + defer ioutils.Close(writer, &err) for resultItem := new(SearchResult); reader.NextRecord(resultItem) == nil; resultItem = new(SearchResult) { if err != nil { return nil, err diff --git a/artifactory/utils/transfersettings.go b/artifactory/utils/transfersettings.go index 1a3d36fd8..705743c31 100644 --- a/artifactory/utils/transfersettings.go +++ b/artifactory/utils/transfersettings.go @@ -3,6 +3,7 @@ package utils import ( "bytes" "encoding/json" + "errors" "os" "path/filepath" @@ -54,10 +55,7 @@ func LoadTransferSettings() (settings *TransferSettings, err error) { unlockFunc, err := lock.CreateLock(filepath.Join(locksDirPath, transferSettingsLockFile)) // Defer the lockFile.Unlock() function before throwing a possible error to avoid deadlock situations. defer func() { - e := unlockFunc() - if err == nil { - err = e - } + err = errors.Join(err, unlockFunc()) }() if err != nil { return @@ -99,10 +97,7 @@ func SaveTransferSettings(settings *TransferSettings) (err error) { unlockFunc, err := lock.CreateLock(filepath.Join(locksDirPath, transferSettingsLockFile)) // Defer the lockFile.Unlock() function before throwing a possible error to avoid deadlock situations. defer func() { - e := unlockFunc() - if err == nil { - err = e - } + err = errors.Join(err, unlockFunc()) }() if err != nil { return diff --git a/common/commands/config.go b/common/commands/config.go index a0802387c..53ce635e4 100644 --- a/common/commands/config.go +++ b/common/commands/config.go @@ -118,10 +118,7 @@ func (cc *ConfigCommand) Run() (err error) { unlockFunc, err := lock.CreateLock(lockDirPath) // Defer the lockFile.Unlock() function before throwing a possible error to avoid deadlock situations. defer func() { - e := unlockFunc() - if err == nil { - err = e - } + err = errors.Join(err, unlockFunc()) }() if err != nil { return diff --git a/go.mod b/go.mod index 5d9f3fc81..5114b033d 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( github.com/gocarina/gocsv v0.0.0-20231116093920-b87c2d0e983a github.com/google/uuid v1.6.0 github.com/gookit/color v1.5.4 - github.com/jedib0t/go-pretty/v6 v6.5.4 + github.com/jedib0t/go-pretty/v6 v6.5.5 github.com/jfrog/build-info-go v1.9.24 github.com/jfrog/gofrog v1.6.3 github.com/jfrog/jfrog-client-go v1.38.0 @@ -19,13 +19,13 @@ require ( github.com/manifoldco/promptui v0.9.0 github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c github.com/spf13/viper v1.18.2 - github.com/stretchr/testify v1.8.4 + github.com/stretchr/testify v1.9.0 github.com/urfave/cli v1.22.14 github.com/vbauerster/mpb/v7 v7.5.3 - golang.org/x/exp v0.0.0-20240213143201-ec583247a57a - golang.org/x/mod v0.15.0 + golang.org/x/exp v0.0.0-20240318143956-a85f2c67cd81 + golang.org/x/mod v0.16.0 golang.org/x/sync v0.6.0 - golang.org/x/term v0.17.0 + golang.org/x/term v0.18.0 golang.org/x/text v0.14.0 gopkg.in/yaml.v3 v3.0.1 ) @@ -88,10 +88,10 @@ require ( github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect go.uber.org/atomic v1.9.0 // indirect go.uber.org/multierr v1.9.0 // indirect - golang.org/x/crypto v0.19.0 // indirect - golang.org/x/net v0.21.0 // indirect - golang.org/x/sys v0.17.0 // indirect - golang.org/x/tools v0.18.0 // indirect + golang.org/x/crypto v0.21.0 // indirect + golang.org/x/net v0.22.0 // indirect + golang.org/x/sys v0.18.0 // indirect + golang.org/x/tools v0.19.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/warnings.v0 v0.1.2 // indirect ) diff --git a/go.sum b/go.sum index ee7d37364..58ceb4446 100644 --- a/go.sum +++ b/go.sum @@ -79,8 +79,8 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= -github.com/jedib0t/go-pretty/v6 v6.5.4 h1:gOGo0613MoqUcf0xCj+h/V3sHDaZasfv152G6/5l91s= -github.com/jedib0t/go-pretty/v6 v6.5.4/go.mod h1:5LQIxa52oJ/DlDSLv0HEkWOFMDGoWkJb9ss5KqPpJBg= +github.com/jedib0t/go-pretty/v6 v6.5.5 h1:PpIU8lOjxvVYGGKule0QxxJfNysUSbC9lggQU2cpZJc= +github.com/jedib0t/go-pretty/v6 v6.5.5/go.mod h1:5LQIxa52oJ/DlDSLv0HEkWOFMDGoWkJb9ss5KqPpJBg= github.com/jfrog/archiver/v3 v3.6.0 h1:OVZ50vudkIQmKMgA8mmFF9S0gA47lcag22N13iV3F1w= github.com/jfrog/archiver/v3 v3.6.0/go.mod h1:fCAof46C3rAXgZurS8kNRNdSVMKBbZs+bNNhPYxLldI= github.com/jfrog/build-info-go v1.9.24 h1:MjT+4bYecbNQ+dbLczg0lkE5DoLAhdyrF0cRXtnEJqI= @@ -180,8 +180,9 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -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/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= github.com/terminalstatic/go-xsd-validate v0.1.5 h1:RqpJnf6HGE2CB/lZB1A8BYguk8uRtcvYAPLCF15qguo= @@ -211,14 +212,14 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4= golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= -golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo= -golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= -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/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= +golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= +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/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/mod v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8= -golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic= +golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= @@ -226,8 +227,8 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= -golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= -golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= +golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc= +golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -260,15 +261,15 @@ golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y= -golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= +golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= -golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U= -golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= +golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8= +golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= @@ -282,8 +283,8 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= -golang.org/x/tools v0.18.0 h1:k8NLag8AGHnn+PHbl7g43CtqZAwG60vZkLqgyZgIHgQ= -golang.org/x/tools v0.18.0/go.mod h1:GL7B4CwcLLeo59yx/9UWWuNOW1n3VZ4f5axWfML7Lcg= +golang.org/x/tools v0.19.0 h1:tfGCXNR1OsFG+sVdLAitlpjAvD/I6dHDKnYrpEZUHkw= +golang.org/x/tools v0.19.0/go.mod h1:qoJWxmGSIBmAeriMx19ogtrEPrGtDbPK634QFIcLAhc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/utils/config/encryption.go b/utils/config/encryption.go index 3e040c93d..eb4db4445 100644 --- a/utils/config/encryption.go +++ b/utils/config/encryption.go @@ -5,6 +5,7 @@ import ( "crypto/cipher" "crypto/rand" "encoding/base64" + ioutils "github.com/jfrog/gofrog/io" "io" "os" "strconv" @@ -115,12 +116,7 @@ func getEncryptionKeyFromSecurityConfFile() (key string, err error) { config := viper.New() config.SetConfigType("yaml") f, err := os.Open(secFile) - defer func() { - e := f.Close() - if err == nil { - err = e - } - }() + defer ioutils.Close(f, &err) if err != nil { return "", errorutils.CheckError(err) } diff --git a/utils/config/tokenrefresh.go b/utils/config/tokenrefresh.go index 298a3917d..e8f4da70b 100644 --- a/utils/config/tokenrefresh.go +++ b/utils/config/tokenrefresh.go @@ -1,6 +1,7 @@ package config import ( + "errors" "github.com/jfrog/jfrog-client-go/access" accessservices "github.com/jfrog/jfrog-client-go/access/services" "github.com/jfrog/jfrog-client-go/utils/errorutils" @@ -75,10 +76,7 @@ func tokenRefreshHandler(currentAccessToken string, tokenType TokenType) (newAcc unlockFunc, err := lock.CreateLock(lockDirPath) // Defer the lockFile.Unlock() function before throwing a possible error to avoid deadlock situations. defer func() { - e := unlockFunc() - if err == nil { - err = e - } + err = errors.Join(err, unlockFunc()) }() if err != nil { return @@ -211,10 +209,7 @@ func CreateInitialRefreshableTokensIfNeeded(serverDetails *ServerDetails) (err e unlockFunc, err := lock.CreateLock(lockDirPath) // Defer the lockFile.Unlock() function before throwing a possible error to avoid deadlock situations. defer func() { - e := unlockFunc() - if err == nil { - err = e - } + err = errors.Join(err, unlockFunc()) }() if err != nil { return diff --git a/utils/coreutils/tableutils.go b/utils/coreutils/tableutils.go index afe507016..cfa3a3eb1 100644 --- a/utils/coreutils/tableutils.go +++ b/utils/coreutils/tableutils.go @@ -2,6 +2,7 @@ package coreutils import ( "bufio" + "errors" "fmt" "math" "os" @@ -136,10 +137,7 @@ func PrintTable(rows interface{}, title string, emptyTableMessage string, printE tableWriter.Style().Options.SeparateRows = true stdoutWriter := bufio.NewWriter(os.Stdout) defer func() { - e := stdoutWriter.Flush() - if err == nil { - err = e - } + err = errors.Join(err, stdoutWriter.Flush()) }() tableWriter.SetOutputMirror(stdoutWriter) tableWriter.Render() diff --git a/utils/plugins/utils.go b/utils/plugins/utils.go index c436cb0e0..e678e17c5 100644 --- a/utils/plugins/utils.go +++ b/utils/plugins/utils.go @@ -2,6 +2,7 @@ package plugins import ( "encoding/json" + "errors" "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" "github.com/jfrog/jfrog-cli-core/v2/utils/lock" cliLog "github.com/jfrog/jfrog-cli-core/v2/utils/log" @@ -61,10 +62,7 @@ func readPluginsConfigAndConvertV0tToV1IfNeeded() (err error) { unlockFunc, err = lock.CreateLock(lockDirPath) // Defer the lockFile.Unlock() function before throwing a possible error to avoid deadlock situations. defer func() { - e := unlockFunc() - if err == nil { - err = e - } + err = errors.Join(err, unlockFunc()) }() if err != nil { return From b631e3df50dd44ae7f90127bb343c5c470d15401 Mon Sep 17 00:00:00 2001 From: Michael Sverdlov Date: Tue, 19 Mar 2024 19:05:25 +0200 Subject: [PATCH 2/2] Update deprecated setup-go action and improve close funcs (#1159) --- .../commands/buildinfo/adddependencies.go | 8 ++--- artifactory/commands/dotnet/dotnetcommand.go | 12 ++------ artifactory/commands/generic/delete.go | 5 +--- artifactory/commands/generic/download.go | 19 ++---------- artifactory/commands/generic/upload.go | 7 +---- artifactory/commands/golang/publish.go | 30 +++++++------------ artifactory/commands/npm/publish.go | 5 +--- artifactory/commands/python/poetry.go | 5 +--- artifactory/commands/python/python.go | 5 +--- .../commands/terraform/terraformpublish.go | 6 ++-- .../commands/transferconfig/transferconfig.go | 10 ++----- artifactory/commands/transferconfig/utils.go | 6 ++-- .../transferconfigmerge.go | 5 ++-- .../transferfiles/delayedartifactshandler.go | 5 ++-- .../transferfiles/longpropertycheck.go | 6 ++-- .../utils/precheckrunner/checkrunner.go | 5 ++-- artifactory/utils/container/buildinfo.go | 16 ++-------- artifactory/utils/utils.go | 7 ++--- common/build/buildutils.go | 10 ++----- common/progressbar/filesprogressbar.go | 6 ++-- go.mod | 4 +-- go.sum | 8 ++--- utils/dependencies/utils.go | 3 +- 23 files changed, 54 insertions(+), 139 deletions(-) diff --git a/artifactory/commands/buildinfo/adddependencies.go b/artifactory/commands/buildinfo/adddependencies.go index fc389aa2b..d35d3731e 100644 --- a/artifactory/commands/buildinfo/adddependencies.go +++ b/artifactory/commands/buildinfo/adddependencies.go @@ -2,6 +2,7 @@ package buildinfo import ( "errors" + ioutils "github.com/jfrog/gofrog/io" regxp "regexp" "strconv" @@ -316,15 +317,12 @@ func convertFileInfoToDependencies(files map[string]*fileutils.FileDetails) []bu func searchItems(spec *spec.SpecFiles, servicesManager artifactory.ArtifactoryServicesManager) (resultReader *content.ContentReader, err error) { temp := []*content.ContentReader{} var searchParams services.SearchParams - var reader *content.ContentReader defer func() { for _, reader := range temp { - e := reader.Close() - if err == nil { - err = e - } + ioutils.Close(reader, &err) } }() + var reader *content.ContentReader for i := 0; i < len(spec.Files); i++ { searchParams, err = utils.GetSearchParams(spec.Get(i)) if err != nil { diff --git a/artifactory/commands/dotnet/dotnetcommand.go b/artifactory/commands/dotnet/dotnetcommand.go index 60c79e4e0..54ddebe93 100644 --- a/artifactory/commands/dotnet/dotnetcommand.go +++ b/artifactory/commands/dotnet/dotnetcommand.go @@ -118,12 +118,7 @@ func (dc *DotnetCommand) Exec() (err error) { return err } defer func() { - if callbackFunc != nil { - e := callbackFunc() - if err == nil { - err = e - } - } + err = errors.Join(err, callbackFunc()) }() if err = buildInfoModule.CalcDependencies(); err != nil { if dc.isDotnetTestCommand() { @@ -272,10 +267,7 @@ func InitNewConfig(configDirPath, repoName string, server *config.ServerDetails, } log.Debug("Nuget config file created at:", configFile.Name()) defer func() { - e := configFile.Close() - if err == nil { - err = errorutils.CheckError(e) - } + err = errors.Join(err, errorutils.CheckError(configFile.Close())) }() // We would prefer to write the NuGet configuration using the `nuget add source` command, diff --git a/artifactory/commands/generic/delete.go b/artifactory/commands/generic/delete.go index ede3965c8..367965dc2 100644 --- a/artifactory/commands/generic/delete.go +++ b/artifactory/commands/generic/delete.go @@ -68,10 +68,7 @@ func (dc *DeleteCommand) GetPathsToDelete() (contentReader *content.ContentReade var temp []*content.ContentReader defer func() { for _, reader := range temp { - e := reader.Close() - if err == nil { - err = e - } + ioutils.Close(reader, &err) } }() for i := 0; i < len(dc.Spec().Files); i++ { diff --git a/artifactory/commands/generic/download.go b/artifactory/commands/generic/download.go index 293508572..ef5c7f5ab 100644 --- a/artifactory/commands/generic/download.go +++ b/artifactory/commands/generic/download.go @@ -119,23 +119,13 @@ func (dc *DownloadCommand) download() (err error) { log.Error(err) } if summary != nil { - defer func() { - e := summary.ArtifactsDetailsReader.Close() - if err == nil { - err = e - } - }() + defer gofrog.Close(summary.ArtifactsDetailsReader, &err) // If 'detailed summary' was requested, then the reader should not be closed here. // It will be closed after it will be used to generate the summary. if dc.DetailedSummary() { dc.result.SetReader(summary.TransferDetailsReader) } else { - defer func() { - e := summary.TransferDetailsReader.Close() - if err == nil { - err = e - } - }() + defer gofrog.Close(summary.TransferDetailsReader, &err) } totalDownloaded = summary.TotalSucceeded totalFailed = summary.TotalFailed @@ -168,10 +158,7 @@ func (dc *DownloadCommand) download() (err error) { var tmpRoot string tmpRoot, err = createDownloadResultEmptyTmpReflection(summary.TransferDetailsReader) defer func() { - e := fileutils.RemoveTempDir(tmpRoot) - if err == nil { - err = e - } + err = errors.Join(err, fileutils.RemoveTempDir(tmpRoot)) }() if err != nil { return err diff --git a/artifactory/commands/generic/upload.go b/artifactory/commands/generic/upload.go index c89cd9f8b..e346a9d53 100644 --- a/artifactory/commands/generic/upload.go +++ b/artifactory/commands/generic/upload.go @@ -139,12 +139,7 @@ func (uc *UploadCommand) upload() (err error) { } if summary != nil { artifactsDetailsReader = summary.ArtifactsDetailsReader - defer func() { - e := artifactsDetailsReader.Close() - if err == nil { - err = e - } - }() + defer ioutils.Close(artifactsDetailsReader, &err) // If 'detailed summary' was requested, then the reader should not be closed here. // It will be closed after it will be used to generate the summary. if uc.DetailedSummary() { diff --git a/artifactory/commands/golang/publish.go b/artifactory/commands/golang/publish.go index c9a73a3c8..4b042c64c 100644 --- a/artifactory/commands/golang/publish.go +++ b/artifactory/commands/golang/publish.go @@ -85,18 +85,17 @@ func publishPackage(packageVersion, targetRepo, buildName, buildNumber, projectK version := version.NewVersion(artifactoryVersion) if version.AtLeast(_go.ArtifactoryMinSupportedVersion) { log.Debug("Creating info file", projectPath) - pathToInfo, err := createInfoFile(packageVersion) + var pathToInfo string + pathToInfo, err = createInfoFile(packageVersion) if err != nil { return nil, nil, err } defer func() { - e := os.Remove(pathToInfo) - if err == nil { - err = errorutils.CheckError(e) - } + err = errors.Join(err, errorutils.CheckError(os.Remove(pathToInfo))) }() if collectBuildInfo { - infoArtifact, err := createInfoFileArtifact(pathToInfo, packageVersion) + var infoArtifact *buildinfo.Artifact + infoArtifact, err = createInfoFileArtifact(pathToInfo, packageVersion) if err != nil { return nil, nil, err } @@ -123,10 +122,7 @@ func createInfoFile(packageVersion string) (path string, err error) { return "", errorutils.CheckError(err) } defer func() { - e := file.Close() - if err == nil { - err = errorutils.CheckError(e) - } + err = errors.Join(err, errorutils.CheckError(file.Close())) }() _, err = file.Write(content) if err != nil { @@ -153,10 +149,7 @@ func readModFile(version, projectPath string, createArtifact bool) ([]byte, *bui return nil, nil, err } defer func() { - e := modFile.Close() - if err == nil { - err = errorutils.CheckError(e) - } + err = errors.Join(err, errorutils.CheckError(modFile.Close())) }() content, err := io.ReadAll(modFile) if err != nil { @@ -189,10 +182,7 @@ func archive(moduleName, version, projectPath, tempDir string, excludedPatterns openedFile = true defer func() { if openedFile { - e := tempFile.Close() - if err == nil { - err = errorutils.CheckError(e) - } + err = errors.Join(err, errorutils.CheckError(tempFile.Close())) } }() if err = archiveProject(tempFile, projectPath, moduleName, version, excludedPatterns); err != nil { @@ -214,10 +204,10 @@ func archive(moduleName, version, projectPath, tempDir string, excludedPatterns } } // Sync the file before renaming it - if err := tempFile.Sync(); err != nil { + if err = tempFile.Sync(); err != nil { return "", nil, err } - if err := tempFile.Close(); err != nil { + if err = tempFile.Close(); err != nil { return "", nil, err } openedFile = false diff --git a/artifactory/commands/npm/publish.go b/artifactory/commands/npm/publish.go index 333655bba..86b2056ce 100644 --- a/artifactory/commands/npm/publish.go +++ b/artifactory/commands/npm/publish.go @@ -428,10 +428,7 @@ func (npc *NpmPublishCommand) readPackageInfoFromTarball(packedFilePath string) return errorutils.CheckError(err) } defer func() { - e := tarball.Close() - if err == nil { - err = errorutils.CheckError(e) - } + err = errors.Join(err, errorutils.CheckError(tarball.Close())) }() gZipReader, err := gzip.NewReader(tarball) if err != nil { diff --git a/artifactory/commands/python/poetry.go b/artifactory/commands/python/poetry.go index b9367b71c..da91baa99 100644 --- a/artifactory/commands/python/poetry.go +++ b/artifactory/commands/python/poetry.go @@ -47,10 +47,7 @@ func (pc *PoetryCommand) Run() (err error) { } defer func() { if pythonBuildInfo != nil && err != nil { - e := pythonBuildInfo.Clean() - if e != nil { - err = errors.New(err.Error() + "\n" + e.Error()) - } + err = errors.Join(err, pythonBuildInfo.Clean()) } }() err = pc.SetPypiRepoUrlWithCredentials() diff --git a/artifactory/commands/python/python.go b/artifactory/commands/python/python.go index defac7960..8d2de8b3e 100644 --- a/artifactory/commands/python/python.go +++ b/artifactory/commands/python/python.go @@ -43,10 +43,7 @@ func (pc *PythonCommand) Run() (err error) { } defer func() { if pythonBuildInfo != nil && err != nil { - e := pythonBuildInfo.Clean() - if e != nil { - err = errors.New(err.Error() + "\n" + e.Error()) - } + err = errors.Join(err, pythonBuildInfo.Clean()) } }() err = pc.SetPypiRepoUrlWithCredentials() diff --git a/artifactory/commands/terraform/terraformpublish.go b/artifactory/commands/terraform/terraformpublish.go index 517160a8e..0a6e854e7 100644 --- a/artifactory/commands/terraform/terraformpublish.go +++ b/artifactory/commands/terraform/terraformpublish.go @@ -1,6 +1,7 @@ package terraform import ( + "errors" buildInfo "github.com/jfrog/build-info-go/entities" ioutils "github.com/jfrog/gofrog/io" "github.com/jfrog/gofrog/parallel" @@ -335,10 +336,7 @@ func checkIfTerraformModule(path string) (isModule bool, err error) { return false, errorutils.CheckError(err) } defer func() { - e := d.Close() - if err == nil { - err = errorutils.CheckError(e) - } + err = errors.Join(err, d.Close()) }() files, err := d.Readdir(-1) diff --git a/artifactory/commands/transferconfig/transferconfig.go b/artifactory/commands/transferconfig/transferconfig.go index ddc6ce01c..42af06f40 100644 --- a/artifactory/commands/transferconfig/transferconfig.go +++ b/artifactory/commands/transferconfig/transferconfig.go @@ -3,6 +3,7 @@ package transferconfig import ( "bytes" "context" + "errors" "fmt" "net/http" "os" @@ -105,10 +106,7 @@ func (tcc *TransferConfigCommand) Run() (err error) { tcc.LogTitle("Phase 2/5 - Export configuration from the source Artifactory") exportPath, cleanUp, err := tcc.exportSourceArtifactory() defer func() { - cleanUpErr := cleanUp() - if err == nil { - err = cleanUpErr - } + err = errors.Join(err, cleanUp()) }() if err != nil { return @@ -313,9 +311,7 @@ func (tcc *TransferConfigCommand) getEncryptedItems(selectedSourceRepos map[util return "", nil, err } defer func() { - if reactivationErr := reactivateKeyEncryption(); err == nil { - err = reactivationErr - } + err = errors.Join(err, reactivateKeyEncryption()) }() // Download artifactory.config.xml from the source Artifactory server. diff --git a/artifactory/commands/transferconfig/utils.go b/artifactory/commands/transferconfig/utils.go index 75d27310c..58c3b62c5 100644 --- a/artifactory/commands/transferconfig/utils.go +++ b/artifactory/commands/transferconfig/utils.go @@ -4,6 +4,7 @@ import ( "archive/zip" "bytes" "compress/flate" + "errors" "io" "os" "path/filepath" @@ -31,10 +32,7 @@ func archiveConfig(exportPath string, configXml string) (buffer *bytes.Buffer, r return flate.NewWriter(out, flate.BestCompression) }) defer func() { - closeErr := writer.Close() - if retErr == nil { - retErr = errorutils.CheckError(closeErr) - } + retErr = errors.Join(retErr, errorutils.CheckError(writer.Close())) }() err := handleTypoInAccessBootstrap(exportPath) diff --git a/artifactory/commands/transferconfigmerge/transferconfigmerge.go b/artifactory/commands/transferconfigmerge/transferconfigmerge.go index ea1c0d273..cf2b7a986 100644 --- a/artifactory/commands/transferconfigmerge/transferconfigmerge.go +++ b/artifactory/commands/transferconfigmerge/transferconfigmerge.go @@ -1,6 +1,7 @@ package transferconfigmerge import ( + "errors" "fmt" "reflect" "strings" @@ -358,9 +359,7 @@ func (tcmc *TransferConfigMergeCommand) decryptAndGetAllRemoteRepositories(remot return } defer func() { - if reactivationErr := reactivateKeyEncryption(); err == nil { - err = reactivationErr - } + err = errors.Join(err, reactivateKeyEncryption()) }() var remoteRepositoryKeys []string for _, remoteRepositoryDetails := range remoteRepositoriesDetails { diff --git a/artifactory/commands/transferfiles/delayedartifactshandler.go b/artifactory/commands/transferfiles/delayedartifactshandler.go index a5a20cfc9..caf80fcde 100644 --- a/artifactory/commands/transferfiles/delayedartifactshandler.go +++ b/artifactory/commands/transferfiles/delayedartifactshandler.go @@ -2,6 +2,7 @@ package transferfiles import ( "encoding/json" + "errors" "fmt" "os" "path" @@ -62,9 +63,7 @@ func getDelaysFilePrefix(repoKey string, phaseStartTime string) string { func (mng *TransferDelayedArtifactsMng) start() (err error) { defer func() { if mng.delayedWriter != nil { - if e := mng.delayedWriter.close(); err == nil { - err = errorutils.CheckError(e) - } + err = errors.Join(err, errorutils.CheckError(mng.delayedWriter.close())) } }() diff --git a/artifactory/commands/transferfiles/longpropertycheck.go b/artifactory/commands/transferfiles/longpropertycheck.go index 99c091a1a..5791c2fb8 100644 --- a/artifactory/commands/transferfiles/longpropertycheck.go +++ b/artifactory/commands/transferfiles/longpropertycheck.go @@ -2,6 +2,7 @@ package transferfiles import ( "encoding/json" + "errors" "fmt" "io" "sync" @@ -228,10 +229,7 @@ func runAqlService(serviceManager artifactory.ArtifactoryServicesManager, query } defer func() { if reader != nil { - e := reader.Close() - if err == nil { - err = errorutils.CheckError(e) - } + err = errors.Join(err, errorutils.CheckError(reader.Close())) } }() respBody, err := io.ReadAll(reader) diff --git a/artifactory/commands/utils/precheckrunner/checkrunner.go b/artifactory/commands/utils/precheckrunner/checkrunner.go index eca8a5e2e..62b435453 100644 --- a/artifactory/commands/utils/precheckrunner/checkrunner.go +++ b/artifactory/commands/utils/precheckrunner/checkrunner.go @@ -2,6 +2,7 @@ package precheckrunner import ( "context" + "errors" "fmt" "github.com/gookit/color" "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" @@ -116,9 +117,7 @@ func (pcr *PreCheckRunner) Run(context context.Context, serverDetails *config.Se } // Execute checks defer func() { - if e := pcr.cleanup(); e != nil && err == nil { - err = e - } + err = errors.Join(err, pcr.cleanup()) }() var checkPassed bool for i, check := range pcr.checks { diff --git a/artifactory/utils/container/buildinfo.go b/artifactory/utils/container/buildinfo.go index 89a543494..412f2f4ab 100644 --- a/artifactory/utils/container/buildinfo.go +++ b/artifactory/utils/container/buildinfo.go @@ -114,9 +114,7 @@ func writeLayersToFile(layers []utils.ResultItem) (filePath string, err error) { if err != nil { return } - defer func() { - err = writer.Close() - }() + defer ioutils.Close(writer, &err) for _, layer := range layers { writer.Write(layer) } @@ -196,11 +194,7 @@ func performSearch(imagePathPattern string, serviceManager artifactory.Artifacto if err != nil { return nil, err } - defer func() { - if deferErr := reader.Close(); err == nil { - err = deferErr - } - }() + defer ioutils.Close(reader, &err) resultMap = make(map[string]*utils.ResultItem) for resultItem := new(utils.ResultItem); reader.NextRecord(resultItem) == nil; resultItem = new(utils.ResultItem) { resultMap[resultItem.Name] = resultItem @@ -220,11 +214,7 @@ func performMultiPlatformImageSearch(imagePathPattern string, serviceManager art if err != nil { return nil, err } - defer func() { - if deferErr := reader.Close(); err == nil { - err = deferErr - } - }() + defer ioutils.Close(reader, &err) pathToSha2 := make(map[string]string) pathToImageLayers := make(map[string][]*utils.ResultItem) resultMap = make(map[string][]*utils.ResultItem) diff --git a/artifactory/utils/utils.go b/artifactory/utils/utils.go index ab4c6a882..1699a4d43 100644 --- a/artifactory/utils/utils.go +++ b/artifactory/utils/utils.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + ioutils "github.com/jfrog/gofrog/io" "io" "net/http" "net/url" @@ -227,11 +228,7 @@ func RemoteUnmarshal(serviceManager artifactory.ArtifactoryServicesManager, remo if err != nil { return } - defer func() { - if localErr := ioReaderCloser.Close(); err == nil { - err = localErr - } - }() + defer ioutils.Close(ioReaderCloser, &err) content, err := io.ReadAll(ioReaderCloser) if err != nil { return errorutils.CheckError(err) diff --git a/common/build/buildutils.go b/common/build/buildutils.go index df222bd5c..4184403aa 100644 --- a/common/build/buildutils.go +++ b/common/build/buildutils.go @@ -120,10 +120,7 @@ func saveBuildData(action interface{}, buildName, buildNumber, projectKey string return err } defer func() { - e := tempFile.Close() - if err == nil { - err = errorutils.CheckError(e) - } + err = errors.Join(err, errorutils.CheckError(tempFile.Close())) }() _, err = tempFile.Write(content.Bytes()) return err @@ -149,10 +146,7 @@ func SaveBuildInfo(buildName, buildNumber, projectKey string, buildInfo *buildIn return err } defer func() { - e := tempFile.Close() - if err == nil { - err = errorutils.CheckError(e) - } + err = errors.Join(err, errorutils.CheckError(tempFile.Close())) }() _, err = tempFile.Write(content.Bytes()) return errorutils.CheckError(err) diff --git a/common/progressbar/filesprogressbar.go b/common/progressbar/filesprogressbar.go index 17de11da0..159f11af9 100644 --- a/common/progressbar/filesprogressbar.go +++ b/common/progressbar/filesprogressbar.go @@ -1,6 +1,7 @@ package progressbar import ( + "errors" "net/url" "os" "strings" @@ -323,10 +324,7 @@ func ExecWithProgress(cmd CommandWithProgress) (err error) { if progressBar != nil { cmd.SetProgress(progressBar) defer func() { - e := progressBar.Quit() - if err == nil { - err = e - } + err = errors.Join(err, progressBar.Quit()) }() } err = commands.Exec(cmd) diff --git a/go.mod b/go.mod index 5114b033d..0cd206a5b 100644 --- a/go.mod +++ b/go.mod @@ -96,8 +96,8 @@ require ( gopkg.in/warnings.v0 v0.1.2 // indirect ) -// replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go dev +replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.28.1-0.20240319160327-95a883923e0b -// replace github.com/jfrog/build-info-go => github.com/jfrog/build-info-go dev +replace github.com/jfrog/build-info-go => github.com/jfrog/build-info-go v1.8.9-0.20240319160313-0093dee91fc1 // replace github.com/jfrog/gofrog => github.com/jfrog/gofrog v1.3.3-0.20231223133729-ef57bd08cedc diff --git a/go.sum b/go.sum index 58ceb4446..ee0ff3a87 100644 --- a/go.sum +++ b/go.sum @@ -83,12 +83,12 @@ github.com/jedib0t/go-pretty/v6 v6.5.5 h1:PpIU8lOjxvVYGGKule0QxxJfNysUSbC9lggQU2 github.com/jedib0t/go-pretty/v6 v6.5.5/go.mod h1:5LQIxa52oJ/DlDSLv0HEkWOFMDGoWkJb9ss5KqPpJBg= github.com/jfrog/archiver/v3 v3.6.0 h1:OVZ50vudkIQmKMgA8mmFF9S0gA47lcag22N13iV3F1w= github.com/jfrog/archiver/v3 v3.6.0/go.mod h1:fCAof46C3rAXgZurS8kNRNdSVMKBbZs+bNNhPYxLldI= -github.com/jfrog/build-info-go v1.9.24 h1:MjT+4bYecbNQ+dbLczg0lkE5DoLAhdyrF0cRXtnEJqI= -github.com/jfrog/build-info-go v1.9.24/go.mod h1:CaCKqcg3V2W9/ZysE4ZvXZMgsvunclhjrTTQQGp3CzM= +github.com/jfrog/build-info-go v1.8.9-0.20240319160313-0093dee91fc1 h1:au9aqxUQIhxRU6vr58rk2/w3hOwxg5ryu6pxgYkixl0= +github.com/jfrog/build-info-go v1.8.9-0.20240319160313-0093dee91fc1/go.mod h1:doFB4bFDVHeGulD6GF9LzsrRaIOrSoklV9DgIAEqHgc= github.com/jfrog/gofrog v1.6.3 h1:F7He0+75HcgCe6SGTSHLFCBDxiE2Ja0tekvvcktW6wc= github.com/jfrog/gofrog v1.6.3/go.mod h1:SZ1EPJUruxrVGndOzHd+LTiwWYKMlHqhKD+eu+v5Hqg= -github.com/jfrog/jfrog-client-go v1.38.0 h1:0QP4/dSmJe0oYUrAqzoPDpGdJHcrOeq9mycnb0pSxqQ= -github.com/jfrog/jfrog-client-go v1.38.0/go.mod h1:EHRLxpu0pIT7+ulYDNQ7IeieYBHMQeEPr8CoBHoJzQY= +github.com/jfrog/jfrog-client-go v1.28.1-0.20240319160327-95a883923e0b h1:pNrQakO24/lNCK8LuBC9FxbfbSO1G8de8n9/FKPGbVc= +github.com/jfrog/jfrog-client-go v1.28.1-0.20240319160327-95a883923e0b/go.mod h1:/crrK2T2rXlJxBQdmw67i91W37/1R+ZM6xxAwVpm16M= github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4= github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= diff --git a/utils/dependencies/utils.go b/utils/dependencies/utils.go index 4f105002c..7be49aa89 100644 --- a/utils/dependencies/utils.go +++ b/utils/dependencies/utils.go @@ -39,8 +39,7 @@ func DownloadExtractor(targetPath, downloadPath string) error { func CreateChecksumFile(targetPath, checksum string) (err error) { out, err := os.Create(targetPath) defer func() { - e := errorutils.CheckError(out.Close()) - err = errors.Join(err, e) + err = errors.Join(err, errorutils.CheckError(out.Close())) }() if errorutils.CheckError(err) != nil { return err