From dd0e67540f131d6e0455eca902831f24bc058bc6 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Sun, 25 Feb 2024 15:35:25 -0500 Subject: [PATCH] all: Remove deprecated io/ioutil package Porting everything to the newer imports was trivial except for one instance which required a very small refactor. --- engine/graph/reverse.go | 9 +++--- engine/resources/aws_ec2.go | 4 +-- engine/resources/docker_container.go | 4 +-- engine/resources/docker_container_test.go | 4 +-- engine/resources/docker_image.go | 4 +-- engine/resources/file.go | 36 +++++++++++++---------- engine/resources/group.go | 4 +-- engine/resources/mount.go | 5 ++-- engine/resources/mount_linux_test.go | 7 ++--- engine/resources/mount_test.go | 15 +++++----- engine/resources/net.go | 5 ++-- engine/resources/password.go | 4 +-- engine/resources/resources_test.go | 17 +++++------ engine/resources/user.go | 4 +-- examples/longpoll/redirect-client.go | 4 +-- gapi/helpers.go | 4 +-- integration/basic_test.go | 5 ++-- integration/cluster.go | 3 +- integration/instance.go | 9 +++--- lang/core/core_test.go | 3 +- lang/core/os/readfile_func.go | 4 +-- lang/core/sys/cpucount_fact.go | 4 +-- lang/funcs/funcgen/func.go | 3 +- lang/funcs/funcgen/func_test.go | 8 ++--- lang/funcs/funcgen/pkg.go | 4 +-- lang/funcs/funcgen/pkg_test.go | 6 ++-- lang/inputs/inputs.go | 12 ++++---- lang/interfaces/metadata.go | 3 +- lang/interpret_test.go | 19 ++++++------ lib/main.go | 3 +- pgp/pgp.go | 6 ++-- pgraph/graphviz.go | 3 +- puppet/gapi.go | 7 ++--- test/test-govet.sh | 5 ++++ util/afero_test.go | 19 ++++++------ 35 files changed, 125 insertions(+), 131 deletions(-) diff --git a/engine/graph/reverse.go b/engine/graph/reverse.go index a95fa6fe..82294ce9 100644 --- a/engine/graph/reverse.go +++ b/engine/graph/reverse.go @@ -19,7 +19,6 @@ package graph import ( "fmt" - "io/ioutil" "os" "path" "sort" @@ -161,7 +160,7 @@ func (obj *Engine) ReversalList() (map[string]string, error) { result := make(map[string]string) // some key to contents dir := obj.statePrefix() // loop through this dir... - files, err := ioutil.ReadDir(dir) + files, err := os.ReadDir(dir) if err != nil && !os.IsNotExist(err) { return nil, errwrap.Wrapf(err, "error reading list of state dirs") } else if err != nil { @@ -171,7 +170,7 @@ func (obj *Engine) ReversalList() (map[string]string, error) { for _, x := range files { key := x.Name() // some uid for the resource file := path.Join(dir, key, ReverseFile) - content, err := ioutil.ReadFile(file) + content, err := os.ReadFile(file) if err != nil && !os.IsNotExist(err) { return nil, errwrap.Wrapf(err, "could not read reverse file: %s", file) } else if err != nil { @@ -263,7 +262,7 @@ func (obj *State) ReversalWrite(str string, overwrite bool) error { } file := path.Join(dir, ReverseFile) // return a unique file - content, err := ioutil.ReadFile(file) + content, err := os.ReadFile(file) if err != nil && !os.IsNotExist(err) { return errwrap.Wrapf(err, "could not read reverse file: %s", file) } @@ -280,7 +279,7 @@ func (obj *State) ReversalWrite(str string, overwrite bool) error { } } - return ioutil.WriteFile(file, []byte(str), ReversePerm) + return os.WriteFile(file, []byte(str), ReversePerm) } // ReversalDelete removes the reversal state information for this resource. diff --git a/engine/resources/aws_ec2.go b/engine/resources/aws_ec2.go index 18e4a06a..b7c0f6df 100644 --- a/engine/resources/aws_ec2.go +++ b/engine/resources/aws_ec2.go @@ -24,7 +24,7 @@ import ( "encoding/json" "encoding/pem" "fmt" - "io/ioutil" + "io" "net" "net/http" "regexp" @@ -980,7 +980,7 @@ func (obj *AwsEc2Res) snsGetCert(url string) (*x509.Certificate, error) { return nil, errwrap.Wrapf(err, "http get error") } defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return nil, errwrap.Wrapf(err, "error reading post body") } diff --git a/engine/resources/docker_container.go b/engine/resources/docker_container.go index 7daef5c8..500707a8 100644 --- a/engine/resources/docker_container.go +++ b/engine/resources/docker_container.go @@ -22,7 +22,7 @@ package resources import ( "context" "fmt" - "io/ioutil" + "io" "regexp" "strings" "time" @@ -287,7 +287,7 @@ func (obj *DockerContainerRes) CheckApply(ctx context.Context, apply bool) (bool return false, errwrap.Wrapf(err, "error pulling image") } // Wait for the image to download, EOF signals that it's done. - if _, err := ioutil.ReadAll(p); err != nil { + if _, err := io.ReadAll(p); err != nil { return false, errwrap.Wrapf(err, "error reading image pull result") } diff --git a/engine/resources/docker_container_test.go b/engine/resources/docker_container_test.go index 2787d016..03705aad 100644 --- a/engine/resources/docker_container_test.go +++ b/engine/resources/docker_container_test.go @@ -22,7 +22,7 @@ package resources import ( "context" "fmt" - "io/ioutil" + "io" "log" "os" "testing" @@ -155,7 +155,7 @@ func setup() error { if err != nil { return fmt.Errorf("error pulling image: %s", err) } - if _, err := ioutil.ReadAll(p); err != nil { + if _, err := io.ReadAll(p); err != nil { return fmt.Errorf("error reading image pull result: %s", err) } diff --git a/engine/resources/docker_image.go b/engine/resources/docker_image.go index 5815732c..4dd57969 100644 --- a/engine/resources/docker_image.go +++ b/engine/resources/docker_image.go @@ -22,7 +22,7 @@ package resources import ( "context" "fmt" - "io/ioutil" + "io" "regexp" "strings" "time" @@ -211,7 +211,7 @@ func (obj *DockerImageRes) CheckApply(ctx context.Context, apply bool) (checkOK return false, errwrap.Wrapf(err, "error pulling image") } // Wait for the image to download, EOF signals that it's done. - if _, err := ioutil.ReadAll(p); err != nil { + if _, err := io.ReadAll(p); err != nil { return false, errwrap.Wrapf(err, "error reading image pull result") } diff --git a/engine/resources/file.go b/engine/resources/file.go index 0dab5966..7ce2dfb5 100644 --- a/engine/resources/file.go +++ b/engine/resources/file.go @@ -24,7 +24,7 @@ import ( "encoding/hex" "fmt" "io" - "io/ioutil" + "io/fs" "os" "path" "path/filepath" @@ -1076,7 +1076,7 @@ func (obj *FileRes) fragmentsCheckApply(ctx context.Context, apply bool) (bool, for _, frag := range obj.Fragments { // It's a single file. Add it to what we're building... if isDir := strings.HasSuffix(frag, "/"); !isDir { - out, err := ioutil.ReadFile(frag) + out, err := os.ReadFile(frag) if err != nil { return false, errwrap.Wrapf(err, "could not read file fragment") } @@ -1085,7 +1085,7 @@ func (obj *FileRes) fragmentsCheckApply(ctx context.Context, apply bool) (bool, } // We're a dir, peer inside... - files, err := ioutil.ReadDir(frag) + files, err := os.ReadDir(frag) if err != nil { return false, errwrap.Wrapf(err, "could not read fragment directory") } @@ -1096,7 +1096,7 @@ func (obj *FileRes) fragmentsCheckApply(ctx context.Context, apply bool) (bool, continue } f := path.Join(frag, file.Name()) - out, err := ioutil.ReadFile(f) + out, err := os.ReadFile(f) if err != nil { return false, errwrap.Wrapf(err, "could not read directory file fragment") } @@ -1571,7 +1571,7 @@ func (obj *FileRes) Reversed() (engine.ReversibleRes, error) { // We do this whether we specified content with Content or w/ Fragments. // The `res.State != FileStateAbsent` check is an optional optimization. if ((obj.Content != nil || len(obj.Fragments) > 0) || obj.State == FileStateAbsent) && res.State != FileStateAbsent { - content, err := ioutil.ReadFile(obj.getPath()) + content, err := os.ReadFile(obj.getPath()) if err != nil && !os.IsNotExist(err) { return nil, errwrap.Wrapf(err, "could not read file for reversal storage") } @@ -1598,8 +1598,8 @@ func (obj *FileRes) Reversed() (engine.ReversibleRes, error) { } // There is a race if the operating system is adding/changing/removing - // the file between the ioutil.Readfile at the top and here. If there is - // a discrepancy between the two, then you might get an unexpected + // the file between the os.ReadFile at the top and here. If there is a + // discrepancy between the two, then you might get an unexpected // reverse, but in reality, your perspective is pretty absurd. This is a // user error, and not an issue we actually care about, afaict. fileInfo, err := os.Stat(obj.getPath()) @@ -1647,9 +1647,9 @@ func (obj *FileRes) GraphQueryAllowed(opts ...engine.GraphQueryableOption) error } // smartPath adds a trailing slash to the path if it is a directory. -func smartPath(fileInfo os.FileInfo) string { - smartPath := fileInfo.Name() // absolute path - if fileInfo.IsDir() { +func smartPath(dirEntry fs.DirEntry) string { + smartPath := dirEntry.Name() // absolute path + if dirEntry.IsDir() { smartPath += "/" // add a trailing slash for dirs } return smartPath @@ -1670,24 +1670,30 @@ func ReadDir(path string) ([]FileInfo, error) { return nil, fmt.Errorf("path must be a directory") } output := []FileInfo{} // my file info - fileInfos, err := ioutil.ReadDir(path) + files, err := os.ReadDir(path) if os.IsNotExist(err) { return output, err // return empty list } if err != nil { return nil, err } - for _, fi := range fileInfos { - abs := path + smartPath(fi) + for _, file := range files { + abs := path + smartPath(file) rel, err := filepath.Rel(path, abs) // NOTE: calls Clean() if err != nil { // shouldn't happen return nil, errwrap.Wrapf(err, "unhandled error in ReadDir") } - if fi.IsDir() { + if file.IsDir() { rel += "/" // add a trailing slash for dirs } + + fileInfo, err := file.Info() + if err != nil { + return nil, errwrap.Wrapf(err, "unhandled error in FileInfo") + } + x := FileInfo{ - FileInfo: fi, + FileInfo: fileInfo, AbsPath: abs, RelPath: rel, } diff --git a/engine/resources/group.go b/engine/resources/group.go index 2bf0114d..081ec35d 100644 --- a/engine/resources/group.go +++ b/engine/resources/group.go @@ -20,7 +20,7 @@ package resources import ( "context" "fmt" - "io/ioutil" + "io" "os/exec" "os/user" "strconv" @@ -210,7 +210,7 @@ func (obj *GroupRes) CheckApply(ctx context.Context, apply bool) (bool, error) { return false, errwrap.Wrapf(err, "cmd failed to start") } // capture any error messages - slurp, err := ioutil.ReadAll(stderr) + slurp, err := io.ReadAll(stderr) if err != nil { return false, errwrap.Wrapf(err, "error slurping error message") } diff --git a/engine/resources/mount.go b/engine/resources/mount.go index 28c1b46c..5327fc3f 100644 --- a/engine/resources/mount.go +++ b/engine/resources/mount.go @@ -21,7 +21,6 @@ import ( "bytes" "context" "fmt" - "io/ioutil" "os" "path/filepath" "strings" @@ -148,7 +147,7 @@ func (obj *MountRes) Validate() error { } // validate type - fs, err := ioutil.ReadFile(procFilesystems) + fs, err := os.ReadFile(procFilesystems) if err != nil { return errwrap.Wrapf(err, "error reading %s", procFilesystems) } @@ -517,7 +516,7 @@ func (obj *MountRes) fstabWrite(file string, mounts fstab.Mounts) error { contents := fmt.Sprintf("# Generated by %s at %d", obj.init.Program, time.Now().UnixNano()) + "\n" contents = contents + mounts.String() + "\n" // write the file - if err := ioutil.WriteFile(file, []byte(contents), fstabUmask); err != nil { + if err := os.WriteFile(file, []byte(contents), fstabUmask); err != nil { return errwrap.Wrapf(err, "error writing fstab file: %s", file) } return nil diff --git a/engine/resources/mount_linux_test.go b/engine/resources/mount_linux_test.go index 3eb1f741..5e427fce 100644 --- a/engine/resources/mount_linux_test.go +++ b/engine/resources/mount_linux_test.go @@ -20,7 +20,6 @@ package resources import ( - "io/ioutil" "os" "testing" @@ -49,18 +48,18 @@ func TestMountExists(t *testing.T) { }, } - file, err := ioutil.TempFile("", "proc") + file, err := os.CreateTemp("", "proc") if err != nil { t.Errorf("error creating temp file: %v", err) return } defer os.Remove(file.Name()) for _, test := range mountExistsTests { - if err := ioutil.WriteFile(file.Name(), test.procMock, 0664); err != nil { + if err := os.WriteFile(file.Name(), test.procMock, 0664); err != nil { t.Errorf("error writing proc file: %s: %v", file.Name(), err) return } - if err := ioutil.WriteFile(test.in.Spec, []byte{}, 0664); err != nil { + if err := os.WriteFile(test.in.Spec, []byte{}, 0664); err != nil { t.Errorf("error writing fstab file: %s: %v", file.Name(), err) return } diff --git a/engine/resources/mount_test.go b/engine/resources/mount_test.go index b976ed21..f24a21a4 100644 --- a/engine/resources/mount_test.go +++ b/engine/resources/mount_test.go @@ -20,7 +20,6 @@ package resources import ( - "io/ioutil" "os" "testing" @@ -65,7 +64,7 @@ var fstabWriteTests = []struct { } func (obj *MountRes) TestFstabWrite(t *testing.T) { - file, err := ioutil.TempFile("", "fstab") + file, err := os.CreateTemp("", "fstab") if err != nil { t.Errorf("error creating temp file: %v", err) return @@ -117,7 +116,7 @@ var fstabEntryAddTests = []struct { } func (obj *MountRes) TestFstabEntryAdd(t *testing.T) { - file, err := ioutil.TempFile("", "fstab") + file, err := os.CreateTemp("", "fstab") if err != nil { t.Errorf("error creating temp file: %v", err) return @@ -125,7 +124,7 @@ func (obj *MountRes) TestFstabEntryAdd(t *testing.T) { defer os.Remove(file.Name()) for _, test := range fstabEntryAddTests { - if err := ioutil.WriteFile(file.Name(), test.fstabMock, 0644); err != nil { + if err := os.WriteFile(file.Name(), test.fstabMock, 0644); err != nil { t.Errorf("error writing fstab file: %s: %v", file.Name(), err) return } @@ -163,7 +162,7 @@ var fstabEntryRemoveTests = []struct { } func (obj *MountRes) TestFstabEntryRemove(t *testing.T) { - file, err := ioutil.TempFile("", "fstab") + file, err := os.CreateTemp("", "fstab") if err != nil { t.Errorf("error creating temp file: %v", err) return @@ -171,7 +170,7 @@ func (obj *MountRes) TestFstabEntryRemove(t *testing.T) { defer os.Remove(file.Name()) for _, test := range fstabEntryRemoveTests { - if err := ioutil.WriteFile(file.Name(), test.fstabMock, 0644); err != nil { + if err := os.WriteFile(file.Name(), test.fstabMock, 0644); err != nil { t.Errorf("error writing fstab file: %s: %v", file.Name(), err) return } @@ -258,7 +257,7 @@ var fstabEntryExistsTests = []struct { } func TestFstabEntryExists(t *testing.T) { - file, err := ioutil.TempFile("", "fstab") + file, err := os.CreateTemp("", "fstab") if err != nil { t.Errorf("error creating temp file: %v", err) return @@ -266,7 +265,7 @@ func TestFstabEntryExists(t *testing.T) { defer os.Remove(file.Name()) for _, test := range fstabEntryExistsTests { - if err := ioutil.WriteFile(file.Name(), test.fstabMock, 0644); err != nil { + if err := os.WriteFile(file.Name(), test.fstabMock, 0644); err != nil { t.Errorf("error writing fstab file: %s: %v", file.Name(), err) return } diff --git a/engine/resources/net.go b/engine/resources/net.go index 6703e962..173cee0a 100644 --- a/engine/resources/net.go +++ b/engine/resources/net.go @@ -23,7 +23,6 @@ import ( "bytes" "context" "fmt" - "io/ioutil" "net" "os" "path" @@ -509,7 +508,7 @@ func (obj *NetRes) fileCheckApply(ctx context.Context, apply bool) (bool, error) fileContents := []byte{} if exists { // check the file contents - byt, err := ioutil.ReadFile(obj.unitFilePath) + byt, err := os.ReadFile(obj.unitFilePath) if err != nil { return false, errwrap.Wrapf(err, "error reading file") } @@ -535,7 +534,7 @@ func (obj *NetRes) fileCheckApply(ctx context.Context, apply bool) (bool, error) } // all other situations, we write the file - if err := ioutil.WriteFile(obj.unitFilePath, contents, networkdUnitFileUmask); err != nil { + if err := os.WriteFile(obj.unitFilePath, contents, networkdUnitFileUmask); err != nil { return false, errwrap.Wrapf(err, "error writing configuration file") } return false, nil diff --git a/engine/resources/password.go b/engine/resources/password.go index 3ec8ca1d..7499849d 100644 --- a/engine/resources/password.go +++ b/engine/resources/password.go @@ -21,7 +21,7 @@ import ( "context" "crypto/rand" "fmt" - "io/ioutil" + "io" "math/big" "os" "path" @@ -109,7 +109,7 @@ func (obj *PasswordRes) read() (string, error) { return "", err } defer file.Close() - data, err := ioutil.ReadAll(file) + data, err := io.ReadAll(file) if err != nil { return "", errwrap.Wrapf(err, "could not read from file") } diff --git a/engine/resources/resources_test.go b/engine/resources/resources_test.go index 66e867e8..ee0475a9 100644 --- a/engine/resources/resources_test.go +++ b/engine/resources/resources_test.go @@ -22,7 +22,6 @@ package resources import ( "context" "fmt" - "io/ioutil" "os" "os/user" "path" @@ -166,7 +165,7 @@ func FileExpect(p, s string) Step { // path & string return &manualStep{ action: func() error { return nil }, expect: func() error { - content, err := ioutil.ReadFile(p) + content, err := os.ReadFile(p) if err != nil { return err } @@ -207,7 +206,7 @@ func FileWrite(p, s string) Step { // path & string action: func() error { // TODO: apparently using 0666 is equivalent to respecting the current umask const umask = 0666 - return ioutil.WriteFile(p, []byte(s), umask) + return os.WriteFile(p, []byte(s), umask) }, expect: func() error { return nil }, } @@ -318,7 +317,7 @@ func TestResources1(t *testing.T) { timeline: timeline, expect: func() error { return nil }, // build file for inotifywait - startup: func() error { return ioutil.WriteFile(f, []byte("starting...\n"), 0666) }, + startup: func() error { return os.WriteFile(f, []byte("starting...\n"), 0666) }, cleanup: func() error { return os.Remove(f) }, }) } @@ -354,7 +353,7 @@ func TestResources1(t *testing.T) { timeline: timeline, expect: func() error { return nil }, // build file for inotifywait - startup: func() error { return ioutil.WriteFile(f, []byte("starting...\n"), 0666) }, + startup: func() error { return os.WriteFile(f, []byte("starting...\n"), 0666) }, cleanup: func() error { return os.Remove(f) }, }) } @@ -402,7 +401,7 @@ func TestResources1(t *testing.T) { fail: false, timeline: timeline, expect: func() error { return nil }, - startup: func() error { return ioutil.WriteFile(p, []byte(content), 0666) }, + startup: func() error { return os.WriteFile(p, []byte(content), 0666) }, cleanup: func() error { return os.Remove(p) }, }) } @@ -429,7 +428,7 @@ func TestResources1(t *testing.T) { fail: false, timeline: timeline, expect: func() error { return nil }, - startup: func() error { return ioutil.WriteFile(p, []byte(content), 0666) }, + startup: func() error { return os.WriteFile(p, []byte(content), 0666) }, cleanup: func() error { return os.Remove(p) }, }) } @@ -841,13 +840,13 @@ func TestResources2(t *testing.T) { fileWrite := func(p, s string) func() error { // write the file to path return func() error { - return ioutil.WriteFile(p, []byte(s), 0666) + return os.WriteFile(p, []byte(s), 0666) } } fileExpect := func(p, s string) func() error { // check the contents at the path match the string we expect return func() error { - content, err := ioutil.ReadFile(p) + content, err := os.ReadFile(p) if err != nil { return err } diff --git a/engine/resources/user.go b/engine/resources/user.go index d2ac89b5..8e63c96e 100644 --- a/engine/resources/user.go +++ b/engine/resources/user.go @@ -20,7 +20,7 @@ package resources import ( "context" "fmt" - "io/ioutil" + "io" "os/exec" "os/user" "sort" @@ -276,7 +276,7 @@ func (obj *UserRes) CheckApply(ctx context.Context, apply bool) (bool, error) { return false, errwrap.Wrapf(err, "cmd failed to start") } // capture any error messages - slurp, err := ioutil.ReadAll(stderr) + slurp, err := io.ReadAll(stderr) if err != nil { return false, errwrap.Wrapf(err, "error slurping error message") } diff --git a/examples/longpoll/redirect-client.go b/examples/longpoll/redirect-client.go index 112347d3..1d34a040 100644 --- a/examples/longpoll/redirect-client.go +++ b/examples/longpoll/redirect-client.go @@ -7,7 +7,7 @@ package main import ( "bytes" "fmt" - "io/ioutil" + "io" "log" "math/rand" "net/http" @@ -46,7 +46,7 @@ func main() { } log.Printf("Event received: %+v", result) - s, err := ioutil.ReadAll(result.Body) // TODO: apparently we can stream + s, err := io.ReadAll(result.Body) // TODO: apparently we can stream result.Body.Close() log.Printf("Response: %+v", string(s)) log.Printf("Error: %+v", err) diff --git a/gapi/helpers.go b/gapi/helpers.go index 1538dd8e..a5bcc565 100644 --- a/gapi/helpers.go +++ b/gapi/helpers.go @@ -18,7 +18,7 @@ package gapi import ( - "io/ioutil" + "os" "github.com/purpleidea/mgmt/engine" "github.com/purpleidea/mgmt/util" @@ -31,7 +31,7 @@ const Umask = 0666 // CopyFileToFs copies a file from src path on the local fs to a dst path on fs. func CopyFileToFs(fs engine.WriteableFS, src, dst string) error { - data, err := ioutil.ReadFile(src) + data, err := os.ReadFile(src) if err != nil { return errwrap.Wrapf(err, "can't read from file `%s`", src) } diff --git a/integration/basic_test.go b/integration/basic_test.go index 87a2871e..69c9bf59 100644 --- a/integration/basic_test.go +++ b/integration/basic_test.go @@ -21,7 +21,6 @@ package integration import ( "fmt" - "io/ioutil" "os" "path" "sort" @@ -136,7 +135,7 @@ func TestInstance1(t *testing.T) { sort.Strings(files) // loop in a deterministic order for _, f := range files { filename := path.Join(d, RootDirectory, f) - b, err := ioutil.ReadFile(filename) + b, err := os.ReadFile(filename) if err != nil { t.Errorf("could not read file: `%s`", filename) continue @@ -274,7 +273,7 @@ func TestCluster1(t *testing.T) { sort.Strings(files) // loop in a deterministic order for _, f := range files { filename := path.Join(d, RootDirectory, f) - b, err := ioutil.ReadFile(filename) + b, err := os.ReadFile(filename) if err != nil { t.Errorf("could not read file: `%s`", filename) continue diff --git a/integration/cluster.go b/integration/cluster.go index a3f13f78..a097813e 100644 --- a/integration/cluster.go +++ b/integration/cluster.go @@ -20,7 +20,6 @@ package integration import ( "context" "fmt" - "io/ioutil" "os" "os/exec" "path" @@ -68,7 +67,7 @@ func (obj *Cluster) Init() error { // create temporary directory to use during testing if obj.dir == "" { - obj.dir, err = ioutil.TempDir("", "mgmt-integration-cluster-") + obj.dir, err = os.MkdirTemp("", "mgmt-integration-cluster-") if err != nil { return errwrap.Wrapf(err, "can't create temporary directory") } diff --git a/integration/instance.go b/integration/instance.go index e3883d92..ff4f5b29 100644 --- a/integration/instance.go +++ b/integration/instance.go @@ -20,7 +20,6 @@ package integration import ( "context" "fmt" - "io/ioutil" "os" "os/exec" "path" @@ -117,7 +116,7 @@ func (obj *Instance) Init() error { // create temporary directory to use during testing if obj.dir == "" { var err error - obj.dir, err = ioutil.TempDir("", fmt.Sprintf("mgmt-integration-%s-", obj.Hostname)) + obj.dir, err = os.MkdirTemp("", fmt.Sprintf("mgmt-integration-%s-", obj.Hostname)) if err != nil { return errwrap.Wrapf(err, "can't create temporary directory") } @@ -379,7 +378,7 @@ func (obj *Instance) Wait(ctx context.Context) error { return ctx.Err() } - contents, err := ioutil.ReadFile(obj.convergedStatusFile) + contents, err := os.ReadFile(obj.convergedStatusFile) if err != nil { continue // file might not exist yet, wait for an event } @@ -418,7 +417,7 @@ func (obj *Instance) DeployLang(code string) error { filename := path.Join(obj.dir, "deploy.mcl") data := []byte(code) - if err := ioutil.WriteFile(filename, data, fileMode); err != nil { + if err := os.WriteFile(filename, data, fileMode); err != nil { return err } @@ -451,7 +450,7 @@ func (obj *Instance) Dir() string { // CombinedOutput returns the logged output from the instance. func (obj *Instance) CombinedOutput() (string, error) { - contents, err := ioutil.ReadFile(path.Join(obj.dir, StdoutStderrFile)) + contents, err := os.ReadFile(path.Join(obj.dir, StdoutStderrFile)) if err != nil { return "", err } diff --git a/lang/core/core_test.go b/lang/core/core_test.go index ed6fcf1e..8333b00b 100644 --- a/lang/core/core_test.go +++ b/lang/core/core_test.go @@ -22,7 +22,6 @@ package core import ( "context" "fmt" - "io/ioutil" "os" "reflect" "sync" @@ -489,7 +488,7 @@ func TestLiveFuncExec0(t *testing.T) { funcname: "os.readfile", timeline: timeline, expect: func() error { return nil }, - startup: func() error { return ioutil.WriteFile(p, []byte(content), 0666) }, + startup: func() error { return os.WriteFile(p, []byte(content), 0666) }, cleanup: func() error { return os.Remove(p) }, }) } diff --git a/lang/core/os/readfile_func.go b/lang/core/os/readfile_func.go index 2d52955e..60a13c25 100644 --- a/lang/core/os/readfile_func.go +++ b/lang/core/os/readfile_func.go @@ -20,7 +20,7 @@ package coreos import ( "context" "fmt" - "io/ioutil" + "os" "sync" "github.com/purpleidea/mgmt/lang/funcs" @@ -203,7 +203,7 @@ func (obj *ReadFileFunc) Stream(ctx context.Context) error { } // read file... - content, err := ioutil.ReadFile(*obj.filename) + content, err := os.ReadFile(*obj.filename) if err != nil { return errwrap.Wrapf(err, "error reading file") } diff --git a/lang/core/sys/cpucount_fact.go b/lang/core/sys/cpucount_fact.go index 9a4b94b2..c666f749 100644 --- a/lang/core/sys/cpucount_fact.go +++ b/lang/core/sys/cpucount_fact.go @@ -21,7 +21,7 @@ package coresys import ( "context" - "io/ioutil" + "os" "regexp" "strconv" "strings" @@ -173,7 +173,7 @@ func (obj CPUCountFact) Stream(ctx context.Context) error { // getCPUCount looks in sysfs to get the number of CPUs that are online. func getCPUCount() (int64, error) { - dat, err := ioutil.ReadFile("/sys/devices/system/cpu/online") + dat, err := os.ReadFile("/sys/devices/system/cpu/online") if err != nil { return 0, err } diff --git a/lang/funcs/funcgen/func.go b/lang/funcs/funcgen/func.go index 4729936c..2bbd5ae7 100644 --- a/lang/funcs/funcgen/func.go +++ b/lang/funcs/funcgen/func.go @@ -19,7 +19,6 @@ package main import ( "fmt" - "io/ioutil" "log" "os" "path/filepath" @@ -72,7 +71,7 @@ func parseFuncs(c config, f functions, path, templates string) error { func generateTemplate(c config, f functions, path, templateFile, finalName string) error { log.Printf("Reading: %s", templateFile) basename := filepath.Base(templateFile) - tplFile, err := ioutil.ReadFile(templateFile) + tplFile, err := os.ReadFile(templateFile) if err != nil { return err } diff --git a/lang/funcs/funcgen/func_test.go b/lang/funcs/funcgen/func_test.go index e2b7b9b9..65a32284 100644 --- a/lang/funcs/funcgen/func_test.go +++ b/lang/funcs/funcgen/func_test.go @@ -19,7 +19,7 @@ package main import ( "fmt" - "io/ioutil" + "os" "reflect" "testing" @@ -37,7 +37,7 @@ func testRenderFuncsWithFixture(t *testing.T, fixture string) { } funcs := &functions{} - fixtures, err := ioutil.ReadFile(fmt.Sprintf("fixtures/func_%s.yaml", fixture)) + fixtures, err := os.ReadFile(fmt.Sprintf("fixtures/func_%s.yaml", fixture)) if err != nil { t.Fatalf("Fixtures (yaml) unreadable!\n%v", err) } @@ -46,7 +46,7 @@ func testRenderFuncsWithFixture(t *testing.T, fixture string) { t.Fatalf("Fixtures (yaml) unreadable!\n%v", err) } - golangFixtures, err := ioutil.ReadFile(fmt.Sprintf("fixtures/func_%s.tpl", fixture)) + golangFixtures, err := os.ReadFile(fmt.Sprintf("fixtures/func_%s.tpl", fixture)) if err != nil { t.Fatalf("Fixtures (tpl) unreadable!\n%v", err) } @@ -60,7 +60,7 @@ func testRenderFuncsWithFixture(t *testing.T, fixture string) { if err != nil { t.Fatalf("Not generating template!\n%v", err) } - result, err := ioutil.ReadFile(fmt.Sprintf("fixtures/%s", dstFileName)) + result, err := os.ReadFile(fmt.Sprintf("fixtures/%s", dstFileName)) if err != nil { t.Fatalf("Result unreadable!\n%v", err) } diff --git a/lang/funcs/funcgen/pkg.go b/lang/funcs/funcgen/pkg.go index be8f194a..f19e27ce 100644 --- a/lang/funcs/funcgen/pkg.go +++ b/lang/funcs/funcgen/pkg.go @@ -21,8 +21,8 @@ import ( "bytes" "errors" "fmt" - "io/ioutil" "log" + "os" "os/exec" "path/filepath" "regexp" @@ -55,7 +55,7 @@ func parsePkg(path, filename, templates string) error { var c config filePath := filepath.Join(path, filename) log.Printf("Data: %s", filePath) - cfgFile, err := ioutil.ReadFile(filePath) + cfgFile, err := os.ReadFile(filePath) if err != nil { return err } diff --git a/lang/funcs/funcgen/pkg_test.go b/lang/funcs/funcgen/pkg_test.go index 52665a67..44b074c8 100644 --- a/lang/funcs/funcgen/pkg_test.go +++ b/lang/funcs/funcgen/pkg_test.go @@ -19,7 +19,7 @@ package main import ( "fmt" - "io/ioutil" + "os" "reflect" "testing" @@ -36,7 +36,7 @@ func testParseFuncsWithFixture(t *testing.T, fixture string) { Exclude: []string{"ToLower"}, } - signatures, err := ioutil.ReadFile(fmt.Sprintf("fixtures/func_%s.txt", fixture)) + signatures, err := os.ReadFile(fmt.Sprintf("fixtures/func_%s.txt", fixture)) if err != nil { t.Fatalf("Fixtures (txt) unreadable!\n%v", err) } @@ -46,7 +46,7 @@ func testParseFuncsWithFixture(t *testing.T, fixture string) { } expected := &functions{} - fixtures, err := ioutil.ReadFile(fmt.Sprintf("fixtures/func_%s.yaml", fixture)) + fixtures, err := os.ReadFile(fmt.Sprintf("fixtures/func_%s.yaml", fixture)) if err != nil { t.Fatalf("Fixtures (yaml) unreadable!\n%v", err) } diff --git a/lang/inputs/inputs.go b/lang/inputs/inputs.go index 8e8ea3e7..939042ed 100644 --- a/lang/inputs/inputs.go +++ b/lang/inputs/inputs.go @@ -25,7 +25,7 @@ package inputs import ( "fmt" - "io/ioutil" + "io" "os" "path/filepath" "strings" @@ -162,7 +162,7 @@ func inputStdin(s string, fs engine.Fs) (*ParsedInput, error) { // TODO: yes, we could pass a reader directly, but we'd // need to have a convention for it to get closed after // and we need to save it to disk for deploys to use it - b, err := ioutil.ReadAll(os.Stdin) // doesn't need fs + b, err := io.ReadAll(os.Stdin) // doesn't need fs if err != nil { return nil, errwrap.Wrapf(err, "can't read in stdin") } @@ -202,8 +202,8 @@ func inputMetadata(s string, fs engine.Fs) (*ParsedInput, error) { if err != nil { return nil, errwrap.Wrapf(err, "can't read from file: `%s`", m) } - defer fm.Close() // we're done reading by the time this runs - b, err := ioutil.ReadAll(fm) // doesn't need fs + defer fm.Close() // we're done reading by the time this runs + b, err := io.ReadAll(fm) // doesn't need fs if err != nil { return nil, errwrap.Wrapf(err, "can't read in file: `%s`", m) } @@ -251,8 +251,8 @@ func inputMcl(s string, fs engine.Fs) (*ParsedInput, error) { if err != nil { return nil, errwrap.Wrapf(err, "can't read from file: `%s`", s) } - defer fm.Close() // we're done reading by the time this runs - b, err := ioutil.ReadAll(fm) // doesn't need fs + defer fm.Close() // we're done reading by the time this runs + b, err := io.ReadAll(fm) // doesn't need fs if err != nil { return nil, errwrap.Wrapf(err, "can't read in file: `%s`", s) } diff --git a/lang/interfaces/metadata.go b/lang/interfaces/metadata.go index 8e7f9fd4..f71a37ae 100644 --- a/lang/interfaces/metadata.go +++ b/lang/interfaces/metadata.go @@ -20,7 +20,6 @@ package interfaces import ( "fmt" "io" - "io/ioutil" "strings" "github.com/purpleidea/mgmt/util/errwrap" @@ -196,7 +195,7 @@ func ParseMetadata(reader io.Reader) (*Metadata, error) { //if err := decoder.Decode(metadata); err != nil { // return nil, errwrap.Wrapf(err, "can't parse metadata") //} - b, err := ioutil.ReadAll(reader) + b, err := io.ReadAll(reader) if err != nil { return nil, errwrap.Wrapf(err, "can't read metadata") } diff --git a/lang/interpret_test.go b/lang/interpret_test.go index d542695e..5e5e47b7 100644 --- a/lang/interpret_test.go +++ b/lang/interpret_test.go @@ -24,7 +24,6 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" "os" "path/filepath" "sort" @@ -123,7 +122,7 @@ func TestAstFunc1(t *testing.T) { testCases := []test{} // build test array automatically from reading the dir - files, err := ioutil.ReadDir(dir) + files, err := os.ReadDir(dir) if err != nil { t.Errorf("could not read through tests directory: %+v", err) return @@ -207,7 +206,7 @@ func TestAstFunc1(t *testing.T) { t.Errorf("err making dir(%s): %+v", dir, err) return } - if err := ioutil.WriteFile(name, file.Data, 0660); err != nil { + if err := os.WriteFile(name, file.Data, 0660); err != nil { t.Errorf("err writing file(%s): %+v", name, err) return } @@ -289,7 +288,7 @@ func TestAstFunc1(t *testing.T) { t.Logf(fmt.Sprintf("test #%d", index)+": "+format, v...) } mmFs := afero.NewMemMapFs() - afs := &afero.Afero{Fs: mmFs} // wrap so that we're implementing ioutil + afs := &afero.Afero{Fs: mmFs} // wrap to implement the fs API's fs := &util.AferoFs{Afero: afs} // use this variant, so that we don't copy the dir name @@ -603,7 +602,7 @@ func TestAstFunc2(t *testing.T) { testCases := []test{} // build test array automatically from reading the dir - files, err := ioutil.ReadDir(dir) + files, err := os.ReadDir(dir) if err != nil { t.Errorf("could not read through tests directory: %+v", err) return @@ -690,7 +689,7 @@ func TestAstFunc2(t *testing.T) { t.Errorf("err making dir(%s): %+v", dir, err) return } - if err := ioutil.WriteFile(name, file.Data, 0660); err != nil { + if err := os.WriteFile(name, file.Data, 0660); err != nil { t.Errorf("err writing file(%s): %+v", name, err) return } @@ -796,7 +795,7 @@ func TestAstFunc2(t *testing.T) { t.Logf(fmt.Sprintf("test #%d", index)+": "+format, v...) } mmFs := afero.NewMemMapFs() - afs := &afero.Afero{Fs: mmFs} // wrap so that we're implementing ioutil + afs := &afero.Afero{Fs: mmFs} // wrap to implement the fs API's fs := &util.AferoFs{Afero: afs} // implementation of the Local API (we only expect just this single one) @@ -1411,7 +1410,7 @@ func TestAstFunc3(t *testing.T) { testCases := []test{} // build test array automatically from reading the dir - files, err := ioutil.ReadDir(dir) + files, err := os.ReadDir(dir) if err != nil { t.Errorf("could not read through tests directory: %+v", err) return @@ -1498,7 +1497,7 @@ func TestAstFunc3(t *testing.T) { t.Errorf("err making dir(%s): %+v", dir, err) return } - if err := ioutil.WriteFile(name, file.Data, 0660); err != nil { + if err := os.WriteFile(name, file.Data, 0660); err != nil { t.Errorf("err writing file(%s): %+v", name, err) return } @@ -1598,7 +1597,7 @@ func TestAstFunc3(t *testing.T) { t.Logf(fmt.Sprintf("test #%d", index)+": "+format, v...) } mmFs := afero.NewMemMapFs() - afs := &afero.Afero{Fs: mmFs} // wrap so that we're implementing ioutil + afs := &afero.Afero{Fs: mmFs} // wrap to implement the fs API's fs := &util.AferoFs{Afero: afs} // implementation of the Local API (we only expect just this single one) diff --git a/lib/main.go b/lib/main.go index 882e100e..8f6a6335 100644 --- a/lib/main.go +++ b/lib/main.go @@ -23,7 +23,6 @@ package lib import ( "context" "fmt" - "io/ioutil" "log" "os" "os/user" @@ -258,7 +257,7 @@ func (obj *Main) Run() error { if obj.TmpPrefix || os.MkdirAll(prefix, 0770) != nil { if obj.TmpPrefix || obj.AllowTmpPrefix { var err error - if prefix, err = ioutil.TempDir("", obj.Program+"-"+hostname+"-"); err != nil { + if prefix, err = os.MkdirTemp("", obj.Program+"-"+hostname+"-"); err != nil { return fmt.Errorf("can't create temporary prefix") } Logf("warning: working prefix directory is temporary!") diff --git a/pgp/pgp.go b/pgp/pgp.go index dfc19e76..14031741 100644 --- a/pgp/pgp.go +++ b/pgp/pgp.go @@ -24,7 +24,7 @@ import ( "bytes" "crypto" "encoding/base64" - "io/ioutil" + "io" "log" "os" "strings" @@ -155,7 +155,7 @@ func (obj *PGP) Encrypt(to *openpgp.Entity, msg string) (string, error) { } // encode to base64 - bytes, err := ioutil.ReadAll(buf) + bytes, err := io.ReadAll(buf) if err != nil { return "", errwrap.Wrapf(err, "can't read unverified body") } @@ -199,7 +199,7 @@ func (obj *PGP) Decrypt(encString string) (string, error) { return "", errwrap.Wrapf(err, "can't read message") } - bytes, err := ioutil.ReadAll(md.UnverifiedBody) + bytes, err := io.ReadAll(md.UnverifiedBody) if err != nil { return "", errwrap.Wrapf(err, "can't read unverified body") } diff --git a/pgraph/graphviz.go b/pgraph/graphviz.go index bb844ce8..4a36021a 100644 --- a/pgraph/graphviz.go +++ b/pgraph/graphviz.go @@ -20,7 +20,6 @@ package pgraph // TODO: this should be a subpackage import ( "fmt" "html" - "io/ioutil" "os" "os/exec" "sort" @@ -168,7 +167,7 @@ func (obj *Graphviz) Exec() error { uid, err1 := strconv.Atoi(os.Getenv("SUDO_UID")) gid, err2 := strconv.Atoi(os.Getenv("SUDO_GID")) - if err := ioutil.WriteFile(filename, []byte(obj.Text()), 0644); err != nil { + if err := os.WriteFile(filename, []byte(obj.Text()), 0644); err != nil { return errwrap.Wrapf(err, "error writing to filename") } diff --git a/puppet/gapi.go b/puppet/gapi.go index 75bfcce8..c945c23d 100644 --- a/puppet/gapi.go +++ b/puppet/gapi.go @@ -19,7 +19,6 @@ package puppet import ( "fmt" - "io/ioutil" "os" "strings" "sync" @@ -202,7 +201,7 @@ func (obj *GAPI) Init(data *gapi.Data) error { // store the puppet file on disk for other binaries to see and use prefix := fmt.Sprintf("%s-%s-%s", data.Program, data.Hostname, strings.Replace(PuppetFile, "/", "", -1)) - tmpfile, err := ioutil.TempFile("", prefix) + tmpfile, err := os.CreateTemp("", prefix) if err != nil { return errwrap.Wrapf(err, "can't create temp file") } @@ -222,7 +221,7 @@ func (obj *GAPI) Init(data *gapi.Data) error { } else if obj.Mode == "dir" { // store the puppet files on disk for other binaries to see and use prefix := fmt.Sprintf("%s-%s-%s", data.Program, data.Hostname, strings.Replace(PuppetSite, "/", "", -1)) - tmpdirName, err := ioutil.TempDir("", prefix) + tmpdirName, err := os.MkdirTemp("", prefix) if err != nil { return errwrap.Wrapf(err, "can't create temp dir") } @@ -244,7 +243,7 @@ func (obj *GAPI) Init(data *gapi.Data) error { // store the puppet conf on disk for other binaries to see and use prefix := fmt.Sprintf("%s-%s-%s", data.Program, data.Hostname, strings.Replace(PuppetConf, "/", "", -1)) - tmpfile, err := ioutil.TempFile("", prefix) + tmpfile, err := os.CreateTemp("", prefix) if err != nil { return errwrap.Wrapf(err, "can't create temp file") } diff --git a/test/test-govet.sh b/test/test-govet.sh index 8d36d7f6..7d7e6a60 100755 --- a/test/test-govet.sh +++ b/test/test-govet.sh @@ -94,6 +94,11 @@ function consistent-imports() { if grep '"golang.org/x/net/context"' "$1"; then # use built-in context return 1 fi + + # deprecated import + if grep $'\t"io/ioutil"' "$1"; then # use the documented replacements + return 1 + fi } function reflowed-comments() { diff --git a/util/afero_test.go b/util/afero_test.go index 5f141f6a..820ae5a9 100644 --- a/util/afero_test.go +++ b/util/afero_test.go @@ -21,7 +21,6 @@ package util import ( "bytes" - "io/ioutil" "os" "sort" "testing" @@ -125,7 +124,7 @@ func TestCopyDiskToFs1(t *testing.T) { return } t.Logf("tests directory is: %s", dir) - files, err := ioutil.ReadDir(dir) + files, err := os.ReadDir(dir) if err != nil { t.Errorf("could not read through tests directory: %+v", err) return @@ -146,7 +145,7 @@ func TestCopyDiskToFs1(t *testing.T) { //t.Logf("skipping: %s -> %+v", treeFile, err) continue } - content, err := ioutil.ReadFile(treeFileFull) + content, err := os.ReadFile(treeFileFull) if err != nil { t.Errorf("could not read tree file: %+v", err) return @@ -156,7 +155,7 @@ func TestCopyDiskToFs1(t *testing.T) { t.Logf("testing: %s", treeFile) mmFs := afero.NewMemMapFs() - afs := &afero.Afero{Fs: mmFs} // wrap so that we're implementing ioutil + afs := &afero.Afero{Fs: mmFs} // wrap to implement the fs API's fs := &AferoFs{Afero: afs} if err := CopyDiskToFs(fs, dir+f+"/", "/", false); err != nil { @@ -186,7 +185,7 @@ func TestCopyDiskToFs2(t *testing.T) { return } t.Logf("tests directory is: %s", dir) - files, err := ioutil.ReadDir(dir) + files, err := os.ReadDir(dir) if err != nil { t.Errorf("could not read through tests directory: %+v", err) return @@ -207,7 +206,7 @@ func TestCopyDiskToFs2(t *testing.T) { //t.Logf("skipping: %s -> %+v", treeFile, err) continue } - content, err := ioutil.ReadFile(treeFileFull) + content, err := os.ReadFile(treeFileFull) if err != nil { t.Errorf("could not read tree file: %+v", err) return @@ -217,7 +216,7 @@ func TestCopyDiskToFs2(t *testing.T) { t.Logf("testing: %s", treeFile) mmFs := afero.NewMemMapFs() - afs := &afero.Afero{Fs: mmFs} // wrap so that we're implementing ioutil + afs := &afero.Afero{Fs: mmFs} // wrap to implement the fs API's fs := &AferoFs{Afero: afs} src := dir + f + "/" @@ -250,7 +249,7 @@ func TestCopyDiskContentsToFs1(t *testing.T) { return } t.Logf("tests directory is: %s", dir) - files, err := ioutil.ReadDir(dir) + files, err := os.ReadDir(dir) if err != nil { t.Errorf("could not read through tests directory: %+v", err) return @@ -271,7 +270,7 @@ func TestCopyDiskContentsToFs1(t *testing.T) { //t.Logf("skipping: %s -> %+v", treeFile, err) continue } - content, err := ioutil.ReadFile(treeFileFull) + content, err := os.ReadFile(treeFileFull) if err != nil { t.Errorf("could not read tree file: %+v", err) return @@ -281,7 +280,7 @@ func TestCopyDiskContentsToFs1(t *testing.T) { t.Logf("testing: %s", treeFile) mmFs := afero.NewMemMapFs() - afs := &afero.Afero{Fs: mmFs} // wrap so that we're implementing ioutil + afs := &afero.Afero{Fs: mmFs} // wrap to implement the fs API's fs := &AferoFs{Afero: afs} if err := CopyDiskContentsToFs(fs, dir+f+"/", "/", false); err != nil {