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,10 +1,10 @@
//go:build integration
// +build integration
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
@ -37,7 +37,7 @@ func (ckp certificateKeyPair) key() string {
}
func newSelfSignedCertificateKeyPair(subj string) (*certificateKeyPair, error) {
dir, err := ioutil.TempDir("", "osbuild-auth-tests-")
dir, err := os.MkdirTemp("", "osbuild-auth-tests-")
if err != nil {
return nil, fmt.Errorf("cannot create a temporary directory for the certificate: %v", err)
}
@ -79,7 +79,7 @@ func (c ca) key() string {
}
func newCA(subj string) (*ca, error) {
baseDir, err := ioutil.TempDir("", "osbuild-auth-tests-ca")
baseDir, err := os.MkdirTemp("", "osbuild-auth-tests-ca")
if err != nil {
return nil, fmt.Errorf("cannot create a temporary dir for a new CA: %v", err)
}
@ -139,7 +139,7 @@ func newCA(subj string) (*ca, error) {
}
func (c ca) newCertificateKeyPair(subj, extensions, addext string) (*certificateKeyPair, error) {
dir, err := ioutil.TempDir("", "osbuild-auth-tests-")
dir, err := os.MkdirTemp("", "osbuild-auth-tests-")
if err != nil {
return nil, fmt.Errorf("cannot create a temporary directory for the certificate: %v", err)
}

View file

@ -8,8 +8,8 @@ import (
"crypto/x509"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"os"
"testing"
"github.com/stretchr/testify/require"
@ -24,7 +24,7 @@ type connectionConfig struct {
}
func createTLSConfig(config *connectionConfig) (*tls.Config, error) {
caCertPEM, err := ioutil.ReadFile(config.CACertFile)
caCertPEM, err := os.ReadFile(config.CACertFile)
if err != nil {
return nil, err
}

View file

@ -6,7 +6,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"log"
"os"
"os/exec"
@ -160,7 +160,7 @@ func TestStatusCommands(t *testing.T) {
}
func TestSourcesCommands(t *testing.T) {
sources_toml, err := ioutil.TempFile("", "SOURCES-*.TOML")
sources_toml, err := os.CreateTemp("", "SOURCES-*.TOML")
require.NoErrorf(t, err, "Could not create temporary file: %v", err)
defer os.Remove(sources_toml.Name())
@ -317,7 +317,7 @@ func getLogs(t *testing.T, uuid uuid.UUID) string {
}
func pushBlueprint(t *testing.T, bp *blueprint.Blueprint) {
tmpfile, err := ioutil.TempFile("", "osbuild-test-")
tmpfile, err := os.CreateTemp("", "osbuild-test-")
require.Nilf(t, err, "Could not create temporary file: %v", err)
defer os.Remove(tmpfile.Name())
@ -386,10 +386,10 @@ func runComposer(t *testing.T, command ...string) []byte {
err = cmd.Start()
require.Nilf(t, err, "Could not start command: %v", err)
contents, err := ioutil.ReadAll(stdout)
contents, err := io.ReadAll(stdout)
require.NoError(t, err, "Could not read stdout from command")
errcontents, err := ioutil.ReadAll(stderr)
errcontents, err := io.ReadAll(stderr)
require.NoError(t, err, "Could not read stderr from command")
err = cmd.Wait()
@ -453,8 +453,8 @@ func NewTemporaryWorkDir(t *testing.T, pattern string) TemporaryWorkDir {
d.OldWorkDir, err = os.Getwd()
require.Nilf(t, err, "os.GetWd: %v", err)
d.Path, err = ioutil.TempDir("", pattern)
require.Nilf(t, err, "ioutil.TempDir: %v", err)
d.Path, err = os.MkdirTemp("", pattern)
require.Nilf(t, err, "os.MkdirTemp: %v", err)
err = os.Chdir(d.Path)
require.Nilf(t, err, "os.ChDir: %v", err)

View file

@ -6,7 +6,6 @@ import (
"crypto/x509"
"errors"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
@ -389,7 +388,7 @@ func createTLSConfig(c *connectionConfig) (*tls.Config, error) {
var roots *x509.CertPool
if c.CACertFile != "" {
caCertPEM, err := ioutil.ReadFile(c.CACertFile)
caCertPEM, err := os.ReadFile(c.CACertFile)
if err != nil {
return nil, err
}

View file

@ -10,7 +10,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
@ -574,7 +573,7 @@ func guessPipelineToExport(rawManifest json.RawMessage) string {
// tests the result
func runTestcase(t *testing.T, testcase testcaseStruct, store string) {
_ = os.Mkdir("/var/lib/osbuild-composer-tests", 0755)
outputDirectory, err := ioutil.TempDir("/var/lib/osbuild-composer-tests", "osbuild-image-tests-*")
outputDirectory, err := os.MkdirTemp("/var/lib/osbuild-composer-tests", "osbuild-image-tests-*")
require.NoError(t, err, "error creating temporary output directory")
defer func() {
@ -594,7 +593,7 @@ func runTestcase(t *testing.T, testcase testcaseStruct, store string) {
// getAllCases returns paths to all testcases in the testcase directory
func getAllCases() ([]string, error) {
cases, err := ioutil.ReadDir(constants.TestPaths.TestCasesDirectory)
cases, err := os.ReadDir(constants.TestPaths.TestCasesDirectory)
if err != nil {
return nil, fmt.Errorf("cannot list test cases: %v", err)
}
@ -615,7 +614,7 @@ func getAllCases() ([]string, error) {
// runTests opens, parses and runs all the specified testcases
func runTests(t *testing.T, cases []string) {
_ = os.Mkdir("/var/lib/osbuild-composer-tests", 0755)
store, err := ioutil.TempDir("/var/lib/osbuild-composer-tests", "osbuild-image-tests-*")
store, err := os.MkdirTemp("/var/lib/osbuild-composer-tests", "osbuild-image-tests-*")
require.NoError(t, err, "error creating temporary store")
defer func() {

View file

@ -3,6 +3,7 @@
// currently run as a "base test". Instead, it's run as a part of the
// koji.sh test because it needs a working Koji instance to pass.
//go:build integration
// +build integration
package main
@ -12,7 +13,6 @@ import (
"crypto/tls"
"crypto/x509"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
@ -37,14 +37,14 @@ func TestKojiRefund(t *testing.T) {
// use the self-signed certificate generated by run-koji-container
certPool := x509.NewCertPool()
cert, err := ioutil.ReadFile(shareDir + "/ca-crt.pem")
cert, err := os.ReadFile(shareDir + "/ca-crt.pem")
require.NoError(t, err)
ok := certPool.AppendCertsFromPEM(cert)
require.True(t, ok)
transport.TLSClientConfig = &tls.Config{
RootCAs: certPool,
RootCAs: certPool,
MinVersion: tls.VersionTLS12,
}
@ -98,14 +98,14 @@ func TestKojiImport(t *testing.T) {
// use the self-signed certificate generated by run-koji-container
certPool := x509.NewCertPool()
cert, err := ioutil.ReadFile(shareDir + "/ca-crt.pem")
cert, err := os.ReadFile(shareDir + "/ca-crt.pem")
require.NoError(t, err)
ok := certPool.AppendCertsFromPEM(cert)
require.True(t, ok)
transport.TLSClientConfig = &tls.Config{
RootCAs: certPool,
RootCAs: certPool,
MinVersion: tls.VersionTLS12,
}
@ -125,7 +125,7 @@ func TestKojiImport(t *testing.T) {
}()
// Create a random file
f, err := ioutil.TempFile("", "osbuild-koji-test-*.qcow2")
f, err := os.CreateTemp("", "osbuild-koji-test-*.qcow2")
require.NoError(t, err)
defer func() {
assert.NoError(t, f.Close())

View file

@ -4,10 +4,10 @@ import (
"encoding/base64"
"encoding/json"
"flag"
"io/ioutil"
"log"
"math/big"
"net/http"
"os"
"strings"
"time"
@ -46,7 +46,7 @@ func main() {
E string `json:"e"`
}
rsaPubBytes, err := ioutil.ReadFile(rsaPubPem)
rsaPubBytes, err := os.ReadFile(rsaPubPem)
if err != nil {
panic(err)
}
@ -116,7 +116,7 @@ func main() {
token := jwt.NewWithClaims(jwt.SigningMethodRS256, cc)
token.Header["kid"] = "key-id"
rsaPrivBytes, err := ioutil.ReadFile(rsaPem)
rsaPrivBytes, err := os.ReadFile(rsaPem)
if err != nil {
panic(err)
}

View file

@ -5,7 +5,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"path"
@ -97,7 +96,7 @@ func main() {
panic("Could not open compose request: " + err.Error())
}
}
file, err := ioutil.ReadAll(reader)
file, err := io.ReadAll(reader)
if err != nil {
panic("Could not read compose request: " + err.Error())
}

View file

@ -5,7 +5,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"path"
@ -63,7 +62,7 @@ func main() {
panic("Could not open path to image options: " + err.Error())
}
}
file, err := ioutil.ReadAll(reader)
file, err := io.ReadAll(reader)
if err != nil {
panic("Could not read image options: " + err.Error())
}

View file

@ -4,7 +4,7 @@ import (
"context"
"flag"
"fmt"
"io/ioutil"
"os"
"github.com/osbuild/osbuild-composer/internal/cloud/gcp"
"github.com/sirupsen/logrus"
@ -62,7 +62,7 @@ func main() {
var credentials []byte
if credentialsPath != "" {
var err error
credentials, err = ioutil.ReadFile(credentialsPath)
credentials, err = os.ReadFile(credentialsPath)
if err != nil {
logrus.Fatalf("[GCP] Error while reading credentials: %v", err)
}

View file

@ -3,12 +3,12 @@ package main
import (
"crypto/rand"
"fmt"
"github.com/osbuild/osbuild-composer/internal/upload/oci"
"github.com/spf13/cobra"
"io/ioutil"
"math"
"math/big"
"os"
"github.com/osbuild/osbuild-composer/internal/upload/oci"
"github.com/spf13/cobra"
)
var (
@ -77,7 +77,7 @@ func uploaderFromConfig() (oci.Uploader, error) {
return nil, fmt.Errorf("when suppling a private key the following args are mandatory as well:" +
" fingerprint, tenancy, region, and user-id")
}
pk, err := ioutil.ReadFile(privateKeyFile)
pk, err := os.ReadFile(privateKeyFile)
if err != nil {
return nil, fmt.Errorf("failed to read private key file %w", err)
}

View file

@ -4,7 +4,6 @@ import (
"context"
"crypto/rand"
"fmt"
"io/ioutil"
"math"
"math/big"
"net/url"
@ -295,7 +294,7 @@ func (impl *OSBuildJobImpl) Run(job worker.Job) error {
}
}()
outputDirectory, err = ioutil.TempDir(impl.Output, job.Id().String()+"-*")
outputDirectory, err = os.MkdirTemp(impl.Output, job.Id().String()+"-*")
if err != nil {
return fmt.Errorf("error creating temporary output directory: %v", err)
}
@ -440,7 +439,7 @@ func (impl *OSBuildJobImpl) Run(job worker.Job) error {
Datastore: targetOptions.Datastore,
}
tempDirectory, err := ioutil.TempDir(impl.Output, job.Id().String()+"-vmware-*")
tempDirectory, err := os.MkdirTemp(impl.Output, job.Id().String()+"-vmware-*")
if err != nil {
targetResult.TargetError = clienterrors.WorkerClientError(clienterrors.ErrorInvalidConfig, err.Error(), nil)
break

View file

@ -7,7 +7,6 @@ import (
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"path"
"strings"
@ -48,7 +47,7 @@ type JobImplementation interface {
}
func createTLSConfig(config *connectionConfig) (*tls.Config, error) {
caCertPEM, err := ioutil.ReadFile(config.CACertFile)
caCertPEM, err := os.ReadFile(config.CACertFile)
if err != nil {
return nil, err
}
@ -274,7 +273,7 @@ func main() {
token := ""
if config.Authentication.OfflineTokenPath != "" {
t, err := ioutil.ReadFile(config.Authentication.OfflineTokenPath)
t, err := os.ReadFile(config.Authentication.OfflineTokenPath)
if err != nil {
logrus.Fatalf("Could not read offline token: %v", err)
}
@ -283,7 +282,7 @@ func main() {
clientSecret := ""
if config.Authentication.ClientSecretPath != "" {
cs, err := ioutil.ReadFile(config.Authentication.ClientSecretPath)
cs, err := os.ReadFile(config.Authentication.ClientSecretPath)
if err != nil {
logrus.Fatalf("Could not read client secret: %v", err)
}