Skip to content

Commit

Permalink
Remove all io/ioutil imports. (#4106)
Browse files Browse the repository at this point in the history
* Remove all io/ioutil imports.

* Convert DirEntry to FileInfo for mode checks.
  • Loading branch information
cmacknz authored Jan 22, 2024
1 parent f6827a0 commit 65a68f8
Show file tree
Hide file tree
Showing 49 changed files with 158 additions and 176 deletions.
3 changes: 1 addition & 2 deletions dev-tools/cmd/buildfleetcfg/buildfleetcfg.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions dev-tools/licenses/license_generate.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions dev-tools/mage/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -118,7 +117,7 @@ func joinMaps(args ...map[string]interface{}) map[string]interface{} {
}

func expandFile(src, dst string, args ...map[string]interface{}) error {
tmplData, err := ioutil.ReadFile(src)
tmplData, err := os.ReadFile(src)
if err != nil {
return fmt.Errorf("failed reading from template %v, %w", src, err)
}
Expand All @@ -133,7 +132,7 @@ func expandFile(src, dst string, args ...map[string]interface{}) error {
return err
}

if err = ioutil.WriteFile(createDir(dst), []byte(output), 0644); err != nil {
if err = os.WriteFile(createDir(dst), []byte(output), 0644); err != nil {
return fmt.Errorf("failed to write rendered template: %w", err)
}

Expand Down Expand Up @@ -236,13 +235,13 @@ func FindReplace(file string, re *regexp.Regexp, repl string) error {
return err
}

contents, err := ioutil.ReadFile(file)
contents, err := os.ReadFile(file)
if err != nil {
return err
}

out := re.ReplaceAllString(string(contents), repl)
return ioutil.WriteFile(file, []byte(out), info.Mode().Perm())
return os.WriteFile(file, []byte(out), info.Mode().Perm())
}

// MustFindReplace invokes FindReplace and panics if an error occurs.
Expand Down
3 changes: 1 addition & 2 deletions dev-tools/mage/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package mage
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -159,7 +158,7 @@ func makeConfigTemplate(destination string, mode os.FileMode, confParams ConfigF
}
}

data, err := ioutil.ReadFile(confFile.Template)
data, err := os.ReadFile(confFile.Template)
if err != nil {
return errors.Wrapf(err, "failed to read config template %q", confFile.Template)
}
Expand Down
34 changes: 23 additions & 11 deletions dev-tools/mage/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
package mage

import (
"fmt"
"io"
"io/ioutil"
"io/fs"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -41,7 +42,7 @@ func (t *CopyTask) Execute() error {
return errors.Wrapf(err, "copy failed: cannot stat source file %v", t.Source)
}

return errors.Wrap(t.recursiveCopy(t.Source, t.Dest, info), "copy failed")
return errors.Wrap(t.recursiveCopy(t.Source, t.Dest, fs.FileInfoToDirEntry(info)), "copy failed")
}

func (t *CopyTask) init() error {
Expand All @@ -64,14 +65,14 @@ func (t *CopyTask) isExcluded(src string) bool {
return false
}

func (t *CopyTask) recursiveCopy(src, dest string, info os.FileInfo) error {
if info.IsDir() {
return t.dirCopy(src, dest, info)
func (t *CopyTask) recursiveCopy(src, dest string, entry fs.DirEntry) error {
if entry.IsDir() {
return t.dirCopy(src, dest, entry)
}
return t.fileCopy(src, dest, info)
return t.fileCopy(src, dest, entry)
}

func (t *CopyTask) fileCopy(src, dest string, info os.FileInfo) error {
func (t *CopyTask) fileCopy(src, dest string, entry fs.DirEntry) error {
if t.isExcluded(src) {
return nil
}
Expand All @@ -82,6 +83,11 @@ func (t *CopyTask) fileCopy(src, dest string, info os.FileInfo) error {
}
defer srcFile.Close()

info, err := entry.Info()
if err != nil {
return fmt.Errorf("converting dir entry: %w", err)
}

if !info.Mode().IsRegular() {
return errors.Errorf("failed to copy source file because it is not a " +
"regular file")
Expand All @@ -104,28 +110,34 @@ func (t *CopyTask) fileCopy(src, dest string, info os.FileInfo) error {
return destFile.Close()
}

func (t *CopyTask) dirCopy(src, dest string, info os.FileInfo) error {
func (t *CopyTask) dirCopy(src, dest string, entry fs.DirEntry) error {
if t.isExcluded(src) {
return nil
}

info, err := entry.Info()
if err != nil {
return fmt.Errorf("converting dir entry: %w", err)
}

mode := t.DirMode
if mode == 0 {
mode = info.Mode()
}

if err := os.MkdirAll(dest, mode&os.ModePerm); err != nil {
return errors.Wrap(err, "failed creating dirs")
}

contents, err := ioutil.ReadDir(src)
contents, err := os.ReadDir(src)
if err != nil {
return errors.Wrapf(err, "failed to read dir %v", src)
}

for _, info := range contents {
for _, entry := range contents {
srcFile := filepath.Join(src, info.Name())
destFile := filepath.Join(dest, info.Name())
if err = t.recursiveCopy(srcFile, destFile, info); err != nil {
if err = t.recursiveCopy(srcFile, destFile, entry); err != nil {
return errors.Wrapf(err, "failed to copy %v to %v", srcFile, destFile)
}
}
Expand Down
9 changes: 4 additions & 5 deletions dev-tools/mage/gotest.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -126,14 +125,14 @@ func DefaultTestBinaryArgs() TestBinaryArgs {
// Use MODULE=module to run only tests for `module`.
func GoTestIntegrationForModule(ctx context.Context) error {
module := EnvOr("MODULE", "")
modulesFileInfo, err := ioutil.ReadDir("./module")
modulesDirEntry, err := os.ReadDir("./module")
if err != nil {
return err
}

foundModule := false
failedModules := []string{}
for _, fi := range modulesFileInfo {
for _, fi := range modulesDirEntry {
if !fi.IsDir() {
continue
}
Expand Down Expand Up @@ -327,7 +326,7 @@ func GoTest(ctx context.Context, params GoTestArgs) error {
codecovReport = strings.TrimSuffix(params.CoverageProfileFile,
filepath.Ext(params.CoverageProfileFile)) + "-cov.xml"

coverage, err := ioutil.ReadFile(params.CoverageProfileFile)
coverage, err := os.ReadFile(params.CoverageProfileFile)
if err != nil {
return fmt.Errorf("failed to read code coverage report: %w", err)
}
Expand Down Expand Up @@ -364,7 +363,7 @@ func makeCommand(ctx context.Context, env map[string]string, cmd string, args ..
for k, v := range env {
c.Env = append(c.Env, k+"="+v)
}
c.Stdout = ioutil.Discard
c.Stdout = io.Discard
if mg.Verbose() {
c.Stdout = os.Stdout
}
Expand Down
8 changes: 4 additions & 4 deletions dev-tools/mage/integtest_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package mage
import (
"fmt"
"go/build"
"io/ioutil"
"io"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -138,14 +138,14 @@ func (d *DockerIntegrationTester) Test(dir string, mageTarget string, env map[st
}

// Docker-compose rm is noisy. So only pass through stderr when in verbose.
out := ioutil.Discard
out := io.Discard
if mg.Verbose() {
out = os.Stderr
}

_, err = sh.Exec(
composeEnv,
ioutil.Discard,
io.Discard,
out,
"docker-compose",
"-p", dockerComposeProjectName(),
Expand Down Expand Up @@ -266,7 +266,7 @@ func dockerComposeBuildImages() error {
args = append(args, "--pull")
}

out := ioutil.Discard
out := io.Discard
if mg.Verbose() {
out = os.Stderr
}
Expand Down
10 changes: 5 additions & 5 deletions dev-tools/mage/kubernetes/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package kubernetes

import (
"fmt"
"io/ioutil"
"io"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -53,8 +53,8 @@ func (m *KindIntegrationTestStep) Setup(env map[string]string) error {
}

clusterName := kubernetesClusterName()
stdOut := ioutil.Discard
stdErr := ioutil.Discard
stdOut := io.Discard
stdErr := io.Discard
if mg.Verbose() {
stdOut = os.Stdout
stdErr = os.Stderr
Expand Down Expand Up @@ -103,8 +103,8 @@ func (m *KindIntegrationTestStep) Setup(env map[string]string) error {

// Teardown destroys the kubernetes cluster.
func (m *KindIntegrationTestStep) Teardown(env map[string]string) error {
stdOut := ioutil.Discard
stdErr := ioutil.Discard
stdOut := io.Discard
stdErr := io.Discard
if mg.Verbose() {
stdOut = os.Stdout
stdErr = os.Stderr
Expand Down
4 changes: 2 additions & 2 deletions dev-tools/mage/kubernetes/kuberemote.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (
"encoding/pem"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"strings"
"time"
Expand Down Expand Up @@ -610,7 +610,7 @@ func podDone(event watch.Event) (bool, error) {
func createTempFile(content []byte) (string, error) {
randBytes := make([]byte, 16)
rand.Read(randBytes)
tmpfile, err := ioutil.TempFile("", hex.EncodeToString(randBytes))
tmpfile, err := os.CreateTemp("", hex.EncodeToString(randBytes))
if err != nil {
return "", err
}
Expand Down
5 changes: 2 additions & 3 deletions dev-tools/mage/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package kubernetes
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -58,8 +57,8 @@ func (d *IntegrationTester) StepRequirements() mage.IntegrationTestSteps {

// Test performs the tests with kubernetes.
func (d *IntegrationTester) Test(dir string, mageTarget string, env map[string]string) error {
stdOut := ioutil.Discard
stdErr := ioutil.Discard
stdOut := io.Discard
stdErr := io.Discard
if mg.Verbose() {
stdOut = os.Stdout
stdErr = os.Stderr
Expand Down
4 changes: 2 additions & 2 deletions dev-tools/mage/pkgspecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ package mage
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"

"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -138,7 +138,7 @@ func LoadNamedSpec(name string, files ...string) error {
func LoadSpecs(files ...string) (map[string][]OSPackageArgs, error) {
var data [][]byte
for _, file := range files {
d, err := ioutil.ReadFile(file)
d, err := os.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("failed to read from spec file: %w", err)
}
Expand Down
Loading

0 comments on commit 65a68f8

Please sign in to comment.