From 48b39a6683d1f9dd2f45d5ff22cf7865573d3876 Mon Sep 17 00:00:00 2001 From: Hugo Bollon Date: Wed, 21 Jun 2023 15:49:12 +0200 Subject: [PATCH] chore: remove deprecated io/ioutil package & fix shadowing/lint issues --- helm_wrapper.go | 41 ++++++++++++++++++++++------------------- utils.go | 7 ++++--- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/helm_wrapper.go b/helm_wrapper.go index 8bae607..76851e4 100644 --- a/helm_wrapper.go +++ b/helm_wrapper.go @@ -3,7 +3,6 @@ package main import ( "crypto/sha256" "fmt" - "io/ioutil" "os" "os/exec" "path" @@ -15,15 +14,15 @@ import ( ) type HelmWrapper struct { - Errors []error + Errors []error errMutex sync.Mutex ExitCode int - helmBinPath string + helmBinPath string pipeWriterWaitGroup sync.WaitGroup - valuesArgRegexp *regexp.Regexp - temporaryDirectory string + valuesArgRegexp *regexp.Regexp + temporaryDirectory string } func NewHelmWrapper() (*HelmWrapper, error) { @@ -63,19 +62,19 @@ func (c *HelmWrapper) pipeWriter(outPipeName string, data []byte) { cleartextSecretFile, err := os.OpenFile(outPipeName, os.O_WRONLY, 0) if err != nil { - c.errorf("failed to open cleartext secret pipe '%s' in pipe writer: %s", outPipeName, err) + _ = c.errorf("failed to open cleartext secret pipe '%s' in pipe writer: %s", outPipeName, err) return } defer func() { - err := cleartextSecretFile.Close() + err = cleartextSecretFile.Close() if err != nil { - c.errorf("failed to close cleartext secret pipe '%s' in pipe writer: %s", outPipeName, err) + _ = c.errorf("failed to close cleartext secret pipe '%s' in pipe writer: %s", outPipeName, err) } }() _, err = cleartextSecretFile.Write(data) if err != nil { - c.errorf("failed to write cleartext secret to pipe '%s': %s", outPipeName, err) + _ = c.errorf("failed to write cleartext secret to pipe '%s': %s", outPipeName, err) } } @@ -114,14 +113,14 @@ func (c *HelmWrapper) replaceValueFileArg(args []string, cleartextSecretFilename func (c *HelmWrapper) mkTmpDir() (func(), error) { var err error - c.temporaryDirectory, err = ioutil.TempDir("", fmt.Sprintf("%s.", path.Base(os.Args[0]))) + c.temporaryDirectory, err = os.MkdirTemp("", fmt.Sprintf("%s.", path.Base(os.Args[0]))) if err != nil { return nil, c.errorf("failed to create temporary directory: %s", err) } return func() { err := os.RemoveAll(c.temporaryDirectory) if err != nil { - c.errorf("failed to remove temporary directory '%s': %s", c.temporaryDirectory, err) + _ = c.errorf("failed to remove temporary directory '%s': %s", c.temporaryDirectory, err) } }, nil } @@ -134,7 +133,7 @@ func (c *HelmWrapper) mkPipe(cleartextSecretFilename string) (func(), error) { return func() { err := os.Remove(cleartextSecretFilename) if err != nil { - c.errorf("failed to remove cleartext secret pipe '%s': %s", cleartextSecretFilename, err) + _ = c.errorf("failed to remove cleartext secret pipe '%s': %s", cleartextSecretFilename, err) } }, nil } @@ -154,7 +153,8 @@ func (c *HelmWrapper) RunHelm() { for i := range os.Args { args := os.Args[i:] - filename, cleartextSecretFilename, err := c.valuesArg(args) + var filename, cleartextSecretFilename string + filename, cleartextSecretFilename, err = c.valuesArg(args) if err != nil { return } @@ -162,9 +162,10 @@ func (c *HelmWrapper) RunHelm() { continue } - encrypted, err := DetectSopsYaml(filename) + var encrypted bool + encrypted, err = DetectSopsYaml(filename) if err != nil { - c.errorf("error checking if file is encrypted: %s", err) + _ = c.errorf("error checking if file is encrypted: %s", err) return } if !encrypted { @@ -173,13 +174,15 @@ func (c *HelmWrapper) RunHelm() { c.replaceValueFileArg(args, cleartextSecretFilename) - cleartextSecrets, err := decrypt.File(filename, "yaml") + var cleartextSecrets []byte + cleartextSecrets, err = decrypt.File(filename, "yaml") if err != nil { - c.errorf("failed to decrypt secret file '%s': %s", filename, err) + _ = c.errorf("failed to decrypt secret file '%s': %s", filename, err) return } - cleanFn, err := c.mkPipe(cleartextSecretFilename) + var cleanFn func() + cleanFn, err = c.mkPipe(cleartextSecretFilename) if err != nil { return } @@ -198,6 +201,6 @@ func (c *HelmWrapper) RunHelm() { err = cmd.Run() if err != nil { c.ExitCode = cmd.ProcessState.ExitCode() - c.errorf("failed to run Helm: %s", err) + _ = c.errorf("failed to run Helm: %s", err) } } diff --git a/utils.go b/utils.go index f8012b6..9023e5c 100644 --- a/utils.go +++ b/utils.go @@ -1,12 +1,13 @@ package main import ( - "io/ioutil" + "os" + "gopkg.in/yaml.v3" ) func DetectSopsYaml(filename string) (bool, error) { - data, err := ioutil.ReadFile(filename) + data, err := os.ReadFile(filename) if err != nil { return false, err } @@ -14,7 +15,7 @@ func DetectSopsYaml(filename string) (bool, error) { var sf map[string]interface{} err = yaml.Unmarshal(data, &sf) if err != nil { - return false, err + return false, err } if _, ok := sf["sops"]; ok {