Skip to content

Commit f636875

Browse files
committed
Drop usage of io/ioutil
`io/ioutil` has been deprecated since Go 1.16, see: https://golang.org/doc/go1.16#ioutil Signed-off-by: Hidde Beydals <[email protected]>
1 parent 08bb991 commit f636875

21 files changed

+71
-87
lines changed

controllers/bucket_controller.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"context"
2121
"crypto/sha1"
2222
"fmt"
23-
"io/ioutil"
2423
"os"
2524
"path/filepath"
2625
"strings"
@@ -196,7 +195,7 @@ func (r *BucketReconciler) reconcile(ctx context.Context, obj *sourcev1.Bucket)
196195
}
197196

198197
// Create temp dir for the bucket objects
199-
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("%s-%s-%s-", obj.Kind, obj.Namespace, obj.Name))
198+
tmpDir, err := os.MkdirTemp("", fmt.Sprintf("%s-%s-%s-", obj.Kind, obj.Namespace, obj.Name))
200199
if err != nil {
201200
conditions.MarkFalse(obj, sourcev1.SourceAvailableCondition, sourcev1.StorageOperationFailedReason, "Failed to create temporary directory: %s", err)
202201
return ctrl.Result{}, err
@@ -474,7 +473,7 @@ func (r *BucketReconciler) checksum(root string) (string, error) {
474473
if !info.Mode().IsRegular() {
475474
return nil
476475
}
477-
data, err := ioutil.ReadFile(path)
476+
data, err := os.ReadFile(path)
478477
if err != nil {
479478
return err
480479
}

controllers/bucket_controller_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"context"
2121
"crypto/md5"
2222
"fmt"
23-
"io/ioutil"
2423
"net/http"
2524
"net/http/httptest"
2625
"net/url"
@@ -397,7 +396,7 @@ func TestBucketReconciler_reconcileSource(t *testing.T) {
397396
Client: builder.Build(),
398397
Storage: storage,
399398
}
400-
tmpDir, err := ioutil.TempDir("", "reconcile-bucket-source-")
399+
tmpDir, err := os.MkdirTemp("", "reconcile-bucket-source-")
401400
g.Expect(err).ToNot(HaveOccurred())
402401
defer os.RemoveAll(tmpDir)
403402

@@ -499,7 +498,7 @@ func TestBucketReconciler_reconcileArtifact(t *testing.T) {
499498
t.Run(tt.name, func(t *testing.T) {
500499
g := NewWithT(t)
501500

502-
tmpDir, err := ioutil.TempDir("", "reconcile-bucket-artifact-")
501+
tmpDir, err := os.MkdirTemp("", "reconcile-bucket-artifact-")
503502
g.Expect(err).ToNot(HaveOccurred())
504503
defer os.RemoveAll(tmpDir)
505504

@@ -561,7 +560,7 @@ func TestBucketReconciler_checksum(t *testing.T) {
561560
}
562561
for _, tt := range tests {
563562
t.Run(tt.name, func(t *testing.T) {
564-
root, err := ioutil.TempDir("", "bucket-checksum-")
563+
root, err := os.MkdirTemp("", "bucket-checksum-")
565564
if err != nil {
566565
t.Fatal(err)
567566
}
@@ -588,7 +587,7 @@ func mockFile(root, path, content string) error {
588587
if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
589588
panic(err)
590589
}
591-
if err := ioutil.WriteFile(filePath, []byte(content), 0644); err != nil {
590+
if err := os.WriteFile(filePath, []byte(content), 0644); err != nil {
592591
panic(err)
593592
}
594593
return nil

controllers/gitrepository_controller.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package controllers
1919
import (
2020
"context"
2121
"fmt"
22-
"io/ioutil"
2322
"os"
2423
"strings"
2524
"time"
@@ -206,7 +205,7 @@ func (r *GitRepositoryReconciler) reconcile(ctx context.Context, obj *sourcev1.G
206205
}
207206

208207
// Create temp dir for Git clone
209-
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("%s-%s-%s-", obj.Kind, obj.Namespace, obj.Name))
208+
tmpDir, err := os.MkdirTemp("", fmt.Sprintf("%s-%s-%s-", obj.Kind, obj.Namespace, obj.Name))
210209
if err != nil {
211210
conditions.MarkFalse(obj, sourcev1.SourceAvailableCondition, sourcev1.StorageOperationFailedReason, "Failed to create temporary directory: %s", err)
212211
return ctrl.Result{}, err

controllers/gitrepository_controller_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package controllers
1818

1919
import (
2020
"fmt"
21-
"io/ioutil"
2221
"net/url"
2322
"os"
2423
"path/filepath"
@@ -385,7 +384,7 @@ func TestGitRepositoryReconciler_reconcileSource_authStrategy(t *testing.T) {
385384
t.Skipf("Skipped for Git implementation %q", i)
386385
}
387386

388-
tmpDir, err := ioutil.TempDir("", "auth-strategy-")
387+
tmpDir, err := os.MkdirTemp("", "auth-strategy-")
389388
g.Expect(err).To(BeNil())
390389
defer os.RemoveAll(tmpDir)
391390

@@ -527,7 +526,7 @@ func TestGitRepositoryReconciler_reconcileSource_checkoutStrategy(t *testing.T)
527526
t.Run(i, func(t *testing.T) {
528527
g := NewWithT(t)
529528

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

533532
obj := obj.DeepCopy()
@@ -832,7 +831,7 @@ func TestGitRepositoryReconciler_reconcileInclude(t *testing.T) {
832831
obj.Spec.Include = append(obj.Spec.Include, incl)
833832
}
834833

835-
tmpDir, err := ioutil.TempDir("", "include-")
834+
tmpDir, err := os.MkdirTemp("", "include-")
836835
g.Expect(err).NotTo(HaveOccurred())
837836

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

1084-
fileBytes, err := ioutil.ReadFile(path)
1083+
fileBytes, err := os.ReadFile(path)
10851084
if err != nil {
10861085
return err
10871086
}

controllers/helmchart_controller_chart.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package controllers
1919
import (
2020
"context"
2121
"fmt"
22-
"io/ioutil"
2322
"os"
2423
"path/filepath"
2524
"strings"
@@ -84,7 +83,7 @@ func (r *HelmChartReconciler) reconcileChart(ctx context.Context, obj *sourcev1.
8483

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

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

350-
b, err := ioutil.ReadFile(secureP)
349+
b, err := os.ReadFile(secureP)
351350
if err != nil {
352351
return nil, fmt.Errorf("could not read values from file %q: %w", p, err)
353352
}

controllers/helmchart_controller_source.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"errors"
2222
"fmt"
2323
"io"
24-
"io/ioutil"
2524
"net/url"
2625
"os"
2726

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

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

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

185-
dir, err := ioutil.TempDir("", fmt.Sprintf("%s-%s-", obj.Name, obj.Namespace))
184+
dir, err := os.MkdirTemp("", fmt.Sprintf("%s-%s-", obj.Name, obj.Namespace))
186185
if err != nil {
187186
conditions.MarkFalse(obj, sourcev1.SourceAvailableCondition, sourcev1.StorageOperationFailedReason, "Could not create temporary working directory: %s", err.Error())
188187
return ctrl.Result{}, err

controllers/helmrepository_controller.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"bytes"
2121
"context"
2222
"fmt"
23-
"io/ioutil"
2423
"net/url"
2524
"os"
2625
"time"
@@ -265,7 +264,7 @@ func (r *HelmRepositoryReconciler) reconcileSource(ctx context.Context, obj *sou
265264
}
266265

267266
// Get client options from secret
268-
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("%s-%s-source-", obj.Name, obj.Namespace))
267+
tmpDir, err := os.MkdirTemp("", fmt.Sprintf("%s-%s-source-", obj.Name, obj.Namespace))
269268
if err != nil {
270269
conditions.MarkFalse(obj, sourcev1.SourceAvailableCondition, sourcev1.StorageOperationFailedReason, "Could not create temporary directory for credentials: %s", err.Error())
271270
r.Events.Event(ctx, obj, events.EventSeverityError, sourcev1.AuthenticationFailedReason, conditions.Get(obj, sourcev1.SourceAvailableCondition).Message)

controllers/storage.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"fmt"
2424
"hash"
2525
"io"
26-
"io/ioutil"
2726
"net/url"
2827
"os"
2928
"path/filepath"
@@ -179,7 +178,7 @@ func (s *Storage) Archive(artifact *sourcev1.Artifact, dir string, filter Archiv
179178
}
180179

181180
localPath := s.LocalPath(*artifact)
182-
tf, err := ioutil.TempFile(filepath.Split(localPath))
181+
tf, err := os.CreateTemp(filepath.Split(localPath))
183182
if err != nil {
184183
return err
185184
}
@@ -277,7 +276,7 @@ func (s *Storage) Archive(artifact *sourcev1.Artifact, dir string, filter Archiv
277276
// If successful, it sets the checksum and last update time on the artifact.
278277
func (s *Storage) AtomicWriteFile(artifact *sourcev1.Artifact, reader io.Reader, mode os.FileMode) (err error) {
279278
localPath := s.LocalPath(*artifact)
280-
tf, err := ioutil.TempFile(filepath.Split(localPath))
279+
tf, err := os.CreateTemp(filepath.Split(localPath))
281280
if err != nil {
282281
return err
283282
}
@@ -316,7 +315,7 @@ func (s *Storage) AtomicWriteFile(artifact *sourcev1.Artifact, reader io.Reader,
316315
// If successful, it sets the checksum and last update time on the artifact.
317316
func (s *Storage) Copy(artifact *sourcev1.Artifact, reader io.Reader) (err error) {
318317
localPath := s.LocalPath(*artifact)
319-
tf, err := ioutil.TempFile(filepath.Split(localPath))
318+
tf, err := os.CreateTemp(filepath.Split(localPath))
320319
if err != nil {
321320
return err
322321
}
@@ -363,7 +362,7 @@ func (s *Storage) CopyFromPath(artifact *sourcev1.Artifact, path string) (err er
363362
// given path.
364363
func (s *Storage) CopyToPath(artifact *sourcev1.Artifact, subPath, toPath string) error {
365364
// create a tmp directory to store artifact
366-
tmp, err := ioutil.TempDir("", "flux-include-")
365+
tmp, err := os.MkdirTemp("", "flux-include-")
367366
if err != nil {
368367
return err
369368
}

controllers/storage_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"compress/gzip"
2222
"fmt"
2323
"io"
24-
"io/ioutil"
2524
"os"
2625
"path"
2726
"path/filepath"
@@ -34,7 +33,7 @@ import (
3433
)
3534

3635
func createStoragePath() (string, error) {
37-
return ioutil.TempDir("", "")
36+
return os.MkdirTemp("", "")
3837
}
3938

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

55-
f, err := ioutil.TempFile(dir, "")
54+
f, err := os.CreateTemp(dir, "")
5655
if err != nil {
5756
t.Fatalf("while creating temporary file: %v", err)
5857
}
@@ -124,7 +123,7 @@ func TestStorage_Archive(t *testing.T) {
124123
os.RemoveAll(dir)
125124
}
126125
}()
127-
dir, err = ioutil.TempDir("", "archive-test-files-")
126+
dir, err = os.MkdirTemp("", "archive-test-files-")
128127
if err != nil {
129128
return
130129
}
@@ -244,7 +243,7 @@ func TestStorage_Archive(t *testing.T) {
244243

245244
func TestStorageRemoveAllButCurrent(t *testing.T) {
246245
t.Run("bad directory in archive", func(t *testing.T) {
247-
dir, err := ioutil.TempDir("", "")
246+
dir, err := os.MkdirTemp("", "")
248247
if err != nil {
249248
t.Fatal(err)
250249
}

controllers/suite_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package controllers
1818

1919
import (
2020
"fmt"
21-
"io/ioutil"
2221
"math/rand"
2322
"os"
2423
"path/filepath"
@@ -154,15 +153,15 @@ func TestMain(m *testing.M) {
154153

155154
func initTestTLS() {
156155
var err error
157-
tlsPublicKey, err = ioutil.ReadFile("testdata/certs/server.pem")
156+
tlsPublicKey, err = os.ReadFile("testdata/certs/server.pem")
158157
if err != nil {
159158
panic(err)
160159
}
161-
tlsPrivateKey, err = ioutil.ReadFile("testdata/certs/server-key.pem")
160+
tlsPrivateKey, err = os.ReadFile("testdata/certs/server-key.pem")
162161
if err != nil {
163162
panic(err)
164163
}
165-
tlsCA, err = ioutil.ReadFile("testdata/certs/ca.pem")
164+
tlsCA, err = os.ReadFile("testdata/certs/ca.pem")
166165
if err != nil {
167166
panic(err)
168167
}

internal/fs/fs.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"errors"
99
"fmt"
1010
"io"
11-
"io/ioutil"
1211
"os"
1312
"path/filepath"
1413
"runtime"
@@ -92,7 +91,7 @@ func CopyDir(src, dst string) error {
9291
return fmt.Errorf("cannot mkdir %s: %w", dst, err)
9392
}
9493

95-
entries, err := ioutil.ReadDir(src)
94+
entries, err := os.ReadDir(src)
9695
if err != nil {
9796
return fmt.Errorf("cannot read directory %s: %w", dst, err)
9897
}

0 commit comments

Comments
 (0)