Skip to content

Commit 8bc1a4b

Browse files
JuneezeeAndrew Suderman
andauthored
refactor: move from io/ioutil to io and os packages (#858)
The io/ioutil package has been deprecated as of Go 1.16 [1]. This commit replaces the existing io/ioutil functions with their new definitions in io and os packages. [1]: https://golang.org/doc/go1.16#ioutil Signed-off-by: Eng Zer Jun <[email protected]> Co-authored-by: Andrew Suderman <[email protected]>
1 parent 467d06f commit 8bc1a4b

File tree

7 files changed

+17
-21
lines changed

7 files changed

+17
-21
lines changed

cmd/polaris/audit.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"context"
2020
"encoding/json"
2121
"fmt"
22-
"io/ioutil"
22+
"io"
2323
"net/http"
2424
"os"
2525
"os/exec"
@@ -137,7 +137,7 @@ func ProcessHelmTemplates(helmChart, helmValues string) (string, error) {
137137
return "", err
138138
}
139139

140-
dir, err := ioutil.TempDir("", "*")
140+
dir, err := os.MkdirTemp("", "*")
141141
if err != nil {
142142
return "", err
143143
}
@@ -212,7 +212,7 @@ func outputAudit(auditData validator.AuditData, outputFile, outputURL, outputFor
212212

213213
defer resp.Body.Close()
214214

215-
body, err := ioutil.ReadAll(resp.Body)
215+
body, err := io.ReadAll(resp.Body)
216216

217217
if err != nil {
218218
logrus.Errorf("Error reading response: %v", err)
@@ -223,7 +223,7 @@ func outputAudit(auditData validator.AuditData, outputFile, outputURL, outputFor
223223
}
224224

225225
if outputFile != "" {
226-
err := ioutil.WriteFile(outputFile, []byte(outputBytes), 0644)
226+
err := os.WriteFile(outputFile, []byte(outputBytes), 0644)
227227
if err != nil {
228228
logrus.Errorf("Error writing output to file: %v", err)
229229
os.Exit(1)

cmd/polaris/fix.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package cmd
1616

1717
import (
1818
"fmt"
19-
"io/ioutil"
2019
"os"
2120
"path/filepath"
2221
"strings"
@@ -93,7 +92,7 @@ var fixCommand = &cobra.Command{
9392
}
9493

9594
for _, fullFilePath := range yamlFiles {
96-
yamlContent, err := ioutil.ReadFile(fullFilePath)
95+
yamlContent, err := os.ReadFile(fullFilePath)
9796
if err != nil {
9897
logrus.Fatalf("Error reading file with file path %s: %v", fullFilePath, err)
9998
}
@@ -136,7 +135,7 @@ var fixCommand = &cobra.Command{
136135
}
137136

138137
if updatedYamlContent != "" {
139-
err = ioutil.WriteFile(fullFilePath, []byte(updatedYamlContent), 0644)
138+
err = os.WriteFile(fullFilePath, []byte(updatedYamlContent), 0644)
140139
if err != nil {
141140
logrus.Fatalf("Error writing output to file: %v", err)
142141
}

pkg/config/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import (
1919
"errors"
2020
"fmt"
2121
"io"
22-
"io/ioutil"
2322
"net/http"
23+
"os"
2424
"strings"
2525

2626
"github.com/gobuffalo/packr/v2"
@@ -70,10 +70,10 @@ func ParseFile(path string) (Configuration, error) {
7070
if err2 != nil {
7171
return Configuration{}, err2
7272
}
73-
rawBytes, err = ioutil.ReadAll(response.Body)
73+
rawBytes, err = io.ReadAll(response.Body)
7474
} else {
7575
// path is local
76-
rawBytes, err = ioutil.ReadFile(path)
76+
rawBytes, err = os.ReadFile(path)
7777
}
7878
if err != nil {
7979
return Configuration{}, err

pkg/kube/resources.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"encoding/json"
2121
"fmt"
2222
"io"
23-
"io/ioutil"
2423
"os"
2524
"path/filepath"
2625
"regexp"
@@ -216,7 +215,7 @@ func CreateResourceProviderFromPath(directory string) (*ResourceProvider, error)
216215
if !strings.HasSuffix(path, ".yml") && !strings.HasSuffix(path, ".yaml") {
217216
return nil
218217
}
219-
contents, err := ioutil.ReadFile(path)
218+
contents, err := os.ReadFile(path)
220219
if err != nil {
221220
logrus.Errorf("Error reading file: %v", path)
222221
return err
@@ -427,7 +426,7 @@ func deduplicateControllers(inputResources []GenericResource) []GenericResource
427426
}
428427

429428
func (resources *ResourceProvider) addResourcesFromReader(reader io.Reader) error {
430-
contents, err := ioutil.ReadAll(reader)
429+
contents, err := io.ReadAll(reader)
431430
if err != nil {
432431
logrus.Errorf("Error reading from %v: %v", reader, err)
433432
return err

pkg/kube/resources_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ package kube
1717
import (
1818
"bytes"
1919
"context"
20-
"io/ioutil"
20+
"os"
2121
"testing"
2222
"time"
2323

@@ -78,7 +78,7 @@ func TestGetMultipleResourceFromBadFile(t *testing.T) {
7878
}
7979

8080
func TestAddResourcesFromReader(t *testing.T) {
81-
contents, err := ioutil.ReadFile("./test_files/test_2/multi.yaml")
81+
contents, err := os.ReadFile("./test_files/test_2/multi.yaml")
8282
assert.NoError(t, err)
8383
reader := bytes.NewBuffer(contents)
8484
resources := newResourceProvider("unknown", "Path", "-")

pkg/validator/fullaudit.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"bytes"
1919
"fmt"
2020
"io"
21-
"io/ioutil"
2221
"os"
2322
"time"
2423

@@ -62,7 +61,7 @@ func RunAudit(config conf.Configuration, kubeResources *kube.ResourceProvider) (
6261
// ReadAuditFromFile reads the data from a past audit stored in a JSON or YAML file.
6362
func ReadAuditFromFile(fileName string) AuditData {
6463
auditData := AuditData{}
65-
oldFileBytes, err := ioutil.ReadFile(fileName)
64+
oldFileBytes, err := os.ReadFile(fileName)
6665
if err != nil {
6766
logrus.Errorf("Unable to read contents of loaded file: %v", err)
6867
os.Exit(1)

test/schema_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package test
1616

1717
import (
1818
"fmt"
19-
"io/ioutil"
2019
"os"
2120
"path/filepath"
2221
"runtime"
@@ -48,7 +47,7 @@ func init() {
4847
checkToTest := os.Getenv("POLARIS_CHECK_TEST")
4948
_, baseDir, _, _ := runtime.Caller(0)
5049
baseDir = filepath.Dir(baseDir) + "/checks"
51-
dirs, err := ioutil.ReadDir(baseDir)
50+
dirs, err := os.ReadDir(baseDir)
5251
if err != nil {
5352
panic(err)
5453
}
@@ -58,13 +57,13 @@ func init() {
5857
continue
5958
}
6059
checkDir := baseDir + "/" + check
61-
cases, err := ioutil.ReadDir(checkDir)
60+
cases, err := os.ReadDir(checkDir)
6261
if err != nil {
6362
panic(err)
6463
}
6564
configString := "checks:\n " + check + ": danger"
6665
checkPath := checkDir + "/check.yaml"
67-
customCheckContent, err := ioutil.ReadFile(checkPath)
66+
customCheckContent, err := os.ReadFile(checkPath)
6867
if err == nil {
6968
lines := strings.Split(string(customCheckContent), "\n")
7069
for idx := range lines {

0 commit comments

Comments
 (0)