Skip to content

Commit

Permalink
Add logic to handle for f.Close() for util/gpg
Browse files Browse the repository at this point in the history
Signed-off-by: xin.li <xin.li@daocloud.io>
  • Loading branch information
my-git9 committed Jul 28, 2022
1 parent e3940cd commit da98962
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions util/gpg/gpg.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"regexp"
"strings"

log "github.com/sirupsen/logrus"

"github.com/argoproj/argo-cd/v2/common"
appsv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
executil "github.com/argoproj/argo-cd/v2/util/exec"
Expand Down Expand Up @@ -163,7 +165,12 @@ func writeKeyToFile(keyData string) (string, error) {
os.Remove(f.Name())
return "", err
}
defer f.Close()
defer func() {
err = f.Close()
if err != nil {
log.Errorf("error closing file %q: %v", f.Name(), err)
}
}()
return f.Name(), nil
}

Expand Down Expand Up @@ -260,7 +267,12 @@ func InitializeGnuPG() error {
return err
}

defer f.Close()
defer func() {
err = f.Close()
if err != nil {
log.Errorf("error closing file %q: %v", f.Name(), err)
}
}()

cmd := exec.Command("gpg", "--no-permission-warning", "--logger-fd", "1", "--batch", "--gen-key", f.Name())
cmd.Env = getGPGEnviron()
Expand All @@ -279,7 +291,12 @@ func ImportPGPKeysFromString(keyData string) ([]*appsv1.GnuPGPublicKey, error) {
if err != nil {
return nil, err
}
defer f.Close()
defer func() {
err = f.Close()
if err != nil {
log.Errorf("error closing file %q: %v", f.Name(), err)
}
}()
return ImportPGPKeys(f.Name())
}

Expand Down Expand Up @@ -399,7 +416,12 @@ func SetPGPTrustLevel(pgpKeys []*appsv1.GnuPGPublicKey, trustLevel string) error
}
}

defer f.Close()
defer func() {
err = f.Close()
if err != nil {
log.Errorf("error closing file %q: %v", f.Name(), err)
}
}()

// Load ownertrust from the file we have constructed and instruct gpg to update the trustdb
cmd := exec.Command("gpg", "--no-permission-warning", "--import-ownertrust", f.Name())
Expand Down

0 comments on commit da98962

Please sign in to comment.