Skip to content

Commit

Permalink
Remove ioutil in favour of io and os packages and exclude crypto/dsa …
Browse files Browse the repository at this point in the history
…from linting as the warning is not relevant.
  • Loading branch information
rossmaclean authored and lbajolet-hashicorp committed Feb 3, 2023
1 parent 0ab9c86 commit 45e4613
Show file tree
Hide file tree
Showing 45 changed files with 85 additions and 121 deletions.
4 changes: 2 additions & 2 deletions acctest/provisioneracc/provisioners.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package provisioneracc
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -157,7 +157,7 @@ func LoadBuilderFragment(templateFragmentPath string) (string, error) {
}
defer fragmentFile.Close()

fragmentString, err := ioutil.ReadAll(fragmentFile)
fragmentString, err := io.ReadAll(fragmentFile)
if err != nil {
return "", fmt.Errorf("Unable to read %s", fragmentAbsPath)
}
Expand Down
3 changes: 1 addition & 2 deletions acctest/testutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package testutils
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)

Expand Down Expand Up @@ -48,7 +47,7 @@ func GetArtifact(manifestfilepath string) (ManifestFile, error) {
// "last_run_uuid": "81fc083f-0b78-d815-ed3a-2e5f53b36bff"
// }
manifest := ManifestFile{}
data, err := ioutil.ReadFile(manifestfilepath)
data, err := os.ReadFile(manifestfilepath)
if err != nil {
return manifest, fmt.Errorf("failed to open manifest file %s", manifestfilepath)
}
Expand Down
3 changes: 1 addition & 2 deletions chroot/communicator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (
)

func TestCommunicator_ImplementsCommunicator(t *testing.T) {
var raw interface{}
raw = &Communicator{}
var raw interface{} = &Communicator{}

if _, ok := raw.(packersdk.Communicator); !ok {
t.Fatalf("Communicator should be a communicator")
Expand Down
13 changes: 5 additions & 8 deletions chroot/step_copy_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package chroot
import (
"context"
"fmt"
"io/ioutil"
"io"
"path"
"runtime"
"strings"
Expand All @@ -23,15 +23,14 @@ func testUI() (packersdk.Ui, func() string) {
errorBuffer := &strings.Builder{}
ui := &packersdk.BasicUi{
Reader: strings.NewReader(""),
Writer: ioutil.Discard,
Writer: io.Discard,
ErrorWriter: errorBuffer,
}
return ui, errorBuffer.String
}

func TestCopyFilesCleanupFunc_ImplementsCleanupFunc(t *testing.T) {
var raw interface{}
raw = new(StepCopyFiles)
var raw interface{} = new(StepCopyFiles)
if _, ok := raw.(Cleanup); !ok {
t.Fatalf("cleanup func should be a CleanupFunc")
}
Expand All @@ -50,8 +49,7 @@ func TestCopyFiles_Run(t *testing.T) {

var gotCommand string
commandRunCount := 0
var wrapper common.CommandWrapper
wrapper = func(ran string) (string, error) {
var wrapper common.CommandWrapper = func(ran string) (string, error) {
gotCommand = ran
commandRunCount++
return "", nil
Expand Down Expand Up @@ -101,8 +99,7 @@ func TestCopyFiles_CopyNothing(t *testing.T) {
}

commandRunCount := 0
var wrapper common.CommandWrapper
wrapper = func(ran string) (string, error) {
var wrapper common.CommandWrapper = func(ran string) (string, error) {
commandRunCount++
return "", nil
}
Expand Down
3 changes: 1 addition & 2 deletions chroot/step_mount_extra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ package chroot
import "testing"

func TestMountExtraCleanupFunc_ImplementsCleanupFunc(t *testing.T) {
var raw interface{}
raw = new(StepMountExtra)
var raw interface{} = new(StepMountExtra)
if _, ok := raw.(Cleanup); !ok {
t.Fatalf("cleanup func should be a CleanupFunc")
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/packer-sdc/internal/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package cmd

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -46,7 +45,7 @@ func (fc FileCheck) Verify(t *testing.T, dir string) {
}
}
for file, expectedContent := range fc.ExpectedContent {
content, err := ioutil.ReadFile(filepath.Join(dir, file))
content, err := os.ReadFile(filepath.Join(dir, file))
if err != nil {
t.Fatalf("ioutil.ReadFile: %v", err)
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/packer-sdc/internal/fs/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package fs
import (
"bytes"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -44,7 +43,7 @@ func SyncDir(src, dst string) error {
return errors.Wrapf(err, "cannot mkdir %s", dst)
}

entries, err := ioutil.ReadDir(src)
entries, err := os.ReadDir(src)
if err != nil {
return errors.Wrapf(err, "cannot read directory %s", dst)
}
Expand All @@ -67,7 +66,7 @@ func SyncDir(src, dst string) error {
}

// Remove files in dst that aren't in src
entries, err = ioutil.ReadDir(dst)
entries, err = os.ReadDir(dst)
if err != nil {
return errors.Wrapf(err, "cannot read directory %s", dst)
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/packer-sdc/internal/renderdocs/renderdocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"bytes"
"embed"
"flag"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -67,7 +66,7 @@ func (cmd *Command) run(args []string) error {
}

func RenderDocsFolder(folder, partials string) error {
entries, err := ioutil.ReadDir(folder)
entries, err := os.ReadDir(folder)
if err != nil {
return errors.Wrapf(err, "cannot read directory %s", folder)
}
Expand Down Expand Up @@ -129,7 +128,9 @@ var partialFiles embed.FS
// getPartial will first try to look for partials in the
// renderdocs/docs-partials dir. This makes common/shared partials available to
// all docs with for example:
// @include 'packer-plugin-sdk/communicator/Config.mdx'
//
// @include 'packer-plugin-sdk/communicator/Config.mdx'
//
// Otherwise it tries to find a partial in/ the actual filesystem.
func getPartial(partialsDir, partialPath string) ([]byte, error) {
if partial, err := partialFiles.ReadFile(strings.Join([]string{"docs-partials", partialPath}, "/")); err == nil {
Expand Down
3 changes: 1 addition & 2 deletions cmd/packer-sdc/internal/struct-markdown/struct_markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -72,7 +71,7 @@ func (cmd *Command) Run(args []string) int {
log.Fatal("Failed to guess project ROOT. If this is a Packer plugin project please make sure the root directory begins with`packer-plugin-*`")
}

b, err := ioutil.ReadFile(fname)
b, err := os.ReadFile(fname)
if err != nil {
log.Fatalf("ReadFile: %+v", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package struct_markdown

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

Expand Down Expand Up @@ -42,7 +42,7 @@ func TestCommand_Run(t *testing.T) {
}
targetedPath := strings.TrimPrefix(tt.args[0], "../test-data/packer-plugin-happycloud/")
for _, p := range tt.FileCheck.ExpectedFiles() {
raw, _ := ioutil.ReadFile(p)
raw, _ := os.ReadFile(p)
content := string(raw)
if !strings.Contains(content, targetedPath) {
t.Errorf("%s must contain '%s'. Its content is:\n%s", p, targetedPath, content)
Expand Down
3 changes: 1 addition & 2 deletions communicator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package communicator
import (
"errors"
"fmt"
"io/ioutil"
"net"
"os"
"time"
Expand Down Expand Up @@ -317,7 +316,7 @@ func (c *Config) ReadSSHPrivateKeyFile() ([]byte, error) {
return []byte{}, fmt.Errorf("Error expanding path for SSH private key: %s", err)
}

privateKey, err = ioutil.ReadFile(keyPath)
privateKey, err = os.ReadFile(keyPath)
if err != nil {
return privateKey, fmt.Errorf("Error on reading SSH private key: %s", err)
}
Expand Down
10 changes: 5 additions & 5 deletions communicator/ssh/key_pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package ssh
import (
"bytes"
"crypto"
"crypto/dsa"
"crypto/dsa" //nolint:all
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
Expand Down Expand Up @@ -60,10 +60,10 @@ type KeyPair struct {
// KeyPairFromPrivateKey returns a KeyPair loaded from an existing private key.
//
// Supported key pair types include:
// - DSA
// - ECDSA
// - ED25519
// - RSA
// - DSA
// - ECDSA
// - ED25519
// - RSA
func KeyPairFromPrivateKey(config FromPrivateKeyConfig) (KeyPair, error) {
privateKey, err := gossh.ParseRawPrivateKey(config.RawPrivateKeyPemBlock)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion communicator/ssh/key_pair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package ssh

import (
"bytes"
"crypto/dsa"
"crypto/dsa" //nolint:all
"crypto/ecdsa"
"crypto/rsa"
"fmt"
Expand Down
6 changes: 3 additions & 3 deletions communicator/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package ssh
import (
"encoding/pem"
"fmt"
"io/ioutil"
"io"
"os"
"time"

Expand All @@ -22,7 +22,7 @@ func parseKeyFile(path string) ([]byte, error) {
}
defer f.Close()

keyBytes, err := ioutil.ReadAll(f)
keyBytes, err := io.ReadAll(f)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -65,7 +65,7 @@ func ReadCertificate(certificatePath string, keySigner ssh.Signer) (ssh.Signer,
}

// Load the certificate
cert, err := ioutil.ReadFile(certificatePath)
cert, err := os.ReadFile(certificatePath)
if err != nil {
return nil, fmt.Errorf("unable to read certificate file: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion communicator/sshkey/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package sshkey

import (
"crypto/dsa"
"crypto/dsa" //nolint:all
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
Expand Down
5 changes: 2 additions & 3 deletions communicator/step_connect_ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import (
"context"
"errors"
"fmt"
"golang.org/x/term"
"io"
"log"
"net"
"os"
"strings"
"time"

"golang.org/x/crypto/ssh/terminal"

helperssh "github.com/hashicorp/packer-plugin-sdk/communicator/ssh"
"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
Expand Down Expand Up @@ -261,7 +260,7 @@ func sshBastionConfig(config *Config) (*gossh.ClientConfig, error) {

if config.SSHBastionInteractive {
var c io.ReadWriteCloser
if terminal.IsTerminal(int(os.Stdin.Fd())) {
if term.IsTerminal(int(os.Stdin.Fd())) {
c = os.Stdin
} else {
tty, err := os.Open("/dev/tty")
Expand Down
4 changes: 2 additions & 2 deletions communicator/step_debug_ssh_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package communicator
import (
"context"
"fmt"
"io/ioutil"
"os"

"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
Expand All @@ -24,7 +24,7 @@ func (s *StepDumpSSHKey) Run(_ context.Context, state multistep.StateBag) multis

ui.Message(fmt.Sprintf("Saving key for debug purposes: %s", s.Path))

err := ioutil.WriteFile(s.Path, s.SSH.SSHPrivateKey, 0700)
err := os.WriteFile(s.Path, s.SSH.SSHPrivateKey, 0700)
if err != nil {
state.Put("error", fmt.Errorf("Error saving debug key: %s", err))
return multistep.ActionHalt
Expand Down
3 changes: 1 addition & 2 deletions multistep/basic_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (
)

func TestBasicRunner_ImplRunner(t *testing.T) {
var raw interface{}
raw = &BasicRunner{}
var raw interface{} = &BasicRunner{}
if _, ok := raw.(Runner); !ok {
t.Fatalf("BasicRunner must be a Runner")
}
Expand Down
3 changes: 1 addition & 2 deletions multistep/commonsteps/step_create_cdrom.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -316,7 +315,7 @@ func (s *StepCreateCD) AddContent(dst, path, content string) error {
if err != nil {
return fmt.Errorf("error creating new directory %s: %s", dstDir, err)
}
err = ioutil.WriteFile(dstPath, []byte(content), 0666)
err = os.WriteFile(dstPath, []byte(content), 0666)
if err != nil {
return fmt.Errorf("Error writing file %s on CD: %s", path, err)
}
Expand Down
Loading

0 comments on commit 45e4613

Please sign in to comment.