Skip to content

Commit

Permalink
chore: remove deprecated io/ioutil package & fix shadowing/lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
hbollon committed Jun 23, 2023
1 parent a62bd33 commit 48b39a6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
41 changes: 22 additions & 19 deletions helm_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"crypto/sha256"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -154,17 +153,19 @@ 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
}
if filename == "" {
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 {
Expand All @@ -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
}
Expand All @@ -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)
}
}
7 changes: 4 additions & 3 deletions utils.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
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
}

var sf map[string]interface{}
err = yaml.Unmarshal(data, &sf)
if err != nil {
return false, err
return false, err
}

if _, ok := sf["sops"]; ok {
Expand Down

0 comments on commit 48b39a6

Please sign in to comment.