From e5cd59a6076c6f7f050f6bb2ac6ea90bc678b786 Mon Sep 17 00:00:00 2001
From: Benjamin <30739825+ahrtr@users.noreply.github.com>
Date: Tue, 2 Nov 2021 03:21:26 +0800
Subject: [PATCH] dev: replace ioutil with io and os (#2318)

---
 internal/cache/cache.go                  |  2 +-
 internal/cache/cache_test.go             | 13 ++++++-------
 internal/cache/default.go                |  3 +--
 internal/cache/hash_test.go              |  3 +--
 internal/renameio/renameio.go            |  4 ++--
 internal/renameio/renameio_test.go       |  3 +--
 internal/renameio/umask_test.go          |  3 +--
 internal/robustio/robustio.go            |  2 +-
 internal/robustio/robustio_flaky.go      |  5 ++---
 internal/robustio/robustio_other.go      |  3 +--
 pkg/commands/run.go                      |  4 ++--
 pkg/fsutils/filecache.go                 |  4 ++--
 pkg/golinters/gofumpt.go                 |  4 ++--
 pkg/golinters/gosec.go                   |  4 ++--
 pkg/golinters/revive.go                  |  4 ++--
 pkg/result/processors/diff.go            |  3 +--
 scripts/expand_website_templates/main.go | 10 +++++-----
 test/errchk.go                           |  4 ++--
 test/fix_test.go                         |  5 ++---
 test/linters_test.go                     |  3 +--
 test/testshared/testshared.go            |  5 ++---
 21 files changed, 40 insertions(+), 51 deletions(-)

diff --git a/internal/cache/cache.go b/internal/cache/cache.go
index 51c75a77..5c3fda70 100644
--- a/internal/cache/cache.go
+++ b/internal/cache/cache.go
@@ -370,7 +370,7 @@ func (c *Cache) putIndexEntry(id ActionID, out OutputID, size int64, allowVerify
 		// Truncate the file only *after* writing it.
 		// (This should be a no-op, but truncate just in case of previous corruption.)
 		//
-		// This differs from ioutil.WriteFile, which truncates to 0 *before* writing
+		// This differs from os.WriteFile, which truncates to 0 *before* writing
 		// via os.O_TRUNC. Truncating only after writing ensures that a second write
 		// of the same content to the same file is idempotent, and does not — even
 		// temporarily! — undo the effect of the first write.
diff --git a/internal/cache/cache_test.go b/internal/cache/cache_test.go
index 514adc44..0efcee54 100644
--- a/internal/cache/cache_test.go
+++ b/internal/cache/cache_test.go
@@ -8,7 +8,6 @@ import (
 	"bytes"
 	"encoding/binary"
 	"fmt"
-	"io/ioutil"
 	"os"
 	"path/filepath"
 	"testing"
@@ -22,7 +21,7 @@ func init() {
 func TestBasic(t *testing.T) {
 	t.Parallel()
 
-	dir, err := ioutil.TempDir("", "cachetest-")
+	dir, err := os.MkdirTemp("", "cachetest-")
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -69,7 +68,7 @@ func TestBasic(t *testing.T) {
 func TestGrowth(t *testing.T) {
 	t.Parallel()
 
-	dir, err := ioutil.TempDir("", "cachetest-")
+	dir, err := os.MkdirTemp("", "cachetest-")
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -122,7 +121,7 @@ func TestVerifyPanic(t *testing.T) {
 		t.Fatal("initEnv did not set verify")
 	}
 
-	dir, err := ioutil.TempDir("", "cachetest-")
+	dir, err := os.MkdirTemp("", "cachetest-")
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -157,7 +156,7 @@ func dummyID(x int) [HashSize]byte {
 func TestCacheTrim(t *testing.T) {
 	t.Parallel()
 
-	dir, err := ioutil.TempDir("", "cachetest-")
+	dir, err := os.MkdirTemp("", "cachetest-")
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -213,7 +212,7 @@ func TestCacheTrim(t *testing.T) {
 		t.Fatal(err)
 	}
 	c.OutputFile(entry.OutputID)
-	data, err := ioutil.ReadFile(filepath.Join(dir, "trim.txt"))
+	data, err := os.ReadFile(filepath.Join(dir, "trim.txt"))
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -226,7 +225,7 @@ func TestCacheTrim(t *testing.T) {
 		t.Fatal(err)
 	}
 	c.OutputFile(entry.OutputID)
-	data2, err := ioutil.ReadFile(filepath.Join(dir, "trim.txt"))
+	data2, err := os.ReadFile(filepath.Join(dir, "trim.txt"))
 	if err != nil {
 		t.Fatal(err)
 	}
diff --git a/internal/cache/default.go b/internal/cache/default.go
index e8866cb3..66950062 100644
--- a/internal/cache/default.go
+++ b/internal/cache/default.go
@@ -6,7 +6,6 @@ package cache
 
 import (
 	"fmt"
-	"io/ioutil"
 	"log"
 	"os"
 	"path/filepath"
@@ -39,7 +38,7 @@ func initDefaultCache() {
 	}
 	if _, err := os.Stat(filepath.Join(dir, "README")); err != nil {
 		// Best effort.
-		if wErr := ioutil.WriteFile(filepath.Join(dir, "README"), []byte(cacheREADME), 0666); wErr != nil {
+		if wErr := os.WriteFile(filepath.Join(dir, "README"), []byte(cacheREADME), 0666); wErr != nil {
 			log.Fatalf("Failed to write README file to cache dir %s: %s", dir, err)
 		}
 	}
diff --git a/internal/cache/hash_test.go b/internal/cache/hash_test.go
index d96fb31e..810e5aa0 100644
--- a/internal/cache/hash_test.go
+++ b/internal/cache/hash_test.go
@@ -6,7 +6,6 @@ package cache
 
 import (
 	"fmt"
-	"io/ioutil"
 	"os"
 	"testing"
 )
@@ -28,7 +27,7 @@ func TestHash(t *testing.T) {
 }
 
 func TestHashFile(t *testing.T) {
-	f, err := ioutil.TempFile("", "cmd-go-test-")
+	f, err := os.CreateTemp("", "cmd-go-test-")
 	if err != nil {
 		t.Fatal(err)
 	}
diff --git a/internal/renameio/renameio.go b/internal/renameio/renameio.go
index fa9d93bf..2f88f4f7 100644
--- a/internal/renameio/renameio.go
+++ b/internal/renameio/renameio.go
@@ -24,7 +24,7 @@ func Pattern(filename string) string {
 	return filepath.Join(filepath.Dir(filename), filepath.Base(filename)+patternSuffix)
 }
 
-// WriteFile is like ioutil.WriteFile, but first writes data to an arbitrary
+// WriteFile is like os.WriteFile, but first writes data to an arbitrary
 // file in the same directory as filename, then renames it atomically to the
 // final name.
 //
@@ -79,7 +79,7 @@ func tempFile(dir, prefix string, perm os.FileMode) (f *os.File, err error) {
 	return
 }
 
-// ReadFile is like ioutil.ReadFile, but on Windows retries spurious errors that
+// ReadFile is like os.ReadFile, but on Windows retries spurious errors that
 // may occur if the file is concurrently replaced.
 //
 // Errors are classified heuristically and retries are bounded, so even this
diff --git a/internal/renameio/renameio_test.go b/internal/renameio/renameio_test.go
index ae188e58..0f8dd348 100644
--- a/internal/renameio/renameio_test.go
+++ b/internal/renameio/renameio_test.go
@@ -8,7 +8,6 @@ package renameio
 
 import (
 	"encoding/binary"
-	"io/ioutil"
 	"math/rand"
 	"os"
 	"path/filepath"
@@ -23,7 +22,7 @@ import (
 )
 
 func TestConcurrentReadsAndWrites(t *testing.T) {
-	dir, err := ioutil.TempDir("", "renameio")
+	dir, err := os.MkdirTemp("", "renameio")
 	if err != nil {
 		t.Fatal(err)
 	}
diff --git a/internal/renameio/umask_test.go b/internal/renameio/umask_test.go
index d75d67c9..921a2bdb 100644
--- a/internal/renameio/umask_test.go
+++ b/internal/renameio/umask_test.go
@@ -7,7 +7,6 @@
 package renameio
 
 import (
-	"io/ioutil"
 	"os"
 	"path/filepath"
 	"syscall"
@@ -15,7 +14,7 @@ import (
 )
 
 func TestWriteFileModeAppliesUmask(t *testing.T) {
-	dir, err := ioutil.TempDir("", "renameio")
+	dir, err := os.MkdirTemp("", "renameio")
 	if err != nil {
 		t.Fatalf("Failed to create temporary directory: %v", err)
 	}
diff --git a/internal/robustio/robustio.go b/internal/robustio/robustio.go
index 76e47ad1..ce3dbbde 100644
--- a/internal/robustio/robustio.go
+++ b/internal/robustio/robustio.go
@@ -22,7 +22,7 @@ func Rename(oldpath, newpath string) error {
 	return rename(oldpath, newpath)
 }
 
-// ReadFile is like ioutil.ReadFile, but on Windows retries errors that may
+// ReadFile is like os.ReadFile, but on Windows retries errors that may
 // occur if the file is concurrently replaced.
 //
 // (See golang.org/issue/31247 and golang.org/issue/32188.)
diff --git a/internal/robustio/robustio_flaky.go b/internal/robustio/robustio_flaky.go
index e0bf5b9b..5963027e 100644
--- a/internal/robustio/robustio_flaky.go
+++ b/internal/robustio/robustio_flaky.go
@@ -7,7 +7,6 @@
 package robustio
 
 import (
-	"io/ioutil"
 	"math/rand"
 	"os"
 	"syscall"
@@ -70,11 +69,11 @@ func rename(oldpath, newpath string) (err error) {
 	})
 }
 
-// readFile is like ioutil.ReadFile, but retries ephemeral errors.
+// readFile is like os.ReadFile, but retries ephemeral errors.
 func readFile(filename string) ([]byte, error) {
 	var b []byte
 	err := retry(func() (err error, mayRetry bool) {
-		b, err = ioutil.ReadFile(filename)
+		b, err = os.ReadFile(filename)
 
 		// Unlike in rename, we do not retry errFileNotFound here: it can occur
 		// as a spurious error, but the file may also genuinely not exist, so the
diff --git a/internal/robustio/robustio_other.go b/internal/robustio/robustio_other.go
index a2428856..b7d01b34 100644
--- a/internal/robustio/robustio_other.go
+++ b/internal/robustio/robustio_other.go
@@ -7,7 +7,6 @@
 package robustio
 
 import (
-	"io/ioutil"
 	"os"
 )
 
@@ -16,7 +15,7 @@ func rename(oldpath, newpath string) error {
 }
 
 func readFile(filename string) ([]byte, error) {
-	return ioutil.ReadFile(filename)
+	return os.ReadFile(filename)
 }
 
 func removeAll(path string) error {
diff --git a/pkg/commands/run.go b/pkg/commands/run.go
index f2ea5415..11ae7d7e 100644
--- a/pkg/commands/run.go
+++ b/pkg/commands/run.go
@@ -3,7 +3,7 @@ package commands
 import (
 	"context"
 	"fmt"
-	"io/ioutil"
+	"io"
 	"log"
 	"os"
 	"runtime"
@@ -390,7 +390,7 @@ func (e *Executor) runAndPrint(ctx context.Context, args []string) error {
 
 	if !logutils.HaveDebugTag("linters_output") {
 		// Don't allow linters and loader to print anything
-		log.SetOutput(ioutil.Discard)
+		log.SetOutput(io.Discard)
 		savedStdout, savedStderr := e.setOutputToDevNull()
 		defer func() {
 			os.Stdout, os.Stderr = savedStdout, savedStderr
diff --git a/pkg/fsutils/filecache.go b/pkg/fsutils/filecache.go
index 2b17a039..04c66823 100644
--- a/pkg/fsutils/filecache.go
+++ b/pkg/fsutils/filecache.go
@@ -2,7 +2,7 @@ package fsutils
 
 import (
 	"fmt"
-	"io/ioutil"
+	"os"
 	"sync"
 
 	"github.com/pkg/errors"
@@ -24,7 +24,7 @@ func (fc *FileCache) GetFileBytes(filePath string) ([]byte, error) {
 		return cachedBytes.([]byte), nil
 	}
 
-	fileBytes, err := ioutil.ReadFile(filePath)
+	fileBytes, err := os.ReadFile(filePath)
 	if err != nil {
 		return nil, errors.Wrapf(err, "can't read file %s", filePath)
 	}
diff --git a/pkg/golinters/gofumpt.go b/pkg/golinters/gofumpt.go
index 75c08814..455572d6 100644
--- a/pkg/golinters/gofumpt.go
+++ b/pkg/golinters/gofumpt.go
@@ -3,7 +3,7 @@ package golinters
 import (
 	"bytes"
 	"fmt"
-	"io/ioutil"
+	"os"
 	"sync"
 
 	"github.com/pkg/errors"
@@ -50,7 +50,7 @@ func NewGofumpt() *goanalysis.Linter {
 			var issues []goanalysis.Issue
 
 			for _, f := range fileNames {
-				input, err := ioutil.ReadFile(f)
+				input, err := os.ReadFile(f)
 				if err != nil {
 					return nil, fmt.Errorf("unable to open file %s: %w", f, err)
 				}
diff --git a/pkg/golinters/gosec.go b/pkg/golinters/gosec.go
index 85a2a6e0..9610b3e8 100644
--- a/pkg/golinters/gosec.go
+++ b/pkg/golinters/gosec.go
@@ -3,7 +3,7 @@ package golinters
 import (
 	"fmt"
 	"go/token"
-	"io/ioutil"
+	"io"
 	"log"
 	"strconv"
 	"strings"
@@ -42,7 +42,7 @@ func NewGosec(settings *config.GoSecSettings) *goanalysis.Linter {
 
 	ruleDefinitions := rules.Generate(filters...)
 
-	logger := log.New(ioutil.Discard, "", 0)
+	logger := log.New(io.Discard, "", 0)
 
 	analyzer := &analysis.Analyzer{
 		Name: gosecName,
diff --git a/pkg/golinters/revive.go b/pkg/golinters/revive.go
index 590332e6..061c9b47 100644
--- a/pkg/golinters/revive.go
+++ b/pkg/golinters/revive.go
@@ -5,7 +5,7 @@ import (
 	"encoding/json"
 	"fmt"
 	"go/token"
-	"io/ioutil"
+	"os"
 	"reflect"
 
 	"github.com/BurntSushi/toml"
@@ -65,7 +65,7 @@ func NewRevive(cfg *config.ReviveSettings) *goanalysis.Linter {
 				return nil, err
 			}
 
-			revive := lint.New(ioutil.ReadFile)
+			revive := lint.New(os.ReadFile)
 
 			lintingRules, err := reviveConfig.GetLintingRules(conf)
 			if err != nil {
diff --git a/pkg/result/processors/diff.go b/pkg/result/processors/diff.go
index 92ab2bd5..65e01785 100644
--- a/pkg/result/processors/diff.go
+++ b/pkg/result/processors/diff.go
@@ -4,7 +4,6 @@ import (
 	"bytes"
 	"fmt"
 	"io"
-	"io/ioutil"
 	"os"
 	"strings"
 
@@ -44,7 +43,7 @@ func (p Diff) Process(issues []result.Issue) ([]result.Issue, error) {
 
 	var patchReader io.Reader
 	if p.patchFilePath != "" {
-		patch, err := ioutil.ReadFile(p.patchFilePath)
+		patch, err := os.ReadFile(p.patchFilePath)
 		if err != nil {
 			return nil, fmt.Errorf("can't read from patch file %s: %s", p.patchFilePath, err)
 		}
diff --git a/scripts/expand_website_templates/main.go b/scripts/expand_website_templates/main.go
index a6dfe435..9d045cf9 100644
--- a/scripts/expand_website_templates/main.go
+++ b/scripts/expand_website_templates/main.go
@@ -7,7 +7,7 @@ import (
 	"encoding/json"
 	"flag"
 	"fmt"
-	"io/ioutil"
+	"io"
 	"log"
 	"net/http"
 	"os"
@@ -96,7 +96,7 @@ func rewriteDocs(replacements map[string]string) error {
 }
 
 func processDoc(path string, replacements map[string]string, madeReplacements map[string]bool) error {
-	contentBytes, err := ioutil.ReadFile(path)
+	contentBytes, err := os.ReadFile(path)
 	if err != nil {
 		return fmt.Errorf("failed to read %s: %w", path, err)
 	}
@@ -147,7 +147,7 @@ func getLatestVersion() (string, error) {
 		return "", fmt.Errorf("failed to get http response for the latest tag: %s", err)
 	}
 	defer resp.Body.Close()
-	body, err := ioutil.ReadAll(resp.Body)
+	body, err := io.ReadAll(resp.Body)
 	if err != nil {
 		return "", fmt.Errorf("failed to read a body for the latest tag: %s", err)
 	}
@@ -160,7 +160,7 @@ func getLatestVersion() (string, error) {
 }
 
 func buildTemplateContext() (map[string]string, error) {
-	golangciYamlExample, err := ioutil.ReadFile(".golangci.example.yml")
+	golangciYamlExample, err := os.ReadFile(".golangci.example.yml")
 	if err != nil {
 		return nil, fmt.Errorf("can't read .golangci.example.yml: %s", err)
 	}
@@ -191,7 +191,7 @@ func buildTemplateContext() (map[string]string, error) {
 
 	helpLines := bytes.Split(help, []byte("\n"))
 	shortHelp := bytes.Join(helpLines[2:], []byte("\n"))
-	changeLog, err := ioutil.ReadFile("CHANGELOG.md")
+	changeLog, err := os.ReadFile("CHANGELOG.md")
 	if err != nil {
 		return nil, err
 	}
diff --git a/test/errchk.go b/test/errchk.go
index b84d7b9c..09710a5f 100644
--- a/test/errchk.go
+++ b/test/errchk.go
@@ -4,8 +4,8 @@ import (
 	"bytes"
 	"errors"
 	"fmt"
-	"io/ioutil"
 	"log"
+	"os"
 	"regexp"
 	"strconv"
 	"strings"
@@ -183,7 +183,7 @@ var (
 func wantedErrors(file, short, defaultLinter string) (errs []wantedError) {
 	cache := make(map[string]*regexp.Regexp)
 
-	src, err := ioutil.ReadFile(file)
+	src, err := os.ReadFile(file)
 	if err != nil {
 		log.Fatal(err)
 	}
diff --git a/test/fix_test.go b/test/fix_test.go
index 97cde388..644a7d0b 100644
--- a/test/fix_test.go
+++ b/test/fix_test.go
@@ -1,7 +1,6 @@
 package test
 
 import (
-	"io/ioutil"
 	"os"
 	"os/exec"
 	"path/filepath"
@@ -54,10 +53,10 @@ func TestFix(t *testing.T) {
 			require.NoError(t, err)
 
 			testshared.NewLintRunner(t).RunWithYamlConfig(string(cfg), args...)
-			output, err := ioutil.ReadFile(input)
+			output, err := os.ReadFile(input)
 			require.NoError(t, err)
 
-			expectedOutput, err := ioutil.ReadFile(filepath.Join(testdataDir, "fix", "out", filepath.Base(input)))
+			expectedOutput, err := os.ReadFile(filepath.Join(testdataDir, "fix", "out", filepath.Base(input)))
 			require.NoError(t, err)
 
 			require.Equal(t, string(expectedOutput), string(output))
diff --git a/test/linters_test.go b/test/linters_test.go
index 93fa1eac..35537048 100644
--- a/test/linters_test.go
+++ b/test/linters_test.go
@@ -2,7 +2,6 @@ package test
 
 import (
 	"bufio"
-	"io/ioutil"
 	"os"
 	"os/exec"
 	"path/filepath"
@@ -99,7 +98,7 @@ func TestGciLocal(t *testing.T) {
 }
 
 func saveConfig(t *testing.T, cfg map[string]interface{}) (cfgPath string, finishFunc func()) {
-	f, err := ioutil.TempFile("", "golangci_lint_test")
+	f, err := os.CreateTemp("", "golangci_lint_test")
 	require.NoError(t, err)
 
 	cfgPath = f.Name() + ".yml"
diff --git a/test/testshared/testshared.go b/test/testshared/testshared.go
index c1a89d69..0d1f91c0 100644
--- a/test/testshared/testshared.go
+++ b/test/testshared/testshared.go
@@ -1,7 +1,6 @@
 package testshared
 
 import (
-	"io/ioutil"
 	"os"
 	"os/exec"
 	"strings"
@@ -134,7 +133,7 @@ func (r *LintRunner) RunWithYamlConfig(cfg string, args ...string) *RunResult {
 }
 
 func (r *LintRunner) RunCommandWithYamlConfig(cfg, command string, args ...string) *RunResult {
-	f, err := ioutil.TempFile("", "golangci_lint_test")
+	f, err := os.CreateTemp("", "golangci_lint_test")
 	assert.NoError(r.t, err)
 	f.Close()
 
@@ -149,7 +148,7 @@ func (r *LintRunner) RunCommandWithYamlConfig(cfg, command string, args ...strin
 	cfg = strings.TrimSpace(cfg)
 	cfg = strings.Replace(cfg, "\t", " ", -1)
 
-	err = ioutil.WriteFile(cfgPath, []byte(cfg), os.ModePerm)
+	err = os.WriteFile(cfgPath, []byte(cfg), os.ModePerm)
 	assert.NoError(r.t, err)
 
 	pargs := append([]string{"-c", cfgPath}, args...)