Update deprecated io/ioutil functions

ioutil has been deprecated since go 1.16, this fixes all of the
deprecated functions we are using:

ioutil.ReadFile -> os.ReadFile
ioutil.ReadAll -> io.ReadAll
ioutil.WriteFile -> os.WriteFile
ioutil.TempFile -> os.CreateTemp
ioutil.TempDir -> os.MkdirTemp

All of the above are a simple name change, the function arguments and
results are exactly the same as before.

ioutil.ReadDir -> os.ReadDir

now returns a os.DirEntry but the IsDir and Name functions work the
same. The difference is that the FileInfo must be retrieved with the
Info() function which can also return an error.

These were identified by running:
golangci-lint run --build-tags=integration ./...
This commit is contained in:
Brian C. Lane 2023-03-06 11:07:30 -08:00 committed by Brian C. Lane
parent 0e4a5b34b2
commit 7a4bb863dd
37 changed files with 113 additions and 126 deletions

View file

@ -1,3 +1,4 @@
//go:build integration
// +build integration
package boot
@ -6,7 +7,6 @@ import (
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"os"
"github.com/aws/aws-sdk-go/aws"
@ -60,7 +60,7 @@ func encodeBase64(input string) string {
// CreateUserData creates cloud-init's user-data that contains user redhat with
// the specified public key
func CreateUserData(publicKeyFile string) (string, error) {
publicKey, err := ioutil.ReadFile(publicKeyFile)
publicKey, err := os.ReadFile(publicKeyFile)
if err != nil {
return "", fmt.Errorf("cannot read the public key: %v", err)
}

View file

@ -1,3 +1,4 @@
//go:build integration
// +build integration
package azuretest
@ -6,7 +7,6 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
@ -137,7 +137,7 @@ func DeleteImageFromAzure(c *azureCredentials, imageName string) error {
// readPublicKey reads the public key from a file and returns it as a string
func readPublicKey(publicKeyFile string) (string, error) {
publicKey, err := ioutil.ReadFile(publicKeyFile)
publicKey, err := os.ReadFile(publicKeyFile)
if err != nil {
return "", fmt.Errorf("cannot read the public key file: %v", err)
}

View file

@ -1,3 +1,4 @@
//go:build integration
// +build integration
package boot
@ -5,7 +6,6 @@ package boot
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
@ -38,9 +38,9 @@ func WithNetworkNamespace(f func(ns NetNS) error) error {
}
// withTempFile provides the function f with a new temporary file
// dir and pattern parameters have the same semantics as in ioutil.TempFile
// dir and pattern parameters have the same semantics as in os.CreateTemp
func withTempFile(dir, pattern string, f func(file *os.File) error) error {
tempFile, err := ioutil.TempFile(dir, pattern)
tempFile, err := os.CreateTemp(dir, pattern)
if err != nil {
return fmt.Errorf("cannot create the temporary file: %v", err)
}
@ -56,7 +56,7 @@ func withTempFile(dir, pattern string, f func(file *os.File) error) error {
}
func withTempDir(dir, pattern string, f func(dir string) error) error {
tempDir, err := ioutil.TempDir(dir, pattern)
tempDir, err := os.MkdirTemp(dir, pattern)
if err != nil {
return fmt.Errorf("cannot create the temporary directory %v", err)
}

View file

@ -1,3 +1,4 @@
//go:build integration
// +build integration
package boot
@ -5,7 +6,6 @@ package boot
import (
"context"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
@ -45,7 +45,7 @@ func newNetworkNamespace() (NetNS, error) {
}
}
f, err := ioutil.TempFile(netnsDir, "osbuild-composer-namespace")
f, err := os.CreateTemp(netnsDir, "osbuild-composer-namespace")
if err != nil {
return "", fmt.Errorf("cannot create a tempfile: %v", err)
}
@ -90,7 +90,7 @@ func newNetworkNamespace() (NetNS, error) {
if err != nil {
return "", fmt.Errorf("cannot set up a loopback device in the new namespace: %v", err)
}
// There's no potential command injection vector here
/* #nosec G204 */
cmd = exec.Command("mount", "-o", "bind", "/proc/self/ns/net", f.Name())
@ -134,7 +134,7 @@ func (n NetNS) Path() string {
// Delete deletes the namespaces
func (n NetNS) Delete() error {
// There's no potential command injection vector here
/* #nosec G204 */
/* #nosec G204 */
cmd := exec.Command("umount", n.Path())
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout

View file

@ -1,3 +1,4 @@
//go:build integration
// +build integration
package vmwaretest
@ -5,7 +6,7 @@ package vmwaretest
import (
"errors"
"fmt"
"io/ioutil"
"io"
"os"
"path/filepath"
"strings"
@ -130,7 +131,7 @@ func runWithStdout(args []string) (string, int) {
retcode := cli.Run(args)
w.Close()
out, _ := ioutil.ReadAll(r)
out, _ := io.ReadAll(r)
os.Stdout = oldStdout
return strings.TrimSpace(string(out)), retcode