Stop using deprecated ioutil functions

This commit is contained in:
Katrina Verey
2022-08-10 16:59:18 -04:00
parent 416eed97c4
commit f6b72077c8
70 changed files with 322 additions and 362 deletions

View File

@@ -7,7 +7,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
@@ -122,7 +121,7 @@ func AddGenerateDockerfile(cmd *cobra.Command) {
Use: "gen [DIR]",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if err := ioutil.WriteFile(filepath.Join(args[0], "Dockerfile"), []byte(`FROM golang:1.18-alpine as builder
if err := os.WriteFile(filepath.Join(args[0], "Dockerfile"), []byte(`FROM golang:1.18-alpine as builder
ENV CGO_ENABLED=0
WORKDIR /go/src/
COPY go.mod go.sum ./
@@ -143,7 +142,7 @@ ENTRYPOINT ["function"]
}
func functionConfigFromFile(file string) (*yaml.RNode, error) {
b, err := ioutil.ReadFile(file)
b, err := os.ReadFile(file)
if err != nil {
return nil, errors.WrapPrefixf(err, "unable to read configuration file %q", file)
}
@@ -161,7 +160,7 @@ type deferredFileReader struct {
func (fr *deferredFileReader) Read(dest []byte) (int, error) {
if fr.srcReader == nil {
src, err := ioutil.ReadFile(fr.path)
src, err := os.ReadFile(fr.path)
if err != nil {
return 0, errors.WrapPrefixf(err, "unable to read input file %s", fr.path)
}

View File

@@ -6,7 +6,7 @@ package command_test
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
@@ -36,7 +36,7 @@ func TestCommand_dockerfile(t *testing.T) {
t.FailNow()
}
b, err := ioutil.ReadFile(filepath.Join(d, "Dockerfile"))
b, err := os.ReadFile(filepath.Join(d, "Dockerfile"))
if !assert.NoError(t, err) {
t.FailNow()
}

View File

@@ -6,7 +6,6 @@ package frameworktestutil
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"regexp"
@@ -216,7 +215,7 @@ func (rc *ProcessorResultsChecker) runInCurrentDir(t *testing.T) (string, string
require.NoError(t, err)
actualOutput := bytes.NewBuffer([]byte{})
rlBytes, err := ioutil.ReadFile(rc.InputFilename)
rlBytes, err := os.ReadFile(rc.InputFilename)
require.NoError(t, err)
rw := kio.ByteReadWriter{
@@ -338,10 +337,10 @@ func (rc *checkerCore) shouldUpdateFixtures() bool {
func (rc *checkerCore) updateFixtures(t *testing.T, actualOutput string, actualError string) {
t.Helper()
if actualError != "" {
require.NoError(t, ioutil.WriteFile(rc.expectedErrorFilename, []byte(actualError), 0600))
require.NoError(t, os.WriteFile(rc.expectedErrorFilename, []byte(actualError), 0600))
}
if len(actualOutput) > 0 {
require.NoError(t, ioutil.WriteFile(rc.expectedOutputFilename, []byte(actualOutput), 0600))
require.NoError(t, os.WriteFile(rc.expectedOutputFilename, []byte(actualOutput), 0600))
}
t.Skip("Updated fixtures for test case")
}
@@ -367,7 +366,7 @@ func (rc *checkerCore) readAssertionFiles(t *testing.T) (string, string) {
}
if err == nil {
// only read the file if it exists
b, err := ioutil.ReadFile(rc.expectedOutputFilename)
b, err := os.ReadFile(rc.expectedOutputFilename)
if !assert.NoError(t, err) {
t.FailNow()
}
@@ -381,7 +380,7 @@ func (rc *checkerCore) readAssertionFiles(t *testing.T) (string, string) {
}
if err == nil {
// only read the file if it exists
b, err := ioutil.ReadFile(rc.expectedErrorFilename)
b, err := os.ReadFile(rc.expectedErrorFilename)
if !assert.NoError(t, err) {
t.FailNow()
}

View File

@@ -4,8 +4,8 @@
package parser
import (
"io"
iofs "io/fs"
"io/ioutil"
"path"
"strings"
@@ -54,7 +54,7 @@ func (l parser) readPath(path string, processContent contentProcessor) error {
if err := checkExtension(path, l.extensions); err != nil {
return err
}
b, err := ioutil.ReadAll(f)
b, err := io.ReadAll(f)
if err != nil {
return err
}
@@ -100,5 +100,5 @@ func (l parser) readFile(path string) ([]byte, error) {
}
defer f.Close()
return ioutil.ReadAll(f)
return io.ReadAll(f)
}

View File

@@ -7,7 +7,7 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"strings"
@@ -268,7 +268,7 @@ func (c *FunctionFilter) doResults(r *kio.ByteReader) error {
if err != nil {
return err
}
err = ioutil.WriteFile(c.ResultsFile, []byte(results), 0600)
err = os.WriteFile(c.ResultsFile, []byte(results), 0600)
if err != nil {
return err
}

View File

@@ -6,7 +6,6 @@ package runtimeutil
import (
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"testing"
@@ -24,7 +23,7 @@ type testRun struct {
func (r testRun) run(reader io.Reader, writer io.Writer) error {
if r.expectedInput != "" {
input, err := ioutil.ReadAll(reader)
input, err := io.ReadAll(reader)
if !assert.NoError(r.t, err) {
r.t.FailNow()
}
@@ -1040,7 +1039,7 @@ metadata:
// results file setup
if len(tt.expectedResults) > 0 && !tt.noMakeResultsFile {
// expect result files to be written -- create a directory for them
f, err := ioutil.TempFile("", "test-kyaml-*.yaml")
f, err := os.CreateTemp("", "test-kyaml-*.yaml")
if !assert.NoError(t, err) {
t.FailNow()
}
@@ -1123,7 +1122,7 @@ metadata:
t.FailNow()
}
b, err := ioutil.ReadFile(tt.instance.ResultsFile)
b, err := os.ReadFile(tt.instance.ResultsFile)
writtenResults := strings.TrimSpace(string(b))
if !assert.NoError(t, err) {
t.FailNow()

View File

@@ -6,7 +6,6 @@ package starlark_test
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
@@ -200,13 +199,13 @@ run(ctx.resource_list["items"], ctx.resource_list["functionConfig"]["spec"]["val
// resource configuration read from a directory.
func ExampleFilter_Filter_file() {
// setup the configuration
d, err := ioutil.TempDir("", "")
d, err := os.MkdirTemp("", "")
if err != nil {
log.Println(err)
}
defer os.RemoveAll(d)
err = ioutil.WriteFile(filepath.Join(d, "deploy1.yaml"), []byte(`
err = os.WriteFile(filepath.Join(d, "deploy1.yaml"), []byte(`
apiVersion: apps/v1
kind: Deployment
metadata:
@@ -222,7 +221,7 @@ spec:
log.Println(err)
}
err = ioutil.WriteFile(filepath.Join(d, "deploy2.yaml"), []byte(`
err = os.WriteFile(filepath.Join(d, "deploy2.yaml"), []byte(`
apiVersion: apps/v1
kind: Deployment
metadata:
@@ -238,7 +237,7 @@ spec:
log.Println(err)
}
err = ioutil.WriteFile(filepath.Join(d, "annotate.star"), []byte(`
err = os.WriteFile(filepath.Join(d, "annotate.star"), []byte(`
def run(items):
for item in items:
item["metadata"]["annotations"]["foo"] = "bar"

View File

@@ -7,8 +7,8 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"go.starlark.net/starlark"
"sigs.k8s.io/kustomize/kyaml/errors"
@@ -57,7 +57,7 @@ func (sf *Filter) setup() error {
// read the program from a file
if sf.Path != "" {
b, err := ioutil.ReadFile(sf.Path)
b, err := os.ReadFile(sf.Path)
if err != nil {
return err
}
@@ -72,7 +72,7 @@ func (sf *Filter) setup() error {
return err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
if err != nil {
return err
}