Skip to content

Commit

Permalink
Update deprecated setup-go action and improve close funcs (#1159)
Browse files Browse the repository at this point in the history
  • Loading branch information
sverdlov93 authored Mar 19, 2024
1 parent bc5cbba commit b631e3d
Show file tree
Hide file tree
Showing 23 changed files with 54 additions and 139 deletions.
8 changes: 3 additions & 5 deletions artifactory/commands/buildinfo/adddependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package buildinfo

import (
"errors"
ioutils "github.com/jfrog/gofrog/io"
regxp "regexp"
"strconv"

Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 2 additions & 10 deletions artifactory/commands/dotnet/dotnetcommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 1 addition & 4 deletions artifactory/commands/generic/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down
19 changes: 3 additions & 16 deletions artifactory/commands/generic/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions artifactory/commands/generic/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
30 changes: 10 additions & 20 deletions artifactory/commands/golang/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
5 changes: 1 addition & 4 deletions artifactory/commands/npm/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 1 addition & 4 deletions artifactory/commands/python/poetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 1 addition & 4 deletions artifactory/commands/python/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 2 additions & 4 deletions artifactory/commands/terraform/terraformpublish.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 3 additions & 7 deletions artifactory/commands/transferconfig/transferconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package transferconfig
import (
"bytes"
"context"
"errors"
"fmt"
"net/http"
"os"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 2 additions & 4 deletions artifactory/commands/transferconfig/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"archive/zip"
"bytes"
"compress/flate"
"errors"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package transferconfigmerge

import (
"errors"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions artifactory/commands/transferfiles/delayedartifactshandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package transferfiles

import (
"encoding/json"
"errors"
"fmt"
"os"
"path"
Expand Down Expand Up @@ -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()))
}
}()

Expand Down
6 changes: 2 additions & 4 deletions artifactory/commands/transferfiles/longpropertycheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package transferfiles

import (
"encoding/json"
"errors"
"fmt"
"io"
"sync"
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions artifactory/commands/utils/precheckrunner/checkrunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package precheckrunner

import (
"context"
"errors"
"fmt"
"github.com/gookit/color"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
Expand Down Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit b631e3d

Please sign in to comment.