all: Remove deprecated io/ioutil package

Porting everything to the newer imports was trivial except for one
instance which required a very small refactor.
This commit is contained in:
James Shubin
2024-02-25 15:35:25 -05:00
parent 8db41e7701
commit dd0e67540f
35 changed files with 125 additions and 131 deletions

View File

@@ -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.

View File

@@ -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")
}

View File

@@ -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")
}

View File

@@ -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)
}

View File

@@ -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")
}

View File

@@ -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,
}

View File

@@ -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")
}

View File

@@ -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

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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

View File

@@ -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")
}

View File

@@ -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
}

View File

@@ -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")
}

View File

@@ -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)

View File

@@ -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)
}

View File

@@ -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

View File

@@ -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")
}

View File

@@ -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
}

View File

@@ -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) },
})
}

View File

@@ -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")
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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)
}

View File

@@ -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
}

View File

@@ -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)
}

View File

@@ -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")
}
@@ -203,7 +203,7 @@ func inputMetadata(s string, fs engine.Fs) (*ParsedInput, error) {
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
b, err := io.ReadAll(fm) // doesn't need fs
if err != nil {
return nil, errwrap.Wrapf(err, "can't read in file: `%s`", m)
}
@@ -252,7 +252,7 @@ func inputMcl(s string, fs engine.Fs) (*ParsedInput, error) {
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
b, err := io.ReadAll(fm) // doesn't need fs
if err != nil {
return nil, errwrap.Wrapf(err, "can't read in file: `%s`", s)
}

View File

@@ -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")
}

View File

@@ -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)

View File

@@ -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!")

View File

@@ -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")
}

View File

@@ -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")
}

View File

@@ -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")
}

View File

@@ -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() {

View File

@@ -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 {