Skip to content

Commit

Permalink
*: address various simple staticcheck warnings
Browse files Browse the repository at this point in the history
Deprecation of `io/ioutil`, removal of unused functions, possible nil
pointer dereference, and other tiny nits.

There are (many) more, but these would require their own (commit)
context.

Signed-off-by: Hidde Beydals <[email protected]>
  • Loading branch information
hiddeco committed Aug 16, 2023
1 parent f85eb01 commit eeaf9f7
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 68 deletions.
8 changes: 4 additions & 4 deletions audit/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ import (
"database/sql"
"flag"
"fmt"
"io/ioutil"
"os"
"os/user"

"github.com/pkg/errors"

// empty import as per https://godoc.org/github.com/lib/pq
_ "github.com/lib/pq"

"gopkg.in/yaml.v3"
"github.com/sirupsen/logrus"
"github.com/getsops/sops/v3/logging"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)

var log *logrus.Logger

func init() {
log = logging.NewLogger("AUDIT")
confBytes, err := ioutil.ReadFile(configFile)
confBytes, err := os.ReadFile(configFile)
if err != nil {
log.WithField("error", err).Debugf("Error reading config")
return
Expand Down
11 changes: 5 additions & 6 deletions cmd/sops/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ package common

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"

"github.com/fatih/color"
wordwrap "github.com/mitchellh/go-wordwrap"
"github.com/urfave/cli"
"github.com/getsops/sops/v3"
"github.com/getsops/sops/v3/cmd/sops/codes"
. "github.com/getsops/sops/v3/cmd/sops/formats"
Expand All @@ -21,7 +18,9 @@ import (
"github.com/getsops/sops/v3/stores/json"
"github.com/getsops/sops/v3/stores/yaml"
"github.com/getsops/sops/v3/version"
"golang.org/x/crypto/ssh/terminal"
"github.com/mitchellh/go-wordwrap"
"github.com/urfave/cli"
"golang.org/x/term"
)

// ExampleFileEmitter emits example files. This is used by the `sops` binary
Expand Down Expand Up @@ -127,7 +126,7 @@ func EncryptTree(opts EncryptTreeOpts) error {

// LoadEncryptedFile loads an encrypted SOPS file, returning a SOPS tree
func LoadEncryptedFile(loader sops.EncryptedFileLoader, inputPath string) (*sops.Tree, error) {
fileBytes, err := ioutil.ReadFile(inputPath)
fileBytes, err := os.ReadFile(inputPath)
if err != nil {
return nil, NewExitError(fmt.Sprintf("Error reading file: %s", err), codes.CouldNotReadInputFile)
}
Expand Down Expand Up @@ -262,7 +261,7 @@ func FixAWSKMSEncryptionContextBug(opts GenericDecryptOpts, tree *sops.Tree) (*s

persistFix := false

if terminal.IsTerminal(int(os.Stdout.Fd())) {
if term.IsTerminal(int(os.Stdout.Fd())) {
var response string
for response != "y" && response != "n" {
fmt.Println("Would you like sops to automatically fix this issue? (y/n): ")
Expand Down
22 changes: 9 additions & 13 deletions cmd/sops/edit.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
package main

import (
"fmt"
"io/ioutil"
"os"

"crypto/md5"
exec "golang.org/x/sys/execabs"
"io"
"strings"

"bufio"
"bytes"

"crypto/md5"
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/google/shlex"
"github.com/getsops/sops/v3"
"github.com/getsops/sops/v3/cmd/sops/codes"
"github.com/getsops/sops/v3/cmd/sops/common"
"github.com/getsops/sops/v3/keyservice"
"github.com/getsops/sops/v3/version"
"github.com/google/shlex"
exec "golang.org/x/sys/execabs"
)

type editOpts struct {
Expand Down Expand Up @@ -109,7 +105,7 @@ func edit(opts editOpts) ([]byte, error) {

func editTree(opts editOpts, tree *sops.Tree, dataKey []byte) ([]byte, error) {
// Create temporary file for editing
tmpdir, err := ioutil.TempDir("", "")
tmpdir, err := os.MkdirTemp("", "")
if err != nil {
return nil, common.NewExitError(fmt.Sprintf("Could not create temporary directory: %s", err), codes.CouldNotWriteOutputFile)
}
Expand Down Expand Up @@ -181,7 +177,7 @@ func runEditorUntilOk(opts runEditorUntilOkOpts) error {
if bytes.Equal(newHash, opts.OriginalHash) {
return common.NewExitError("File has not changed, exiting.", codes.FileHasNotBeenModified)
}
edited, err := ioutil.ReadFile(opts.TmpFile.Name())
edited, err := os.ReadFile(opts.TmpFile.Name())
if err != nil {
return common.NewExitError(fmt.Sprintf("Could not read edited file: %s", err), codes.CouldNotReadInputFile)
}
Expand Down
9 changes: 4 additions & 5 deletions cmd/sops/encrypt.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package main

import (
"io/ioutil"
"path/filepath"

"fmt"
"os"
"path/filepath"

wordwrap "github.com/mitchellh/go-wordwrap"
"github.com/getsops/sops/v3"
"github.com/getsops/sops/v3/cmd/sops/codes"
"github.com/getsops/sops/v3/cmd/sops/common"
"github.com/getsops/sops/v3/keyservice"
"github.com/getsops/sops/v3/version"
"github.com/mitchellh/go-wordwrap"
)

type encryptOpts struct {
Expand Down Expand Up @@ -57,7 +56,7 @@ func ensureNoMetadata(opts encryptOpts, branch sops.TreeBranch) error {

func encrypt(opts encryptOpts) (encryptedFile []byte, err error) {
// Load the file
fileBytes, err := ioutil.ReadFile(opts.InputPath)
fileBytes, err := os.ReadFile(opts.InputPath)
if err != nil {
return nil, common.NewExitError(fmt.Sprintf("Error reading file: %s", err), codes.CouldNotReadInputFile)
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/sops/subcommand/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package exec

import (
"bytes"
"io/ioutil"
"os"
"runtime"
"strings"
Expand All @@ -28,7 +27,7 @@ type ExecOpts struct {
}

func GetFile(dir, filename string) *os.File {
handle, err := ioutil.TempFile(dir, filename)
handle, err := os.CreateTemp(dir, filename)
if err != nil {
log.Fatal(err)
}
Expand All @@ -45,7 +44,7 @@ func ExecWithFile(opts ExecOpts) error {
opts.Fifo = false
}

dir, err := ioutil.TempDir("", ".sops")
dir, err := os.MkdirTemp("", ".sops")
if err != nil {
log.Fatal(err)
}
Expand Down
11 changes: 2 additions & 9 deletions cmd/sops/subcommand/publish/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package publish
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -130,7 +130,7 @@ func Run(opts Opts) error {
return common.NewExitError(fmt.Sprintf("Could not marshal tree: %s", err), codes.ErrorDumpingTree)
}
} else {
fileContents, err = ioutil.ReadFile(path)
fileContents, err = os.ReadFile(path)
if err != nil {
return fmt.Errorf("could not read file: %s", err)
}
Expand Down Expand Up @@ -184,10 +184,3 @@ func Run(opts Opts) error {

return nil
}

func min(a, b int) int {
if a < b {
return a
}
return b
}
7 changes: 3 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ package config //import "github.com/getsops/sops/v3/config"

import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
"strings"

"github.com/sirupsen/logrus"
"github.com/getsops/sops/v3"
"github.com/getsops/sops/v3/age"
"github.com/getsops/sops/v3/azkv"
Expand All @@ -22,6 +20,7 @@ import (
"github.com/getsops/sops/v3/logging"
"github.com/getsops/sops/v3/pgp"
"github.com/getsops/sops/v3/publish"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -223,7 +222,7 @@ func getKeyGroupsFromCreationRule(cRule *creationRule, kmsEncryptionContext map[
}

func loadConfigFile(confPath string) (*configFile, error) {
confBytes, err := ioutil.ReadFile(confPath)
confBytes, err := os.ReadFile(confPath)
if err != nil {
return nil, fmt.Errorf("could not read config file: %s", err)
}
Expand Down Expand Up @@ -329,7 +328,7 @@ func parseCreationRuleForFile(conf *configFile, confPath, filePath string, kmsEn
}

// compare file path relative to path of config file
filePath = strings.TrimPrefix(filePath, configDir + string(filepath.Separator))
filePath = strings.TrimPrefix(filePath, configDir+string(filepath.Separator))

var rule *creationRule

Expand Down
4 changes: 2 additions & 2 deletions decrypt/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package decrypt // import "github.com/getsops/sops/v3/decrypt"

import (
"fmt"
"io/ioutil"
"os"
"time"

"github.com/getsops/sops/v3/aes"
Expand All @@ -18,7 +18,7 @@ import (
// file and returns its cleartext data in an []byte
func File(path, format string) (cleartext []byte, err error) {
// Read the file into an []byte
encryptedData, err := ioutil.ReadFile(path)
encryptedData, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("Failed to read %q: %w", path, err)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ require (
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.8.4
github.com/urfave/cli v1.22.14
golang.org/x/crypto v0.12.0
golang.org/x/net v0.14.0
golang.org/x/sys v0.11.0
golang.org/x/term v0.11.0
Expand Down Expand Up @@ -117,6 +116,7 @@ require (
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/mod v0.9.0 // indirect
golang.org/x/oauth2 v0.11.0 // indirect
golang.org/x/sync v0.3.0 // indirect
Expand Down
2 changes: 1 addition & 1 deletion keyservice/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func keyToString(key *Key) string {
case *Key_VaultKey:
return fmt.Sprintf("Hashicorp Vault key with URI %s/v1/%s/keys/%s", k.VaultKey.VaultAddress, k.VaultKey.EnginePath, k.VaultKey.KeyName)
default:
return fmt.Sprintf("Unknown key type")
return "Unknown key type"
}
}

Expand Down
8 changes: 4 additions & 4 deletions pgp/keysource.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (

"github.com/ProtonMail/go-crypto/openpgp"
"github.com/ProtonMail/go-crypto/openpgp/armor"
"github.com/getsops/sops/v3/logging"
"github.com/sirupsen/logrus"
gpgagent "github.com/getsops/gopgagent"
"github.com/getsops/sops/v3/logging"
"golang.org/x/term"
)

Expand All @@ -49,8 +49,8 @@ var (

// log is the global logger for any PGP MasterKey.
// TODO(hidde): this is not-so-nice for any implementation other than the CLI,
// as it becomes difficult to sugar the logger with data for e.g. individual
// processes.
// as it becomes difficult to sugar the logger with data for e.g. individual
// processes.
var log *logrus.Logger

func init() {
Expand Down Expand Up @@ -588,8 +588,8 @@ func loadRing(path string) (openpgp.EntityList, error) {
func fingerprintIndex(ring openpgp.EntityList) map[string]openpgp.Entity {
fps := make(map[string]openpgp.Entity)
for _, entity := range ring {
fp := strings.ToUpper(hex.EncodeToString(entity.PrimaryKey.Fingerprint[:]))
if entity != nil {
fp := strings.ToUpper(hex.EncodeToString(entity.PrimaryKey.Fingerprint[:]))
fps[fp] = *entity
}
}
Expand Down
2 changes: 1 addition & 1 deletion pgp/keysource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ func Test_shortenFingerprint(t *testing.T) {
func TestPGP(t *testing.T) {
key := NewMasterKeyFromFingerprint("FBC7B9E2A4F9289AC0C1D4843D16CEE4A27381B4")
f := func(x []byte) bool {
if x == nil || len(x) == 0 {
if len(x) == 0 {
return true
}
if err := key.Encrypt(x); err != nil {
Expand Down
Loading

0 comments on commit eeaf9f7

Please sign in to comment.