Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move from io/ioutil to io and os packages #858

Merged
merged 2 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cmd/polaris/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"os"
"os/exec"
Expand Down Expand Up @@ -137,7 +137,7 @@ func ProcessHelmTemplates(helmChart, helmValues string) (string, error) {
return "", err
}

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

defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)

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

if outputFile != "" {
err := ioutil.WriteFile(outputFile, []byte(outputBytes), 0644)
err := os.WriteFile(outputFile, []byte(outputBytes), 0644)
if err != nil {
logrus.Errorf("Error writing output to file: %v", err)
os.Exit(1)
Expand Down
5 changes: 2 additions & 3 deletions cmd/polaris/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package cmd

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -93,7 +92,7 @@ var fixCommand = &cobra.Command{
}

for _, fullFilePath := range yamlFiles {
yamlContent, err := ioutil.ReadFile(fullFilePath)
yamlContent, err := os.ReadFile(fullFilePath)
if err != nil {
logrus.Fatalf("Error reading file with file path %s: %v", fullFilePath, err)
}
Expand Down Expand Up @@ -136,7 +135,7 @@ var fixCommand = &cobra.Command{
}

if updatedYamlContent != "" {
err = ioutil.WriteFile(fullFilePath, []byte(updatedYamlContent), 0644)
err = os.WriteFile(fullFilePath, []byte(updatedYamlContent), 0644)
if err != nil {
logrus.Fatalf("Error writing output to file: %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"

"github.com/gobuffalo/packr/v2"
Expand Down Expand Up @@ -70,10 +70,10 @@ func ParseFile(path string) (Configuration, error) {
if err2 != nil {
return Configuration{}, err2
}
rawBytes, err = ioutil.ReadAll(response.Body)
rawBytes, err = io.ReadAll(response.Body)
} else {
// path is local
rawBytes, err = ioutil.ReadFile(path)
rawBytes, err = os.ReadFile(path)
}
if err != nil {
return Configuration{}, err
Expand Down
5 changes: 2 additions & 3 deletions pkg/kube/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -216,7 +215,7 @@ func CreateResourceProviderFromPath(directory string) (*ResourceProvider, error)
if !strings.HasSuffix(path, ".yml") && !strings.HasSuffix(path, ".yaml") {
return nil
}
contents, err := ioutil.ReadFile(path)
contents, err := os.ReadFile(path)
if err != nil {
logrus.Errorf("Error reading file: %v", path)
return err
Expand Down Expand Up @@ -427,7 +426,7 @@ func deduplicateControllers(inputResources []GenericResource) []GenericResource
}

func (resources *ResourceProvider) addResourcesFromReader(reader io.Reader) error {
contents, err := ioutil.ReadAll(reader)
contents, err := io.ReadAll(reader)
if err != nil {
logrus.Errorf("Error reading from %v: %v", reader, err)
return err
Expand Down
4 changes: 2 additions & 2 deletions pkg/kube/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package kube
import (
"bytes"
"context"
"io/ioutil"
"os"
"testing"
"time"

Expand Down Expand Up @@ -78,7 +78,7 @@ func TestGetMultipleResourceFromBadFile(t *testing.T) {
}

func TestAddResourcesFromReader(t *testing.T) {
contents, err := ioutil.ReadFile("./test_files/test_2/multi.yaml")
contents, err := os.ReadFile("./test_files/test_2/multi.yaml")
assert.NoError(t, err)
reader := bytes.NewBuffer(contents)
resources := newResourceProvider("unknown", "Path", "-")
Expand Down
3 changes: 1 addition & 2 deletions pkg/validator/fullaudit.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"time"

Expand Down Expand Up @@ -62,7 +61,7 @@ func RunAudit(config conf.Configuration, kubeResources *kube.ResourceProvider) (
// ReadAuditFromFile reads the data from a past audit stored in a JSON or YAML file.
func ReadAuditFromFile(fileName string) AuditData {
auditData := AuditData{}
oldFileBytes, err := ioutil.ReadFile(fileName)
oldFileBytes, err := os.ReadFile(fileName)
if err != nil {
logrus.Errorf("Unable to read contents of loaded file: %v", err)
os.Exit(1)
Expand Down
7 changes: 3 additions & 4 deletions test/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package test

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -48,7 +47,7 @@ func init() {
checkToTest := os.Getenv("POLARIS_CHECK_TEST")
_, baseDir, _, _ := runtime.Caller(0)
baseDir = filepath.Dir(baseDir) + "/checks"
dirs, err := ioutil.ReadDir(baseDir)
dirs, err := os.ReadDir(baseDir)
if err != nil {
panic(err)
}
Expand All @@ -58,13 +57,13 @@ func init() {
continue
}
checkDir := baseDir + "/" + check
cases, err := ioutil.ReadDir(checkDir)
cases, err := os.ReadDir(checkDir)
if err != nil {
panic(err)
}
configString := "checks:\n " + check + ": danger"
checkPath := checkDir + "/check.yaml"
customCheckContent, err := ioutil.ReadFile(checkPath)
customCheckContent, err := os.ReadFile(checkPath)
if err == nil {
lines := strings.Split(string(customCheckContent), "\n")
for idx := range lines {
Expand Down