Skip to content

Commit 669d8ae

Browse files
JustinKuliopenshift-merge-robot
authored andcommitted
Replace usages of deprecated ioutil
Signed-off-by: Justin Kulikauskas <[email protected]>
1 parent 34ca695 commit 669d8ae

File tree

5 files changed

+29
-32
lines changed

5 files changed

+29
-32
lines changed

cmd/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"bytes"
55
"fmt"
6-
"io/ioutil"
76
"os"
87

98
"github.com/spf13/pflag"
@@ -61,7 +60,7 @@ func processGeneratorConfig(filePath string) []byte {
6160
p := internal.Plugin{}
6261

6362
// #nosec G304
64-
fileData, err := ioutil.ReadFile(filePath)
63+
fileData, err := os.ReadFile(filePath)
6564
if err != nil {
6665
errorAndExit("failed to read file '%s': %s", filePath, err)
6766
}

internal/plugin_config_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package internal
33

44
import (
55
"fmt"
6-
"io/ioutil"
6+
"os"
77
"path"
88
"path/filepath"
99
"testing"
@@ -24,7 +24,7 @@ data:
2424
game.properties: enemies=potato
2525
`
2626

27-
err := ioutil.WriteFile(manifestsPath, []byte(yamlContent), 0o666)
27+
err := os.WriteFile(manifestsPath, []byte(yamlContent), 0o666)
2828
if err != nil {
2929
t.Fatalf("Failed to write %s", manifestsPath)
3030
}
@@ -48,7 +48,7 @@ spec:
4848
maxClusterRoleBindingUsers: 5
4949
`
5050

51-
err := ioutil.WriteFile(manifestsPath, []byte(yamlContent), 0o666)
51+
err := os.WriteFile(manifestsPath, []byte(yamlContent), 0o666)
5252
if err != nil {
5353
t.Fatalf("Failed to write %s", manifestsPath)
5454
}

internal/plugin_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package internal
44
import (
55
"encoding/json"
66
"fmt"
7-
"io/ioutil"
7+
"os"
88
"path"
99
"path/filepath"
1010
"reflect"
@@ -1126,7 +1126,7 @@ func TestCreatePolicyInvalidYAML(t *testing.T) {
11261126
tmpDir := t.TempDir()
11271127
manifestPath := path.Join(tmpDir, "configmap.yaml")
11281128

1129-
err := ioutil.WriteFile(manifestPath, []byte("$ not Yaml!"), 0o666)
1129+
err := os.WriteFile(manifestPath, []byte("$ not Yaml!"), 0o666)
11301130
if err != nil {
11311131
t.Fatalf("Failed to create %s: %v", manifestPath, err)
11321132
}
@@ -1165,7 +1165,7 @@ metadata:
11651165
name: policy-limitclusteradmin-example
11661166
`
11671167

1168-
err := ioutil.WriteFile(manifestPath, []byte(yamlContent), 0o666)
1168+
err := os.WriteFile(manifestPath, []byte(yamlContent), 0o666)
11691169
if err != nil {
11701170
t.Fatalf("Failed to create %s: %v", manifestPath, err)
11711171
}
@@ -1407,7 +1407,7 @@ func plPathHelper(t *testing.T, plrYAML string, usingPlR bool) (*Plugin, string)
14071407
plrPath := path.Join(tmpDir, "pl.yaml")
14081408
plrYAML = strings.TrimPrefix(plrYAML, "\n")
14091409

1410-
err := ioutil.WriteFile(plrPath, []byte(plrYAML), 0o666)
1410+
err := os.WriteFile(plrPath, []byte(plrYAML), 0o666)
14111411
if err != nil {
14121412
t.Fatal(err.Error())
14131413
}

internal/utils.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"errors"
77
"fmt"
88
"io"
9-
"io/ioutil"
109
"os"
1110
"path"
1211
"path/filepath"
@@ -40,7 +39,7 @@ func getManifests(policyConf *types.PolicyConfig) ([][]map[string]interface{}, e
4039
resolvedFiles := []string{}
4140

4241
if manifestPathInfo.IsDir() {
43-
files, err := ioutil.ReadDir(manifest.Path)
42+
files, err := os.ReadDir(manifest.Path)
4443
if err != nil {
4544
return nil, readErr
4645
}
@@ -405,7 +404,7 @@ func handleExpanders(
405404
// be returned.
406405
func unmarshalManifestFile(manifestPath string) ([]map[string]interface{}, error) {
407406
// #nosec G304
408-
manifestBytes, err := ioutil.ReadFile(manifestPath)
407+
manifestBytes, err := os.ReadFile(manifestPath)
409408
if err != nil {
410409
return nil, fmt.Errorf("failed to read the manifest file %s", manifestPath)
411410
}

internal/utils_test.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package internal
33

44
import (
55
"fmt"
6-
"io/ioutil"
76
"os"
87
"path"
98
"path/filepath"
@@ -128,7 +127,7 @@ data:
128127
enemy,
129128
)
130129

131-
err := ioutil.WriteFile(manifestPath, []byte(manifestYAML), 0o666)
130+
err := os.WriteFile(manifestPath, []byte(manifestYAML), 0o666)
132131
if err != nil {
133132
t.Fatalf("Failed to write %s", manifestPath)
134133
}
@@ -160,7 +159,7 @@ data:
160159
// template
161160
bogusFilePath := path.Join(tmpDir, "README.md")
162161

163-
err := ioutil.WriteFile(bogusFilePath, []byte("# My Manifests"), 0o666)
162+
err := os.WriteFile(bogusFilePath, []byte("# My Manifests"), 0o666)
164163
if err != nil {
165164
t.Fatalf("Failed to write %s", bogusFilePath)
166165
}
@@ -291,7 +290,7 @@ data:
291290
i, enemy,
292291
)
293292

294-
err := ioutil.WriteFile(manifestPath, []byte(manifestYAML), 0o666)
293+
err := os.WriteFile(manifestPath, []byte(manifestYAML), 0o666)
295294
if err != nil {
296295
t.Fatalf("Failed to write %s", manifestPath)
297296
}
@@ -314,7 +313,7 @@ resources:
314313
}
315314

316315
for kustomizePath, kustomizeYAML := range kustomizeManifests {
317-
err := ioutil.WriteFile(kustomizePath, []byte(kustomizeYAML), 0o666)
316+
err := os.WriteFile(kustomizePath, []byte(kustomizeYAML), 0o666)
318317
if err != nil {
319318
t.Fatalf("Failed to write %s", kustomizePath)
320319
}
@@ -331,7 +330,7 @@ resources:
331330
// Write a bogus file to verify it is not picked up when creating the policy template
332331
bogusFilePath := path.Join(kustomizeDir, "this-other-file.yaml")
333332

334-
err = ioutil.WriteFile(bogusFilePath, []byte("# My Manifests"), 0o666)
333+
err = os.WriteFile(bogusFilePath, []byte("# My Manifests"), 0o666)
335334
if err != nil {
336335
t.Fatalf("Failed to write %s", bogusFilePath)
337336
}
@@ -448,7 +447,7 @@ data:
448447
enemy,
449448
)
450449

451-
err := ioutil.WriteFile(manifestPath, []byte(manifestYAML), 0o666)
450+
err := os.WriteFile(manifestPath, []byte(manifestYAML), 0o666)
452451
if err != nil {
453452
t.Fatalf("Failed to write %s", manifestPath)
454453
}
@@ -469,7 +468,7 @@ data:
469468
// template
470469
bogusFilePath := path.Join(tmpDir, "README.md")
471470

472-
err := ioutil.WriteFile(bogusFilePath, []byte("# My Manifests"), 0o666)
471+
err := os.WriteFile(bogusFilePath, []byte("# My Manifests"), 0o666)
473472
if err != nil {
474473
t.Fatalf("Failed to write %s", bogusFilePath)
475474
}
@@ -699,7 +698,7 @@ data:
699698
game.properties: enemies=potato
700699
`
701700

702-
err := ioutil.WriteFile(manifestPath, []byte(manifestYAML), 0o666)
701+
err := os.WriteFile(manifestPath, []byte(manifestYAML), 0o666)
703702
if err != nil {
704703
t.Fatalf("Failed to write %s", manifestPath)
705704
}
@@ -787,7 +786,7 @@ data:
787786
image: "quay.io/potatos1"
788787
`
789788

790-
err := ioutil.WriteFile(manifestPath, []byte(manifestYAML), 0o666)
789+
err := os.WriteFile(manifestPath, []byte(manifestYAML), 0o666)
791790
if err != nil {
792791
t.Fatalf("Failed to write %s", manifestPath)
793792
}
@@ -896,7 +895,7 @@ data:
896895
image: "quay.io/potatos1"
897896
`
898897

899-
err := ioutil.WriteFile(manifestPath, []byte(manifestYAML), 0o666)
898+
err := os.WriteFile(manifestPath, []byte(manifestYAML), 0o666)
900899
if err != nil {
901900
t.Fatalf("Failed to write %s", manifestPath)
902901
}
@@ -935,7 +934,7 @@ kind: ClusterPolicy
935934
metadata:
936935
name: my-awesome-policy`
937936

938-
err := ioutil.WriteFile(manifestPath, []byte(manifestYAML), 0o666)
937+
err := os.WriteFile(manifestPath, []byte(manifestYAML), 0o666)
939938
if err != nil {
940939
t.Fatalf("Failed to write %s", manifestPath)
941940
}
@@ -1052,7 +1051,7 @@ func TestGetPolicyTemplateInvalidManifest(t *testing.T) {
10521051
tmpDir := t.TempDir()
10531052
manifestPath := path.Join(tmpDir, "configmap.yaml")
10541053
// Ensure an error is returned when there is an invalid manifest file
1055-
err := ioutil.WriteFile(manifestPath, []byte("$i am not YAML!"), 0o666)
1054+
err := os.WriteFile(manifestPath, []byte("$i am not YAML!"), 0o666)
10561055
if err != nil {
10571056
t.Fatalf("Failed to write %s", manifestPath)
10581057
}
@@ -1101,7 +1100,7 @@ data:
11011100
enemies=potato
11021101
`
11031102

1104-
err := ioutil.WriteFile(manifestsPath, []byte(yamlContent), 0o666)
1103+
err := os.WriteFile(manifestsPath, []byte(yamlContent), 0o666)
11051104
if err != nil {
11061105
t.Fatalf("Failed to write %s", manifestsPath)
11071106
}
@@ -1146,7 +1145,7 @@ data:
11461145
---
11471146
`
11481147

1149-
err := ioutil.WriteFile(manifestsPath, []byte(yamlContent), 0o666)
1148+
err := os.WriteFile(manifestsPath, []byte(yamlContent), 0o666)
11501149
if err != nil {
11511150
t.Fatalf("Failed to write %s", manifestsPath)
11521151
}
@@ -1185,7 +1184,7 @@ func TestUnmarshalManifestFileInvalidYAML(t *testing.T) {
11851184
manifestPath := path.Join(tmpDir, "configmaps.yaml")
11861185
yamlContent := `$I am not YAML`
11871186

1188-
err := ioutil.WriteFile(manifestPath, []byte(yamlContent), 0o666)
1187+
err := os.WriteFile(manifestPath, []byte(yamlContent), 0o666)
11891188
if err != nil {
11901189
t.Fatalf("Failed to write %s", manifestPath)
11911190
}
@@ -1202,7 +1201,7 @@ func TestUnmarshalManifestFileNotObject(t *testing.T) {
12021201
manifestPath := path.Join(tmpDir, "configmaps.yaml")
12031202
yamlContent := `- i am an array`
12041203

1205-
err := ioutil.WriteFile(manifestPath, []byte(yamlContent), 0o666)
1204+
err := os.WriteFile(manifestPath, []byte(yamlContent), 0o666)
12061205
if err != nil {
12071206
t.Fatalf("Failed to write %s", manifestPath)
12081207
}
@@ -1262,14 +1261,14 @@ func TestVerifyManifestPath(t *testing.T) {
12621261
manifestPath := path.Join(workingDir, "configmap.yaml")
12631262
yamlContent := "---\nkind: ConfigMap"
12641263

1265-
err = ioutil.WriteFile(manifestPath, []byte(yamlContent), 0o666)
1264+
err = os.WriteFile(manifestPath, []byte(yamlContent), 0o666)
12661265
if err != nil {
12671266
t.Fatalf("Failed to write %s", manifestPath)
12681267
}
12691268

12701269
otherManifestPath := path.Join(otherDir, "configmap.yaml")
12711270

1272-
err = ioutil.WriteFile(otherManifestPath, []byte(yamlContent), 0o666)
1271+
err = os.WriteFile(otherManifestPath, []byte(yamlContent), 0o666)
12731272
if err != nil {
12741273
t.Fatalf("Failed to write %s", otherManifestPath)
12751274
}
@@ -1399,7 +1398,7 @@ data:
13991398
for filename, content := range manifestPaths {
14001399
manifestPath := path.Join(kustomizeDir, filename)
14011400

1402-
err = ioutil.WriteFile(manifestPath, []byte(content), 0o666)
1401+
err = os.WriteFile(manifestPath, []byte(content), 0o666)
14031402
if err != nil {
14041403
t.Fatalf("Failed to write %s", manifestPath)
14051404
}

0 commit comments

Comments
 (0)