Skip to content

Commit

Permalink
chore: remove refs to deprecated io/ioutil
Browse files Browse the repository at this point in the history
Signed-off-by: guoguangwu <[email protected]>
  • Loading branch information
testwill authored and lbajolet-hashicorp committed Sep 26, 2023
1 parent 324e628 commit 7eb6a45
Show file tree
Hide file tree
Showing 37 changed files with 112 additions and 135 deletions.
4 changes: 2 additions & 2 deletions acctest/plugin/component_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package plugin
import (
_ "embed"
"fmt"
"io/ioutil"
"io"
"os"
"os/exec"
"testing"
Expand Down Expand Up @@ -54,7 +54,7 @@ func TestAccInitAndBuildBasicAmazonAmiDatasource(t *testing.T) {
}
defer logs.Close()

logsBytes, err := ioutil.ReadAll(logs)
logsBytes, err := io.ReadAll(logs)
if err != nil {
return fmt.Errorf("Unable to read %s", logfile)
}
Expand Down
4 changes: 2 additions & 2 deletions acctest/plugin/plugin_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package plugin
import (
_ "embed"
"fmt"
"io/ioutil"
"io"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestAccInitAndBuildBasicAmazonEbs(t *testing.T) {
}
defer logs.Close()

logsBytes, err := ioutil.ReadAll(logs)
logsBytes, err := io.ReadAll(logs)
if err != nil {
return fmt.Errorf("Unable to read %s", logfile)
}
Expand Down
6 changes: 3 additions & 3 deletions acctest/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package acctest
import (
"context"
"fmt"
"io/ioutil"
"io"
"log"
"os"
"strings"
Expand Down Expand Up @@ -182,8 +182,8 @@ func Test(t TestT, c TestCase) {
log.Printf("[DEBUG] Running 'test' build")
ui := &packersdk.BasicUi{
Reader: os.Stdin,
Writer: ioutil.Discard,
ErrorWriter: ioutil.Discard,
Writer: io.Discard,
ErrorWriter: io.Discard,
PB: &packersdk.NoopProgressTracker{},
}
artifacts, err := build.Run(context.Background(), ui)
Expand Down
3 changes: 1 addition & 2 deletions builder/file/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -77,7 +76,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
} else {
// We're going to write Contents; if it's empty we'll just create an
// empty file.
err := ioutil.WriteFile(b.config.Target, []byte(b.config.Content), 0600)
err := os.WriteFile(b.config.Target, []byte(b.config.Content), 0600)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions builder/file/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package file

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

packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
Expand Down Expand Up @@ -33,7 +33,7 @@ func TestBuilderFileAcc_copy(t *testing.T) {
}

func checkContent(artifacts []packersdk.Artifact) error {
content, err := ioutil.ReadFile("contentTest.txt")
content, err := os.ReadFile("contentTest.txt")
if err != nil {
return err
}
Expand All @@ -45,7 +45,7 @@ func checkContent(artifacts []packersdk.Artifact) error {
}

func checkCopy(artifacts []packersdk.Artifact) error {
content, err := ioutil.ReadFile("copyTest.txt")
content, err := os.ReadFile("copyTest.txt")
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/ssh-keygen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package main

import (
"flag"
"io/ioutil"
"log"
"os"
"os/user"
Expand Down Expand Up @@ -74,12 +73,12 @@ func main() {
log.Fatalf("%s already exists.", cla.Filename)
}
log.Printf("Saving private key to %s", cla.Filename)
if err := ioutil.WriteFile(cla.Filename, keypair.Private, 0600); err != nil {
if err := os.WriteFile(cla.Filename, keypair.Private, 0600); err != nil {
log.Fatal(err)
}
publicFilename := cla.Filename + ".pub"
log.Printf("Saving public key to %s", publicFilename)
if err := ioutil.WriteFile(publicFilename, keypair.Public, 0644); err != nil {
if err := os.WriteFile(publicFilename, keypair.Public, 0644); err != nil {
log.Fatal(err)
}
}
Expand Down
5 changes: 2 additions & 3 deletions command/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package command

import (
"fmt"
"io/ioutil"
"math"
"os"
"path/filepath"
Expand Down Expand Up @@ -1009,9 +1008,9 @@ 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)
t.Fatalf("os.ReadFile: %v", err)
}
if diff := cmp.Diff(expectedContent, string(content)); diff != "" {
t.Errorf("content of %s differs: %s", file, diff)
Expand Down
4 changes: 2 additions & 2 deletions command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package command

import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand All @@ -27,7 +27,7 @@ func fatalCommand(t *testing.T, m Meta) {

func testFixtureContent(n ...string) string {
path := filepath.Join(append([]string{fixturesDir}, n...)...)
b, err := ioutil.ReadFile(path)
b, err := os.ReadFile(path)
if err != nil {
panic(err)
}
Expand Down
3 changes: 1 addition & 2 deletions command/fmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package command
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -128,7 +127,7 @@ func TestFmt_Recursive(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tempDirectory := mustString(ioutil.TempDir(testDir, "test-dir-*"))
tempDirectory := mustString(os.MkdirTemp(testDir, "test-dir-*"))
defer os.RemoveAll(tempDirectory)

createFiles(tempDirectory, tt.alreadyPresentContent)
Expand Down
5 changes: 2 additions & 3 deletions command/hcl2_upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package command

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -64,8 +63,8 @@ func Test_hcl2_upgrade(t *testing.T) {
if tc.exitEarly {
return
}
expected := string(mustBytes(ioutil.ReadFile(expectedPath)))
actual := string(mustBytes(ioutil.ReadFile(outputPath)))
expected := string(mustBytes(os.ReadFile(expectedPath)))
actual := string(mustBytes(os.ReadFile(outputPath)))

if diff := cmp.Diff(expected, actual); diff != "" {
t.Fatalf("unexpected output: %s", diff)
Expand Down
5 changes: 2 additions & 3 deletions command/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package command

import (
"io/ioutil"
"log"
"os"
"path/filepath"
Expand All @@ -23,7 +22,7 @@ func createFiles(dir string, content map[string]string) {
if err := os.MkdirAll(filepath.Dir(contentPath), 0777); err != nil {
panic(err)
}
if err := ioutil.WriteFile(contentPath, []byte(content), 0666); err != nil {
if err := os.WriteFile(contentPath, []byte(content), 0666); err != nil {
panic(err)
}
log.Printf("created tmp file: %s", contentPath)
Expand All @@ -39,7 +38,7 @@ func (c *configDirSingleton) dir(key string) string {
if v, exists := c.dirs[key]; exists {
return v
}
c.dirs[key] = mustString(ioutil.TempDir("", "pkr-test-cfg-dir-"+key))
c.dirs[key] = mustString(os.MkdirTemp("", "pkr-test-cfg-dir-"+key))
return c.dirs[key]
}

Expand Down
5 changes: 2 additions & 3 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -122,7 +121,7 @@ func TestLoadExternalComponentsFromConfig_onlyProvisioner(t *testing.T) {
func TestLoadSingleComponent(t *testing.T) {

// .exe will work everyone for testing purpose, but mostly here to help Window's test runs.
tmpFile, err := ioutil.TempFile(".", "packer-builder-*.exe")
tmpFile, err := os.CreateTemp(".", "packer-builder-*.exe")
if err != nil {
t.Fatalf("failed to create test file with error: %s", err)
}
Expand Down Expand Up @@ -160,7 +159,7 @@ func TestLoadSingleComponent(t *testing.T) {
}

func generateFakePlugins(dirname string, pluginNames []string) (string, []string, func(), error) {
dir, err := ioutil.TempDir("", dirname)
dir, err := os.MkdirTemp("", dirname)
if err != nil {
return "", nil, nil, fmt.Errorf("failed to create temporary test directory: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions datasource/http/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package http
import (
"context"
"fmt"
"io/ioutil"
"io"
"mime"
"net/http"
"regexp"
Expand Down Expand Up @@ -137,7 +137,7 @@ func (d *Datasource) Execute() (cty.Value, error) {
fmt.Println("If the content is binary data, Packer may not properly handle the contents of the response.")
}

bytes, err := ioutil.ReadAll(resp.Body)
bytes, err := io.ReadAll(resp.Body)
// TODO: How to make test case for this?
if err != nil {
fmt.Println("Error processing response body of call")
Expand Down
4 changes: 2 additions & 2 deletions datasource/http/data_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package http
import (
_ "embed"
"fmt"
"io/ioutil"
"io"
"os"
"os/exec"
"regexp"
Expand Down Expand Up @@ -84,7 +84,7 @@ func TestHttpDataSource(t *testing.T) {
}
defer logs.Close()

logsBytes, err := ioutil.ReadAll(logs)
logsBytes, err := io.ReadAll(logs)
if err != nil {
return fmt.Errorf("Unable to read %s", logfile)
}
Expand Down
11 changes: 5 additions & 6 deletions hcl2template/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -76,7 +75,7 @@ func (f *HCL2Formatter) Format(path string) (int, hcl.Diagnostics) {
return f.formatFile(path, diags, bytesModified)
}

fileInfos, err := ioutil.ReadDir(path)
fileInfos, err := os.ReadDir(path)
if err != nil {
diag := &hcl.Diagnostic{
Severity: hcl.DiagError,
Expand Down Expand Up @@ -129,7 +128,7 @@ func (f *HCL2Formatter) processFile(filename string) ([]byte, error) {
}
}

inSrc, err := ioutil.ReadAll(in)
inSrc, err := io.ReadAll(in)
if err != nil {
return nil, fmt.Errorf("failed to read %s: %s", filename, err)
}
Expand Down Expand Up @@ -158,7 +157,7 @@ func (f *HCL2Formatter) processFile(filename string) ([]byte, error) {
if filename == "-" {
_, _ = f.Output.Write(outSrc)
} else {
if err := ioutil.WriteFile(filename, outSrc, 0644); err != nil {
if err := os.WriteFile(filename, outSrc, 0644); err != nil {
return nil, err
}
}
Expand All @@ -178,14 +177,14 @@ func (f *HCL2Formatter) processFile(filename string) ([]byte, error) {
// bytesDiff returns the unified diff of b1 and b2
// Shamelessly copied from Terraform's fmt command.
func bytesDiff(b1, b2 []byte, path string) (data []byte, err error) {
f1, err := ioutil.TempFile("", "")
f1, err := os.CreateTemp("", "")
if err != nil {
return
}
defer os.Remove(f1.Name())
defer f1.Close()

f2, err := ioutil.TempFile("", "")
f2, err := os.CreateTemp("", "")
if err != nil {
return
}
Expand Down
9 changes: 4 additions & 5 deletions hcl2template/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package hcl2template

import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"strings"
Expand Down Expand Up @@ -48,12 +47,12 @@ func TestHCL2Formatter_Format_Write(t *testing.T) {
f.Output = &buf
f.Write = true

unformattedData, err := ioutil.ReadFile("testdata/format/unformatted.pkr.hcl")
unformattedData, err := os.ReadFile("testdata/format/unformatted.pkr.hcl")
if err != nil {
t.Fatalf("failed to open the unformatted fixture %s", err)
}

tf, err := ioutil.TempFile("", "*.pkr.hcl")
tf, err := os.CreateTemp("", "*.pkr.hcl")
if err != nil {
t.Fatalf("failed to create tempfile for test %s", err)
}
Expand All @@ -68,12 +67,12 @@ func TestHCL2Formatter_Format_Write(t *testing.T) {
}

//lets re-read the tempfile which should now be formatted
data, err := ioutil.ReadFile(tf.Name())
data, err := os.ReadFile(tf.Name())
if err != nil {
t.Fatalf("failed to open the newly formatted fixture %s", err)
}

formattedData, err := ioutil.ReadFile("testdata/format/formatted.pkr.hcl")
formattedData, err := os.ReadFile("testdata/format/formatted.pkr.hcl")
if err != nil {
t.Fatalf("failed to open the formatted fixture %s", err)
}
Expand Down
3 changes: 1 addition & 2 deletions hcl2template/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package hcl2template

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -74,7 +73,7 @@ func GetHCL2Files(filename, hclSuffix, jsonSuffix string) (hclFiles, jsonFiles [
return nil, nil, diags
}

fileInfos, err := ioutil.ReadDir(filename)
fileInfos, err := os.ReadDir(filename)
if err != nil {
diag := &hcl.Diagnostic{
Severity: hcl.DiagError,
Expand Down
Loading

0 comments on commit 7eb6a45

Please sign in to comment.