Skip to content

Commit

Permalink
Drop usage of io/ioutil
Browse files Browse the repository at this point in the history
`io/ioutil` has been deprecated since Go 1.16, see:
https://golang.org/doc/go1.16#ioutil

Signed-off-by: Hidde Beydals <hello@hidde.co>
  • Loading branch information
hiddeco committed Jul 27, 2021
1 parent 08bb991 commit 6cd78ba
Show file tree
Hide file tree
Showing 21 changed files with 70 additions and 85 deletions.
5 changes: 2 additions & 3 deletions controllers/bucket_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"crypto/sha1"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -196,7 +195,7 @@ func (r *BucketReconciler) reconcile(ctx context.Context, obj *sourcev1.Bucket)
}

// Create temp dir for the bucket objects
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("%s-%s-%s-", obj.Kind, obj.Namespace, obj.Name))
tmpDir, err := os.MkdirTemp("", fmt.Sprintf("%s-%s-%s-", obj.Kind, obj.Namespace, obj.Name))
if err != nil {
conditions.MarkFalse(obj, sourcev1.SourceAvailableCondition, sourcev1.StorageOperationFailedReason, "Failed to create temporary directory: %s", err)
return ctrl.Result{}, err
Expand Down Expand Up @@ -474,7 +473,7 @@ func (r *BucketReconciler) checksum(root string) (string, error) {
if !info.Mode().IsRegular() {
return nil
}
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions controllers/bucket_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ func TestBucketReconciler_reconcileSource(t *testing.T) {
Client: builder.Build(),
Storage: storage,
}
tmpDir, err := ioutil.TempDir("", "reconcile-bucket-source-")
tmpDir, err := os.MkdirTemp("", "reconcile-bucket-source-")
g.Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(tmpDir)

Expand Down Expand Up @@ -499,7 +499,7 @@ func TestBucketReconciler_reconcileArtifact(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)

tmpDir, err := ioutil.TempDir("", "reconcile-bucket-artifact-")
tmpDir, err := os.MkdirTemp("", "reconcile-bucket-artifact-")
g.Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(tmpDir)

Expand Down Expand Up @@ -561,7 +561,7 @@ func TestBucketReconciler_checksum(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
root, err := ioutil.TempDir("", "bucket-checksum-")
root, err := os.MkdirTemp("", "bucket-checksum-")
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions controllers/gitrepository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package controllers
import (
"context"
"fmt"
"io/ioutil"
"os"
"strings"
"time"
Expand Down Expand Up @@ -206,7 +205,7 @@ func (r *GitRepositoryReconciler) reconcile(ctx context.Context, obj *sourcev1.G
}

// Create temp dir for Git clone
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("%s-%s-%s-", obj.Kind, obj.Namespace, obj.Name))
tmpDir, err := os.MkdirTemp("", fmt.Sprintf("%s-%s-%s-", obj.Kind, obj.Namespace, obj.Name))
if err != nil {
conditions.MarkFalse(obj, sourcev1.SourceAvailableCondition, sourcev1.StorageOperationFailedReason, "Failed to create temporary directory: %s", err)
return ctrl.Result{}, err
Expand Down
9 changes: 4 additions & 5 deletions controllers/gitrepository_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package controllers

import (
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
Expand Down Expand Up @@ -385,7 +384,7 @@ func TestGitRepositoryReconciler_reconcileSource_authStrategy(t *testing.T) {
t.Skipf("Skipped for Git implementation %q", i)
}

tmpDir, err := ioutil.TempDir("", "auth-strategy-")
tmpDir, err := os.MkdirTemp("", "auth-strategy-")
g.Expect(err).To(BeNil())
defer os.RemoveAll(tmpDir)

Expand Down Expand Up @@ -527,7 +526,7 @@ func TestGitRepositoryReconciler_reconcileSource_checkoutStrategy(t *testing.T)
t.Run(i, func(t *testing.T) {
g := NewWithT(t)

tmpDir, err := ioutil.TempDir("", "checkout-strategy-")
tmpDir, err := os.MkdirTemp("", "checkout-strategy-")
g.Expect(err).NotTo(HaveOccurred())

obj := obj.DeepCopy()
Expand Down Expand Up @@ -832,7 +831,7 @@ func TestGitRepositoryReconciler_reconcileInclude(t *testing.T) {
obj.Spec.Include = append(obj.Spec.Include, incl)
}

tmpDir, err := ioutil.TempDir("", "include-")
tmpDir, err := os.MkdirTemp("", "include-")
g.Expect(err).NotTo(HaveOccurred())

var artifacts artifactSet
Expand Down Expand Up @@ -1081,7 +1080,7 @@ func commitFromFixture(repo *gogit.Repository, fixture string) error {
return fs.MkdirAll(fs.Join(path[len(fixture):]), info.Mode())
}

fileBytes, err := ioutil.ReadFile(path)
fileBytes, err := os.ReadFile(path)
if err != nil {
return err
}
Expand Down
7 changes: 3 additions & 4 deletions controllers/helmchart_controller_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package controllers
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -84,7 +83,7 @@ func (r *HelmChartReconciler) reconcileChart(ctx context.Context, obj *sourcev1.

// We need to (re)package the chart
if conditions.IsTrue(obj, sourcev1.DependenciesBuildCondition) || conditions.IsTrue(obj, sourcev1.ValuesFilesMergedCondition) {
tmpDir, err := ioutil.TempDir("", "helm-chart-pkg-")
tmpDir, err := os.MkdirTemp("", "helm-chart-pkg-")
if err != nil {
conditions.MarkFalse(obj, sourcev1.ChartPackagedCondition, sourcev1.StorageOperationFailedReason, "Could not create temporary directory for packaging operation: %s", err.Error())
return ctrl.Result{}, err
Expand Down Expand Up @@ -146,7 +145,7 @@ func (r *HelmChartReconciler) buildChartDependencies(ctx context.Context, obj *s
Chart: chart,
}

tmpDir, err := ioutil.TempDir("", "build-chart-deps-")
tmpDir, err := os.MkdirTemp("", "build-chart-deps-")
if err != nil {
conditions.MarkFalse(obj, sourcev1.ChartReconciled, sourcev1.StorageOperationFailedReason, "Could not create temporary directory for dependency credentials: %s", err.Error())
return ctrl.Result{}, err
Expand Down Expand Up @@ -347,7 +346,7 @@ func mergeFileValues(dir string, valuesFiles []string) (map[string]interface{},
return nil, fmt.Errorf("invalid values file path %q", p)
}

b, err := ioutil.ReadFile(secureP)
b, err := os.ReadFile(secureP)
if err != nil {
return nil, fmt.Errorf("could not read values from file %q: %w", p, err)
}
Expand Down
7 changes: 3 additions & 4 deletions controllers/helmchart_controller_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/url"
"os"

Expand Down Expand Up @@ -105,7 +104,7 @@ func (r *HelmChartReconciler) reconcileFromHelmRepository(ctx context.Context, o
}

// Get client options from secret
tmpDir, err := ioutil.TempDir("", "helm-client-")
tmpDir, err := os.MkdirTemp("", "helm-client-")
if err != nil {
conditions.MarkFalse(obj, sourcev1.SourceAvailableCondition, sourcev1.AuthenticationFailedReason, "Could not create temporary directory for : %s", err.Error())
return ctrl.Result{}, err
Expand Down Expand Up @@ -151,7 +150,7 @@ func (r *HelmChartReconciler) reconcileFromHelmRepository(ctx context.Context, o
}

// Create a new temporary file for the chart and download it
f, err := ioutil.TempFile("", fmt.Sprintf("%s-%s-", obj.Name, obj.Namespace))
f, err := os.CreateTemp("", fmt.Sprintf("%s-%s-", obj.Name, obj.Namespace))
if err != nil {
conditions.MarkFalse(obj, sourcev1.SourceAvailableCondition, sourcev1.ChartPullFailedReason, "Chart download for %q version %q failed: %s", obj.Spec.Chart, chartVer.Version, err.Error())
return ctrl.Result{}, err
Expand Down Expand Up @@ -182,7 +181,7 @@ func (r *HelmChartReconciler) reconcileFromTarballArtifact(ctx context.Context,
return ctrl.Result{}, err
}

dir, err := ioutil.TempDir("", fmt.Sprintf("%s-%s-", obj.Name, obj.Namespace))
dir, err := os.MkdirTemp("", fmt.Sprintf("%s-%s-", obj.Name, obj.Namespace))
if err != nil {
conditions.MarkFalse(obj, sourcev1.SourceAvailableCondition, sourcev1.StorageOperationFailedReason, "Could not create temporary working directory: %s", err.Error())
return ctrl.Result{}, err
Expand Down
3 changes: 1 addition & 2 deletions controllers/helmrepository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/url"
"os"
"time"
Expand Down Expand Up @@ -265,7 +264,7 @@ func (r *HelmRepositoryReconciler) reconcileSource(ctx context.Context, obj *sou
}

// Get client options from secret
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("%s-%s-source-", obj.Name, obj.Namespace))
tmpDir, err := os.MkdirTemp("", fmt.Sprintf("%s-%s-source-", obj.Name, obj.Namespace))
if err != nil {
conditions.MarkFalse(obj, sourcev1.SourceAvailableCondition, sourcev1.StorageOperationFailedReason, "Could not create temporary directory for credentials: %s", err.Error())
r.Events.Event(ctx, obj, events.EventSeverityError, sourcev1.AuthenticationFailedReason, conditions.Get(obj, sourcev1.SourceAvailableCondition).Message)
Expand Down
9 changes: 4 additions & 5 deletions controllers/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"fmt"
"hash"
"io"
"io/ioutil"
"net/url"
"os"
"path/filepath"
Expand Down Expand Up @@ -179,7 +178,7 @@ func (s *Storage) Archive(artifact *sourcev1.Artifact, dir string, filter Archiv
}

localPath := s.LocalPath(*artifact)
tf, err := ioutil.TempFile(filepath.Split(localPath))
tf, err := os.CreateTemp(filepath.Split(localPath))
if err != nil {
return err
}
Expand Down Expand Up @@ -277,7 +276,7 @@ func (s *Storage) Archive(artifact *sourcev1.Artifact, dir string, filter Archiv
// If successful, it sets the checksum and last update time on the artifact.
func (s *Storage) AtomicWriteFile(artifact *sourcev1.Artifact, reader io.Reader, mode os.FileMode) (err error) {
localPath := s.LocalPath(*artifact)
tf, err := ioutil.TempFile(filepath.Split(localPath))
tf, err := os.CreateTemp(filepath.Split(localPath))
if err != nil {
return err
}
Expand Down Expand Up @@ -316,7 +315,7 @@ func (s *Storage) AtomicWriteFile(artifact *sourcev1.Artifact, reader io.Reader,
// If successful, it sets the checksum and last update time on the artifact.
func (s *Storage) Copy(artifact *sourcev1.Artifact, reader io.Reader) (err error) {
localPath := s.LocalPath(*artifact)
tf, err := ioutil.TempFile(filepath.Split(localPath))
tf, err := os.CreateTemp(filepath.Split(localPath))
if err != nil {
return err
}
Expand Down Expand Up @@ -363,7 +362,7 @@ func (s *Storage) CopyFromPath(artifact *sourcev1.Artifact, path string) (err er
// given path.
func (s *Storage) CopyToPath(artifact *sourcev1.Artifact, subPath, toPath string) error {
// create a tmp directory to store artifact
tmp, err := ioutil.TempDir("", "flux-include-")
tmp, err := os.MkdirTemp("", "flux-include-")
if err != nil {
return err
}
Expand Down
9 changes: 4 additions & 5 deletions controllers/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
Expand All @@ -34,7 +33,7 @@ import (
)

func createStoragePath() (string, error) {
return ioutil.TempDir("", "")
return os.MkdirTemp("", "")
}

func cleanupStoragePath(dir string) func() {
Expand All @@ -52,7 +51,7 @@ func TestStorageConstructor(t *testing.T) {
t.Fatal("nonexistent path was allowable in storage constructor")
}

f, err := ioutil.TempFile(dir, "")
f, err := os.CreateTemp(dir, "")
if err != nil {
t.Fatalf("while creating temporary file: %v", err)
}
Expand Down Expand Up @@ -124,7 +123,7 @@ func TestStorage_Archive(t *testing.T) {
os.RemoveAll(dir)
}
}()
dir, err = ioutil.TempDir("", "archive-test-files-")
dir, err = os.MkdirTemp("", "archive-test-files-")
if err != nil {
return
}
Expand Down Expand Up @@ -244,7 +243,7 @@ func TestStorage_Archive(t *testing.T) {

func TestStorageRemoveAllButCurrent(t *testing.T) {
t.Run("bad directory in archive", func(t *testing.T) {
dir, err := ioutil.TempDir("", "")
dir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatal(err)
}
Expand Down
7 changes: 3 additions & 4 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package controllers

import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
Expand Down Expand Up @@ -154,15 +153,15 @@ func TestMain(m *testing.M) {

func initTestTLS() {
var err error
tlsPublicKey, err = ioutil.ReadFile("testdata/certs/server.pem")
tlsPublicKey, err = os.ReadFile("testdata/certs/server.pem")
if err != nil {
panic(err)
}
tlsPrivateKey, err = ioutil.ReadFile("testdata/certs/server-key.pem")
tlsPrivateKey, err = os.ReadFile("testdata/certs/server-key.pem")
if err != nil {
panic(err)
}
tlsCA, err = ioutil.ReadFile("testdata/certs/ca.pem")
tlsCA, err = os.ReadFile("testdata/certs/ca.pem")
if err != nil {
panic(err)
}
Expand Down
3 changes: 1 addition & 2 deletions internal/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -92,7 +91,7 @@ func CopyDir(src, dst string) error {
return fmt.Errorf("cannot mkdir %s: %w", dst, err)
}

entries, err := ioutil.ReadDir(src)
entries, err := os.ReadDir(src)
if err != nil {
return fmt.Errorf("cannot read directory %s: %w", dst, err)
}
Expand Down
Loading

0 comments on commit 6cd78ba

Please sign in to comment.