From 7eb6a45cd483af4236645f75ca83623aa127f929 Mon Sep 17 00:00:00 2001 From: guoguangwu Date: Fri, 22 Sep 2023 15:44:03 +0800 Subject: [PATCH] chore: remove refs to deprecated io/ioutil Signed-off-by: guoguangwu --- acctest/plugin/component_acc_test.go | 4 +-- acctest/plugin/plugin_acc_test.go | 4 +-- acctest/testing.go | 6 ++-- builder/file/builder.go | 3 +- builder/file/builder_test.go | 6 ++-- cmd/ssh-keygen/main.go | 5 ++-- command/build_test.go | 5 ++-- command/command_test.go | 4 +-- command/fmt_test.go | 3 +- command/hcl2_upgrade_test.go | 5 ++-- command/utils_test.go | 5 ++-- config_test.go | 5 ++-- datasource/http/data.go | 4 +-- datasource/http/data_acc_test.go | 4 +-- hcl2template/formatter.go | 11 ++++--- hcl2template/formatter_test.go | 9 +++--- hcl2template/utils.go | 3 +- main.go | 5 ++-- packer/client_test.go | 3 +- packer/plugin-getter/github/getter.go | 5 ++-- packer/plugin-getter/plugins.go | 3 +- packer/plugin-getter/plugins_test.go | 5 ++-- packer/plugin_client.go | 3 +- packer/progressbar_test.go | 10 +++---- packer/testing.go | 6 ++-- .../checksum/post-processor_test.go | 4 +-- .../compress/post-processor_test.go | 18 ++++++------ post-processor/manifest/post-processor.go | 5 ++-- .../shell-local/post-processor_test.go | 9 +++--- provisioner/file/provisioner_test.go | 29 +++++++++---------- .../powershell/provisioner_acc_test.go | 4 +-- provisioner/powershell/provisioner_test.go | 17 +++++------ .../shell-local/provisioner_acc_test.go | 4 +-- provisioner/shell/provisioner_acc_test.go | 4 +-- provisioner/shell/provisioner_test.go | 9 +++--- provisioner/windows-shell/provisioner_test.go | 15 +++++----- scripts/generate-plugins.go | 3 +- 37 files changed, 112 insertions(+), 135 deletions(-) diff --git a/acctest/plugin/component_acc_test.go b/acctest/plugin/component_acc_test.go index 110d8d3765a..e78c77284c0 100644 --- a/acctest/plugin/component_acc_test.go +++ b/acctest/plugin/component_acc_test.go @@ -8,7 +8,7 @@ package plugin import ( _ "embed" "fmt" - "io/ioutil" + "io" "os" "os/exec" "testing" @@ -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) } diff --git a/acctest/plugin/plugin_acc_test.go b/acctest/plugin/plugin_acc_test.go index 137fb394db4..6549b2129a3 100644 --- a/acctest/plugin/plugin_acc_test.go +++ b/acctest/plugin/plugin_acc_test.go @@ -8,7 +8,7 @@ package plugin import ( _ "embed" "fmt" - "io/ioutil" + "io" "os" "os/exec" "path/filepath" @@ -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) } diff --git a/acctest/testing.go b/acctest/testing.go index dbd877fb3d3..dd18be1a365 100644 --- a/acctest/testing.go +++ b/acctest/testing.go @@ -6,7 +6,7 @@ package acctest import ( "context" "fmt" - "io/ioutil" + "io" "log" "os" "strings" @@ -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) diff --git a/builder/file/builder.go b/builder/file/builder.go index e9d9d8f9ebd..0f79c958fe8 100644 --- a/builder/file/builder.go +++ b/builder/file/builder.go @@ -12,7 +12,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "os" "path/filepath" @@ -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 } diff --git a/builder/file/builder_test.go b/builder/file/builder_test.go index 80fc326bdf4..5248318a4dc 100644 --- a/builder/file/builder_test.go +++ b/builder/file/builder_test.go @@ -5,7 +5,7 @@ package file import ( "fmt" - "io/ioutil" + "os" "testing" packersdk "github.com/hashicorp/packer-plugin-sdk/packer" @@ -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 } @@ -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 } diff --git a/cmd/ssh-keygen/main.go b/cmd/ssh-keygen/main.go index f47eca5791f..c29edf76e7e 100644 --- a/cmd/ssh-keygen/main.go +++ b/cmd/ssh-keygen/main.go @@ -5,7 +5,6 @@ package main import ( "flag" - "io/ioutil" "log" "os" "os/user" @@ -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) } } diff --git a/command/build_test.go b/command/build_test.go index 00685e98dc7..de476fdb0c4 100644 --- a/command/build_test.go +++ b/command/build_test.go @@ -5,7 +5,6 @@ package command import ( "fmt" - "io/ioutil" "math" "os" "path/filepath" @@ -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) diff --git a/command/command_test.go b/command/command_test.go index fcbec5fb0a7..712e9300496 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -5,7 +5,7 @@ package command import ( "bytes" - "io/ioutil" + "os" "path/filepath" "testing" @@ -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) } diff --git a/command/fmt_test.go b/command/fmt_test.go index 8fd058b379b..7d9f3b11979 100644 --- a/command/fmt_test.go +++ b/command/fmt_test.go @@ -6,7 +6,6 @@ package command import ( "bytes" "fmt" - "io/ioutil" "os" "path/filepath" "strings" @@ -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) diff --git a/command/hcl2_upgrade_test.go b/command/hcl2_upgrade_test.go index f0ffc7676f2..487d99999cb 100644 --- a/command/hcl2_upgrade_test.go +++ b/command/hcl2_upgrade_test.go @@ -4,7 +4,6 @@ package command import ( - "io/ioutil" "os" "path/filepath" "testing" @@ -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) diff --git a/command/utils_test.go b/command/utils_test.go index 6af1f2c4881..cf5e2dbe3e7 100644 --- a/command/utils_test.go +++ b/command/utils_test.go @@ -4,7 +4,6 @@ package command import ( - "io/ioutil" "log" "os" "path/filepath" @@ -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) @@ -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] } diff --git a/config_test.go b/config_test.go index 1a583a8c3a7..ac101010c3c 100644 --- a/config_test.go +++ b/config_test.go @@ -6,7 +6,6 @@ package main import ( "encoding/json" "fmt" - "io/ioutil" "os" "path/filepath" "reflect" @@ -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) } @@ -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) } diff --git a/datasource/http/data.go b/datasource/http/data.go index ef77adca2ba..58dec04c35b 100644 --- a/datasource/http/data.go +++ b/datasource/http/data.go @@ -8,7 +8,7 @@ package http import ( "context" "fmt" - "io/ioutil" + "io" "mime" "net/http" "regexp" @@ -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") diff --git a/datasource/http/data_acc_test.go b/datasource/http/data_acc_test.go index c5a92718e67..fdf680b8298 100644 --- a/datasource/http/data_acc_test.go +++ b/datasource/http/data_acc_test.go @@ -6,7 +6,7 @@ package http import ( _ "embed" "fmt" - "io/ioutil" + "io" "os" "os/exec" "regexp" @@ -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) } diff --git a/hcl2template/formatter.go b/hcl2template/formatter.go index 5505cde4052..c04bb114748 100644 --- a/hcl2template/formatter.go +++ b/hcl2template/formatter.go @@ -7,7 +7,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -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, @@ -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) } @@ -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 } } @@ -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 } diff --git a/hcl2template/formatter_test.go b/hcl2template/formatter_test.go index 07febb6f439..8cb909292a9 100644 --- a/hcl2template/formatter_test.go +++ b/hcl2template/formatter_test.go @@ -5,7 +5,6 @@ package hcl2template import ( "bytes" - "io/ioutil" "os" "os/exec" "strings" @@ -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) } @@ -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) } diff --git a/hcl2template/utils.go b/hcl2template/utils.go index dc2399a1034..98546a95456 100644 --- a/hcl2template/utils.go +++ b/hcl2template/utils.go @@ -5,7 +5,6 @@ package hcl2template import ( "fmt" - "io/ioutil" "os" "path/filepath" "strings" @@ -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, diff --git a/main.go b/main.go index 834d040409a..fe902f5bdb3 100644 --- a/main.go +++ b/main.go @@ -9,7 +9,6 @@ package main import ( "fmt" "io" - "io/ioutil" "log" "math/rand" "os" @@ -65,13 +64,13 @@ func realMain() int { return 1 } if logWriter == nil { - logWriter = ioutil.Discard + logWriter = io.Discard } packersdk.LogSecretFilter.SetOutput(logWriter) // Disable logging here - log.SetOutput(ioutil.Discard) + log.SetOutput(io.Discard) // We always send logs to a temporary file that we use in case // there is a panic. Otherwise, we delete it. diff --git a/packer/client_test.go b/packer/client_test.go index f0dc074e997..c914a3edaed 100644 --- a/packer/client_test.go +++ b/packer/client_test.go @@ -5,7 +5,6 @@ package packer import ( "bytes" - "io/ioutil" "os" "strings" "testing" @@ -102,7 +101,7 @@ func TestClient_Stderr(t *testing.T) { func TestClient_Stdin(t *testing.T) { // Overwrite stdin for this test with a temporary file - tf, err := ioutil.TempFile("", "packer") + tf, err := os.CreateTemp("", "packer") if err != nil { t.Fatalf("err: %s", err) } diff --git a/packer/plugin-getter/github/getter.go b/packer/plugin-getter/github/getter.go index bde61e51641..a4942a42163 100644 --- a/packer/plugin-getter/github/getter.go +++ b/packer/plugin-getter/github/getter.go @@ -11,7 +11,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "log" "net/http" "os" @@ -71,7 +70,7 @@ func transformChecksumStream() func(in io.ReadCloser) (io.ReadCloser, error) { } } _, _ = buffer.WriteString("]") - return ioutil.NopCloser(buffer), nil + return io.NopCloser(buffer), nil } } @@ -103,7 +102,7 @@ func transformVersionStream(in io.ReadCloser) (io.ReadCloser, error) { return nil, err } - return ioutil.NopCloser(buf), nil + return io.NopCloser(buf), nil } // HostSpecificTokenAuthTransport makes sure the http roundtripper only sets an diff --git a/packer/plugin-getter/plugins.go b/packer/plugin-getter/plugins.go index 92f7e28a8e7..f6ca5c4abfe 100644 --- a/packer/plugin-getter/plugins.go +++ b/packer/plugin-getter/plugins.go @@ -9,7 +9,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "log" "os" "path/filepath" @@ -714,7 +713,7 @@ func (pr *Requirement) InstallLatest(opts InstallOptions) (*Installation, error) log.Printf("[WARNING] %v, ignoring", err) } - if err := ioutil.WriteFile(outputFileName+checksum.Checksummer.FileExt(), []byte(hex.EncodeToString(cs)), 0555); err != nil { + if err := os.WriteFile(outputFileName+checksum.Checksummer.FileExt(), []byte(hex.EncodeToString(cs)), 0555); err != nil { err := fmt.Errorf("failed to write local binary checksum file: %s", err) errs = multierror.Append(errs, err) log.Printf("[WARNING] %v, ignoring", err) diff --git a/packer/plugin-getter/plugins_test.go b/packer/plugin-getter/plugins_test.go index 7a1439de210..dfb28767067 100644 --- a/packer/plugin-getter/plugins_test.go +++ b/packer/plugin-getter/plugins_test.go @@ -10,7 +10,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "log" "os" "path/filepath" @@ -754,7 +753,7 @@ func (g *mockPluginGetter) Get(what string, options GetOptions) (io.ReadCloser, panic(err) } }() - return ioutil.NopCloser(read), nil + return io.NopCloser(read), nil } func zipFile(content map[string]string) io.ReadCloser { @@ -778,7 +777,7 @@ func zipFile(content map[string]string) io.ReadCloser { if err != nil { panic(err) } - return ioutil.NopCloser(buff) + return io.NopCloser(buff) } var _ Getter = &mockPluginGetter{} diff --git a/packer/plugin_client.go b/packer/plugin_client.go index b788f68f7fb..01e899e2813 100644 --- a/packer/plugin_client.go +++ b/packer/plugin_client.go @@ -8,7 +8,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "log" "net" "os" @@ -114,7 +113,7 @@ func NewClient(config *PluginClientConfig) (c *PluginClient) { } if config.Stderr == nil { - config.Stderr = ioutil.Discard + config.Stderr = io.Discard } c = &PluginClient{config: config} diff --git a/packer/progressbar_test.go b/packer/progressbar_test.go index 96ff65d687b..20edc07c9b2 100644 --- a/packer/progressbar_test.go +++ b/packer/progressbar_test.go @@ -5,7 +5,7 @@ package packer import ( "bytes" - "io/ioutil" + "io" "testing" "golang.org/x/sync/errgroup" @@ -16,10 +16,10 @@ import ( func TestProgressTracking_open_close(t *testing.T) { var bar *UiProgressBar - tracker := bar.TrackProgress("1,", 1, 42, ioutil.NopCloser(nil)) + tracker := bar.TrackProgress("1,", 1, 42, io.NopCloser(nil)) tracker.Close() - tracker = bar.TrackProgress("2,", 1, 42, ioutil.NopCloser(nil)) + tracker = bar.TrackProgress("2,", 1, 42, io.NopCloser(nil)) tracker.Close() } @@ -29,7 +29,7 @@ func TestProgressTracking_multi_open_close(t *testing.T) { for i := 0; i < 100; i++ { g.Go(func() error { - tracker := bar.TrackProgress("file,", 1, 42, ioutil.NopCloser(nil)) + tracker := bar.TrackProgress("file,", 1, 42, io.NopCloser(nil)) return tracker.Close() }) } @@ -46,7 +46,7 @@ func TestProgressTracking_races(t *testing.T) { g.Go(func() error { txt := []byte("foobarbaz dolores") b := bytes.NewReader(txt) - tracker := bar.TrackProgress("file,", 1, 42, ioutil.NopCloser(b)) + tracker := bar.TrackProgress("file,", 1, 42, io.NopCloser(b)) for i := 0; i < 42; i++ { tracker.Read([]byte("i")) diff --git a/packer/testing.go b/packer/testing.go index faa3c58283e..b0ebc04b95f 100644 --- a/packer/testing.go +++ b/packer/testing.go @@ -5,7 +5,7 @@ package packer import ( "bytes" - "io/ioutil" + "io" "testing" packersdk "github.com/hashicorp/packer-plugin-sdk/packer" @@ -40,8 +40,8 @@ func TestUi(t *testing.T) packersdk.Ui { var buf bytes.Buffer return &packersdk.BasicUi{ Reader: &buf, - Writer: ioutil.Discard, - ErrorWriter: ioutil.Discard, + Writer: io.Discard, + ErrorWriter: io.Discard, } } diff --git a/post-processor/checksum/post-processor_test.go b/post-processor/checksum/post-processor_test.go index 62c2f2ae3e1..6a08f029735 100644 --- a/post-processor/checksum/post-processor_test.go +++ b/post-processor/checksum/post-processor_test.go @@ -7,7 +7,7 @@ import ( "bytes" "context" "fmt" - "io/ioutil" + "io" "os" "strings" "testing" @@ -38,7 +38,7 @@ func TestChecksumSHA1(t *testing.T) { if err != nil { t.Errorf("Unable to read checksum file: %s", err) } - if buf, _ := ioutil.ReadAll(f); !bytes.Equal(buf, []byte("d3486ae9136e7856bc42212385ea797094475802\tpackage.txt\n")) { + if buf, _ := io.ReadAll(f); !bytes.Equal(buf, []byte("d3486ae9136e7856bc42212385ea797094475802\tpackage.txt\n")) { t.Errorf("Failed to compute checksum: %s\n%s", buf, "d3486ae9136e7856bc42212385ea797094475802 package.txt") } diff --git a/post-processor/compress/post-processor_test.go b/post-processor/compress/post-processor_test.go index e85b2041062..885388c79e8 100644 --- a/post-processor/compress/post-processor_test.go +++ b/post-processor/compress/post-processor_test.go @@ -9,7 +9,7 @@ import ( "compress/gzip" "context" "fmt" - "io/ioutil" + "io" "os" "strings" "testing" @@ -84,7 +84,7 @@ func TestCompressOptions(t *testing.T) { filename := "package.gz" archive, _ := os.Open(filename) gzipReader, _ := gzip.NewReader(archive) - data, _ := ioutil.ReadAll(gzipReader) + data, _ := io.ReadAll(gzipReader) if string(data) != expectedFileContents { t.Errorf("Expected:\n%s\nFound:\n%s\n", expectedFileContents, data) @@ -115,7 +115,7 @@ func TestCompressInterpolation(t *testing.T) { } gzipReader, _ := gzip.NewReader(archive) - data, _ := ioutil.ReadAll(gzipReader) + data, _ := io.ReadAll(gzipReader) if string(data) != expectedFileContents { t.Errorf("Expected:\n%s\nFound:\n%s\n", expectedFileContents, data) @@ -194,7 +194,7 @@ func TestArchive(t *testing.T) { if err != nil { return nil, err } - return ioutil.ReadAll(bzipReader) + return io.ReadAll(bzipReader) }, "zip": func(archive *os.File) ([]byte, error) { fi, _ := archive.Stat() @@ -206,7 +206,7 @@ func TestArchive(t *testing.T) { if err != nil { return nil, err } - return ioutil.ReadAll(ctt) + return io.ReadAll(ctt) }, "tar": func(archive *os.File) ([]byte, error) { tarReader := tar.NewReader(archive) @@ -214,7 +214,7 @@ func TestArchive(t *testing.T) { if err != nil { return nil, err } - return ioutil.ReadAll(tarReader) + return io.ReadAll(tarReader) }, "tar.gz": func(archive *os.File) ([]byte, error) { gzipReader, err := gzip.NewReader(archive) @@ -226,15 +226,15 @@ func TestArchive(t *testing.T) { if err != nil { return nil, err } - return ioutil.ReadAll(tarReader) + return io.ReadAll(tarReader) }, "gz": func(archive *os.File) ([]byte, error) { gzipReader, _ := gzip.NewReader(archive) - return ioutil.ReadAll(gzipReader) + return io.ReadAll(gzipReader) }, "lz4": func(archive *os.File) ([]byte, error) { lz4Reader := lz4.NewReader(archive) - return ioutil.ReadAll(lz4Reader) + return io.ReadAll(lz4Reader) }, } diff --git a/post-processor/manifest/post-processor.go b/post-processor/manifest/post-processor.go index 5fb90c45577..74f203525d9 100644 --- a/post-processor/manifest/post-processor.go +++ b/post-processor/manifest/post-processor.go @@ -10,7 +10,6 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" "log" "os" "path/filepath" @@ -143,7 +142,7 @@ func (p *PostProcessor) PostProcess(ctx context.Context, ui packersdk.Ui, source // Read the current manifest file from disk contents := []byte{} - if contents, err = ioutil.ReadFile(p.config.OutputPath); err != nil && !os.IsNotExist(err) { + if contents, err = os.ReadFile(p.config.OutputPath); err != nil && !os.IsNotExist(err) { return source, true, true, fmt.Errorf("Unable to open %s for reading: %s", p.config.OutputPath, err) } @@ -167,7 +166,7 @@ func (p *PostProcessor) PostProcess(ctx context.Context, ui packersdk.Ui, source // Write JSON to disk if out, err := json.MarshalIndent(manifestFile, "", " "); err == nil { - if err = ioutil.WriteFile(p.config.OutputPath, out, 0664); err != nil { + if err = os.WriteFile(p.config.OutputPath, out, 0664); err != nil { return source, true, true, fmt.Errorf("Unable to write %s: %s", p.config.OutputPath, err) } } else { diff --git a/post-processor/shell-local/post-processor_test.go b/post-processor/shell-local/post-processor_test.go index 39fbec25ec3..13121658af2 100644 --- a/post-processor/shell-local/post-processor_test.go +++ b/post-processor/shell-local/post-processor_test.go @@ -4,7 +4,6 @@ package shell_local import ( - "io/ioutil" "os" "runtime" "testing" @@ -95,7 +94,7 @@ func TestPostProcessorPrepare_Script(t *testing.T) { } // Test with a good one - tf, err := ioutil.TempFile("", "packer") + tf, err := os.CreateTemp("", "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -163,7 +162,7 @@ func TestPostProcessorPrepare_ScriptAndInline(t *testing.T) { } // Test with both - tf, err := ioutil.TempFile("", "packer") + tf, err := os.CreateTemp("", "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -182,7 +181,7 @@ func TestPostProcessorPrepare_ScriptAndScripts(t *testing.T) { raws := testConfig() // Test with both - tf, err := ioutil.TempFile("", "packer") + tf, err := os.CreateTemp("", "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -208,7 +207,7 @@ func TestPostProcessorPrepare_Scripts(t *testing.T) { } // Test with a good one - tf, err := ioutil.TempFile("", "packer") + tf, err := os.CreateTemp("", "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } diff --git a/provisioner/file/provisioner_test.go b/provisioner/file/provisioner_test.go index 17d55d75d2b..280afa0250a 100644 --- a/provisioner/file/provisioner_test.go +++ b/provisioner/file/provisioner_test.go @@ -6,7 +6,6 @@ package file import ( "bytes" "context" - "io/ioutil" "os" "path/filepath" "regexp" @@ -62,7 +61,7 @@ func TestProvisionerPrepare_InvalidSource(t *testing.T) { func TestProvisionerPrepare_ValidSource(t *testing.T) { var p Provisioner - tf, err := ioutil.TempFile("", "packer") + tf, err := os.CreateTemp("", "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -107,7 +106,7 @@ func TestProvisionerPrepare_EmptyDestination(t *testing.T) { func TestProvisionerProvision_SendsFile(t *testing.T) { var p Provisioner - tf, err := ioutil.TempFile("", "packer") + tf, err := os.CreateTemp("", "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -195,7 +194,7 @@ func TestProvisionerProvision_SendsContent(t *testing.T) { func TestProvisionerProvision_SendsFileMultipleFiles(t *testing.T) { var p Provisioner - tf1, err := ioutil.TempFile("", "packer") + tf1, err := os.CreateTemp("", "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -205,7 +204,7 @@ func TestProvisionerProvision_SendsFileMultipleFiles(t *testing.T) { t.Fatalf("error writing tempfile: %s", err) } - tf2, err := ioutil.TempFile("", "packer") + tf2, err := os.CreateTemp("", "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -248,13 +247,13 @@ func TestProvisionerProvision_SendsFileMultipleDirs(t *testing.T) { var p Provisioner // Prepare the first directory - td1, err := ioutil.TempDir("", "packerdir") + td1, err := os.MkdirTemp("", "packerdir") if err != nil { t.Fatalf("error temp folder 1: %s", err) } defer os.Remove(td1) - tf1, err := ioutil.TempFile(td1, "packer") + tf1, err := os.CreateTemp(td1, "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -264,13 +263,13 @@ func TestProvisionerProvision_SendsFileMultipleDirs(t *testing.T) { } // Prepare the second directory - td2, err := ioutil.TempDir("", "packerdir") + td2, err := os.MkdirTemp("", "packerdir") if err != nil { t.Fatalf("error temp folder 1: %s", err) } defer os.Remove(td2) - tf2, err := ioutil.TempFile(td2, "packer") + tf2, err := os.CreateTemp(td2, "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -317,7 +316,7 @@ func TestProvisionerProvision_SendsFileMultipleDirs(t *testing.T) { func TestProvisionerProvision_DownloadsMultipleFilesToFolder(t *testing.T) { var p Provisioner - tf1, err := ioutil.TempFile("", "packer") + tf1, err := os.CreateTemp("", "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -327,7 +326,7 @@ func TestProvisionerProvision_DownloadsMultipleFilesToFolder(t *testing.T) { t.Fatalf("error writing tempfile: %s", err) } - tf2, err := ioutil.TempFile("", "packer") + tf2, err := os.CreateTemp("", "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -388,7 +387,7 @@ func TestProvisionerProvision_DownloadsMultipleFilesToFolder(t *testing.T) { func TestProvisionerProvision_SendsFileMultipleFilesToFolder(t *testing.T) { var p Provisioner - tf1, err := ioutil.TempFile("", "packer") + tf1, err := os.CreateTemp("", "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -398,7 +397,7 @@ func TestProvisionerProvision_SendsFileMultipleFilesToFolder(t *testing.T) { t.Fatalf("error writing tempfile: %s", err) } - tf2, err := ioutil.TempFile("", "packer") + tf2, err := os.CreateTemp("", "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -454,12 +453,12 @@ func TestProvisionDownloadMkdirAll(t *testing.T) { {"path/to/dir"}, {"path/to/dir/"}, } - tmpDir, err := ioutil.TempDir("", "packer-file") + tmpDir, err := os.MkdirTemp("", "packer-file") if err != nil { t.Fatalf("error tempdir: %s", err) } defer os.RemoveAll(tmpDir) - tf, err := ioutil.TempFile(tmpDir, "packer") + tf, err := os.CreateTemp(tmpDir, "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } diff --git a/provisioner/powershell/provisioner_acc_test.go b/provisioner/powershell/provisioner_acc_test.go index 8340b2a5a87..8073d011334 100644 --- a/provisioner/powershell/provisioner_acc_test.go +++ b/provisioner/powershell/provisioner_acc_test.go @@ -5,7 +5,7 @@ package powershell_test import ( "fmt" - "io/ioutil" + "io" "os" "os/exec" "path/filepath" @@ -35,7 +35,7 @@ func LoadProvisionerFragment(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) } diff --git a/provisioner/powershell/provisioner_test.go b/provisioner/powershell/provisioner_test.go index 0d8b4ca35cd..024b0585fa2 100644 --- a/provisioner/powershell/provisioner_test.go +++ b/provisioner/powershell/provisioner_test.go @@ -7,7 +7,6 @@ import ( "bytes" "context" "fmt" - "io/ioutil" "os" "regexp" "strings" @@ -35,7 +34,7 @@ func TestProvisionerPrepare_extractScript(t *testing.T) { } // File contents should contain 2 lines concatenated by newlines: foo\nbar - readFile, err := ioutil.ReadFile(file) + readFile, err := os.ReadFile(file) expectedContents := "foo\nbar\n" if err != nil { t.Fatalf("Should not be error: %s", err) @@ -187,7 +186,7 @@ func TestProvisionerPrepare_Script(t *testing.T) { } // Test with a good one - tf, err := ioutil.TempFile("", "packer") + tf, err := os.CreateTemp("", "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -214,7 +213,7 @@ func TestProvisionerPrepare_ScriptAndInline(t *testing.T) { } // Test with both - tf, err := ioutil.TempFile("", "packer") + tf, err := os.CreateTemp("", "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -234,7 +233,7 @@ func TestProvisionerPrepare_ScriptAndScripts(t *testing.T) { config := testConfig() // Test with both - tf, err := ioutil.TempFile("", "packer") + tf, err := os.CreateTemp("", "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -261,7 +260,7 @@ func TestProvisionerPrepare_Scripts(t *testing.T) { } // Test with a good one - tf, err := ioutil.TempFile("", "packer") + tf, err := os.CreateTemp("", "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -512,7 +511,7 @@ func TestProvisionerProvision_Inline(t *testing.T) { } func TestProvisionerProvision_Scripts(t *testing.T) { - tempFile, _ := ioutil.TempFile("", "packer") + tempFile, _ := os.CreateTemp("", "packer") defer os.Remove(tempFile.Name()) defer tempFile.Close() @@ -542,7 +541,7 @@ func TestProvisionerProvision_Scripts(t *testing.T) { } func TestProvisionerProvision_ScriptsWithEnvVars(t *testing.T) { - tempFile, _ := ioutil.TempFile("", "packer") + tempFile, _ := os.CreateTemp("", "packer") ui := testUi() defer os.Remove(tempFile.Name()) defer tempFile.Close() @@ -579,7 +578,7 @@ func TestProvisionerProvision_ScriptsWithEnvVars(t *testing.T) { } func TestProvisionerProvision_SkipClean(t *testing.T) { - tempFile, _ := ioutil.TempFile("", "packer") + tempFile, _ := os.CreateTemp("", "packer") defer func() { tempFile.Close() os.Remove(tempFile.Name()) diff --git a/provisioner/shell-local/provisioner_acc_test.go b/provisioner/shell-local/provisioner_acc_test.go index b7b7c051a14..1f1b37a0cb8 100644 --- a/provisioner/shell-local/provisioner_acc_test.go +++ b/provisioner/shell-local/provisioner_acc_test.go @@ -5,7 +5,7 @@ package shell_test import ( "fmt" - "io/ioutil" + "io" "os" "os/exec" "path/filepath" @@ -31,7 +31,7 @@ func loadFile(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) } diff --git a/provisioner/shell/provisioner_acc_test.go b/provisioner/shell/provisioner_acc_test.go index 4eefa19e78a..167456d274a 100644 --- a/provisioner/shell/provisioner_acc_test.go +++ b/provisioner/shell/provisioner_acc_test.go @@ -5,7 +5,7 @@ package shell_test import ( "fmt" - "io/ioutil" + "io" "os" "os/exec" "path/filepath" @@ -31,7 +31,7 @@ func loadFile(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) } diff --git a/provisioner/shell/provisioner_test.go b/provisioner/shell/provisioner_test.go index 5640c92425e..40da9b5ea2b 100644 --- a/provisioner/shell/provisioner_test.go +++ b/provisioner/shell/provisioner_test.go @@ -4,7 +4,6 @@ package shell import ( - "io/ioutil" "os" "regexp" "strings" @@ -123,7 +122,7 @@ func TestProvisionerPrepare_Script(t *testing.T) { } // Test with a good one - tf, err := ioutil.TempFile("", "packer") + tf, err := os.CreateTemp("", "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -149,7 +148,7 @@ func TestProvisionerPrepare_ScriptAndInline(t *testing.T) { } // Test with both - tf, err := ioutil.TempFile("", "packer") + tf, err := os.CreateTemp("", "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -168,7 +167,7 @@ func TestProvisionerPrepare_ScriptAndScripts(t *testing.T) { config := testConfig() // Test with both - tf, err := ioutil.TempFile("", "packer") + tf, err := os.CreateTemp("", "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -194,7 +193,7 @@ func TestProvisionerPrepare_Scripts(t *testing.T) { } // Test with a good one - tf, err := ioutil.TempFile("", "packer") + tf, err := os.CreateTemp("", "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } diff --git a/provisioner/windows-shell/provisioner_test.go b/provisioner/windows-shell/provisioner_test.go index 5ef92c64ac0..08990b00ae7 100644 --- a/provisioner/windows-shell/provisioner_test.go +++ b/provisioner/windows-shell/provisioner_test.go @@ -6,7 +6,6 @@ package shell import ( "bytes" "context" - "io/ioutil" "log" "os" "strings" @@ -37,7 +36,7 @@ func TestProvisionerPrepare_extractScript(t *testing.T) { } // File contents should contain 2 lines concatenated by newlines: foo\nbar - readFile, err := ioutil.ReadFile(file) + readFile, err := os.ReadFile(file) if err != nil { t.Fatalf("Should not be error: %s", err) } @@ -102,7 +101,7 @@ func TestProvisionerPrepare_Script(t *testing.T) { } // Test with a good one - tf, err := ioutil.TempFile("", "packer") + tf, err := os.CreateTemp("", "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -129,7 +128,7 @@ func TestProvisionerPrepare_ScriptAndInline(t *testing.T) { } // Test with both - tf, err := ioutil.TempFile("", "packer") + tf, err := os.CreateTemp("", "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -149,7 +148,7 @@ func TestProvisionerPrepare_ScriptAndScripts(t *testing.T) { config := testConfig() // Test with both - tf, err := ioutil.TempFile("", "packer") + tf, err := os.CreateTemp("", "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -176,7 +175,7 @@ func TestProvisionerPrepare_Scripts(t *testing.T) { } // Test with a good one - tf, err := ioutil.TempFile("", "packer") + tf, err := os.CreateTemp("", "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -325,7 +324,7 @@ func TestProvisionerProvision_Inline(t *testing.T) { } func TestProvisionerProvision_Scripts(t *testing.T) { - tf, err := ioutil.TempFile("", "packer") + tf, err := os.CreateTemp("", "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } @@ -357,7 +356,7 @@ func TestProvisionerProvision_Scripts(t *testing.T) { } func TestProvisionerProvision_ScriptsWithEnvVars(t *testing.T) { - tf, err := ioutil.TempFile("", "packer") + tf, err := os.CreateTemp("", "packer") if err != nil { t.Fatalf("error tempfile: %s", err) } diff --git a/scripts/generate-plugins.go b/scripts/generate-plugins.go index bd303cb29ba..7390fafa070 100755 --- a/scripts/generate-plugins.go +++ b/scripts/generate-plugins.go @@ -12,7 +12,6 @@ import ( "go/ast" "go/parser" "go/token" - "io/ioutil" "log" "os" "path/filepath" @@ -142,7 +141,7 @@ func makeImports(builders, provisioners, postProcessors, Datasources []plugin) s // listDirectories recursively lists directories under the specified path func listDirectories(path string) ([]string, error) { names := []string{} - items, err := ioutil.ReadDir(path) + items, err := os.ReadDir(path) if err != nil { return names, err }