Skip to content

Commit

Permalink
chore: Replace deprecated ioutil in util packages (#9848)
Browse files Browse the repository at this point in the history
Signed-off-by: jannfis <[email protected]>
  • Loading branch information
jannfis authored Jul 6, 2022
1 parent b8b7e91 commit f34687b
Show file tree
Hide file tree
Showing 26 changed files with 128 additions and 143 deletions.
4 changes: 2 additions & 2 deletions util/argo/normalizers/knowntypes_normalizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package normalizers

import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"

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

func TestKnownTypes(t *testing.T) {
typesData, err := ioutil.ReadFile("./diffing_known_types.txt")
typesData, err := os.ReadFile("./diffing_known_types.txt")
if !assert.NoError(t, err) {
return
}
Expand Down
12 changes: 6 additions & 6 deletions util/argo/resource_tracking_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package argo

import (
"io/ioutil"
"os"
"testing"

"github.com/argoproj/argo-cd/v2/util/kube"
Expand All @@ -14,7 +14,7 @@ import (
)

func TestSetAppInstanceLabel(t *testing.T) {
yamlBytes, err := ioutil.ReadFile("testdata/svc.yaml")
yamlBytes, err := os.ReadFile("testdata/svc.yaml")
assert.Nil(t, err)

var obj unstructured.Unstructured
Expand All @@ -30,7 +30,7 @@ func TestSetAppInstanceLabel(t *testing.T) {
}

func TestSetAppInstanceAnnotation(t *testing.T) {
yamlBytes, err := ioutil.ReadFile("testdata/svc.yaml")
yamlBytes, err := os.ReadFile("testdata/svc.yaml")
assert.Nil(t, err)

var obj unstructured.Unstructured
Expand All @@ -47,7 +47,7 @@ func TestSetAppInstanceAnnotation(t *testing.T) {
}

func TestSetAppInstanceAnnotationAndLabel(t *testing.T) {
yamlBytes, err := ioutil.ReadFile("testdata/svc.yaml")
yamlBytes, err := os.ReadFile("testdata/svc.yaml")
assert.Nil(t, err)
var obj unstructured.Unstructured
err = yaml.Unmarshal(yamlBytes, &obj)
Expand All @@ -63,7 +63,7 @@ func TestSetAppInstanceAnnotationAndLabel(t *testing.T) {
}

func TestSetAppInstanceAnnotationNotFound(t *testing.T) {
yamlBytes, err := ioutil.ReadFile("testdata/svc.yaml")
yamlBytes, err := os.ReadFile("testdata/svc.yaml")
assert.Nil(t, err)

var obj unstructured.Unstructured
Expand Down Expand Up @@ -108,7 +108,7 @@ func TestParseAppInstanceValueCorrectFormat(t *testing.T) {
}

func sampleResource() *unstructured.Unstructured {
yamlBytes, err := ioutil.ReadFile("testdata/svc.yaml")
yamlBytes, err := os.ReadFile("testdata/svc.yaml")
if err != nil {
panic(err)
}
Expand Down
13 changes: 6 additions & 7 deletions util/cert/cert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cert

import (
"fmt"
"io/ioutil"
"os"
"path"
"testing"
Expand Down Expand Up @@ -473,11 +472,11 @@ func TestGetSSHKnownHostsDataPath(t *testing.T) {
func TestGetCertificateForConnect(t *testing.T) {
t.Run("Success", func(t *testing.T) {
temppath := t.TempDir()
cert, err := ioutil.ReadFile("../../test/fixture/certs/argocd-test-server.crt")
cert, err := os.ReadFile("../../test/fixture/certs/argocd-test-server.crt")
if err != nil {
panic(err)
}
err = ioutil.WriteFile(path.Join(temppath, "127.0.0.1"), cert, 0666)
err = os.WriteFile(path.Join(temppath, "127.0.0.1"), cert, 0666)
if err != nil {
panic(err)
}
Expand All @@ -497,7 +496,7 @@ func TestGetCertificateForConnect(t *testing.T) {

t.Run("No valid cert in file", func(t *testing.T) {
temppath := t.TempDir()
err := ioutil.WriteFile(path.Join(temppath, "127.0.0.1"), []byte("foobar"), 0666)
err := os.WriteFile(path.Join(temppath, "127.0.0.1"), []byte("foobar"), 0666)
if err != nil {
panic(err)
}
Expand All @@ -513,11 +512,11 @@ func TestGetCertificateForConnect(t *testing.T) {
func TestGetCertBundlePathForRepository(t *testing.T) {
t.Run("Success", func(t *testing.T) {
temppath := t.TempDir()
cert, err := ioutil.ReadFile("../../test/fixture/certs/argocd-test-server.crt")
cert, err := os.ReadFile("../../test/fixture/certs/argocd-test-server.crt")
if err != nil {
panic(err)
}
err = ioutil.WriteFile(path.Join(temppath, "127.0.0.1"), cert, 0666)
err = os.WriteFile(path.Join(temppath, "127.0.0.1"), cert, 0666)
if err != nil {
panic(err)
}
Expand All @@ -537,7 +536,7 @@ func TestGetCertBundlePathForRepository(t *testing.T) {

t.Run("No valid cert in file", func(t *testing.T) {
temppath := t.TempDir()
err := ioutil.WriteFile(path.Join(temppath, "127.0.0.1"), []byte("foobar"), 0666)
err := os.WriteFile(path.Join(temppath, "127.0.0.1"), []byte("foobar"), 0666)
if err != nil {
panic(err)
}
Expand Down
13 changes: 6 additions & 7 deletions util/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
Expand Down Expand Up @@ -180,7 +179,7 @@ func SetGLogLevel(glogLevel int) {
}

func writeToTempFile(pattern string, data []byte) string {
f, err := ioutil.TempFile("", pattern)
f, err := os.CreateTemp("", pattern)
errors.CheckError(err)
defer io.Close(f)
_, err = f.Write(data)
Expand Down Expand Up @@ -252,10 +251,10 @@ func InteractiveEdit(filePattern string, data []byte, save func(input []byte) er
err := (term.TTY{In: os.Stdin, TryDev: true}).Safe(cmd.Run)
errors.CheckError(err)

updated, err := ioutil.ReadFile(tempFile)
updated, err := os.ReadFile(tempFile)
errors.CheckError(err)
if string(updated) == "" || string(updated) == string(data) {
errors.CheckError(fmt.Errorf("Edit cancelled, no valid changes were saved."))
errors.CheckError(fmt.Errorf("edit cancelled, no valid changes were saved"))
break
} else {
data = stripComments(updated)
Expand All @@ -272,7 +271,7 @@ func InteractiveEdit(filePattern string, data []byte, save func(input []byte) er
// PrintDiff prints a diff between two unstructured objects to stdout using an external diff utility
// Honors the diff utility set in the KUBECTL_EXTERNAL_DIFF environment variable
func PrintDiff(name string, live *unstructured.Unstructured, target *unstructured.Unstructured) error {
tempDir, err := ioutil.TempDir("", "argocd-diff")
tempDir, err := os.MkdirTemp("", "argocd-diff")
if err != nil {
return err
}
Expand All @@ -284,7 +283,7 @@ func PrintDiff(name string, live *unstructured.Unstructured, target *unstructure
return err
}
}
err = ioutil.WriteFile(targetFile, targetData, 0644)
err = os.WriteFile(targetFile, targetData, 0644)
if err != nil {
return err
}
Expand All @@ -296,7 +295,7 @@ func PrintDiff(name string, live *unstructured.Unstructured, target *unstructure
return err
}
}
err = ioutil.WriteFile(liveFile, liveData, 0644)
err = os.WriteFile(liveFile, liveData, 0644)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions util/clusterauth/clusterauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package clusterauth

import (
"context"
"io/ioutil"
"os"
"testing"
"time"

Expand Down Expand Up @@ -36,7 +36,7 @@ var (
)

func newServiceAccount() *corev1.ServiceAccount {
saBytes, err := ioutil.ReadFile("./testdata/argocd-manager-sa.yaml")
saBytes, err := os.ReadFile("./testdata/argocd-manager-sa.yaml")
errors.CheckError(err)
var sa corev1.ServiceAccount
err = yaml.Unmarshal(saBytes, &sa)
Expand All @@ -45,7 +45,7 @@ func newServiceAccount() *corev1.ServiceAccount {
}

func newServiceAccountSecret() *corev1.Secret {
secretBytes, err := ioutil.ReadFile("./testdata/argocd-manager-sa-token.yaml")
secretBytes, err := os.ReadFile("./testdata/argocd-manager-sa-token.yaml")
errors.CheckError(err)
var secret corev1.Secret
err = yaml.Unmarshal(secretBytes, &secret)
Expand Down
5 changes: 2 additions & 3 deletions util/cmp/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -171,7 +170,7 @@ func compressFiles(appPath string, excluded []string) (*os.File, string, error)
if err != nil {
return nil, "", fmt.Errorf("error creating tempDir for compressing files: %s", err)
}
tgzFile, err := ioutil.TempFile(tempDir, appName)
tgzFile, err := os.CreateTemp(tempDir, appName)
if err != nil {
return nil, "", fmt.Errorf("error creating app temp tgz file: %w", err)
}
Expand All @@ -198,7 +197,7 @@ func compressFiles(appPath string, excluded []string) (*os.File, string, error)
// It is responsibility of the caller to close the returned file.
func receiveFile(ctx context.Context, receiver StreamReceiver, checksum, dst string) (*os.File, error) {
hasher := sha256.New()
file, err := ioutil.TempFile(dst, "")
file, err := os.CreateTemp(dst, "")
if err != nil {
return nil, fmt.Errorf("error creating file: %w", err)
}
Expand Down
3 changes: 1 addition & 2 deletions util/cmp/stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -68,7 +67,7 @@ func TestReceiveApplicationStream(t *testing.T) {
// then
require.NoError(t, err)
assert.NotEmpty(t, workdir)
files, err := ioutil.ReadDir(workdir)
files, err := os.ReadDir(workdir)
require.NoError(t, err)
require.Equal(t, 2, len(files))
names := []string{}
Expand Down
10 changes: 5 additions & 5 deletions util/config/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package config
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"os"

"github.com/ghodss/yaml"
)

// UnmarshalReader is used to read manifests from stdin
func UnmarshalReader(reader io.Reader, obj interface{}) error {
data, err := ioutil.ReadAll(reader)
data, err := io.ReadAll(reader)
if err != nil {
return err
}
Expand Down Expand Up @@ -44,15 +44,15 @@ func unmarshalObject(data []byte, obj interface{}) error {
func MarshalLocalYAMLFile(path string, obj interface{}) error {
yamlData, err := yaml.Marshal(obj)
if err == nil {
err = ioutil.WriteFile(path, yamlData, 0600)
err = os.WriteFile(path, yamlData, 0600)
}
return err
}

// UnmarshalLocalFile retrieves JSON or YAML from a file on disk.
// The caller is responsible for checking error return values.
func UnmarshalLocalFile(path string, obj interface{}) error {
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err == nil {
err = unmarshalObject(data, obj)
}
Expand Down Expand Up @@ -82,7 +82,7 @@ func ReadRemoteFile(url string) ([]byte, error) {
defer func() {
_ = resp.Body.Close()
}()
data, err = ioutil.ReadAll(resp.Body)
data, err = io.ReadAll(resp.Body)
}
return data, err
}
3 changes: 1 addition & 2 deletions util/config/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
Expand All @@ -21,7 +20,7 @@ func TestUnmarshalLocalFile(t *testing.T) {
)
sentinel := fmt.Sprintf("---\nfield1: %q\nfield2: %d", field1, field2)

file, err := ioutil.TempFile(os.TempDir(), "")
file, err := os.CreateTemp(os.TempDir(), "")
if err != nil {
panic(err)
}
Expand Down
5 changes: 2 additions & 3 deletions util/db/gpgkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package db
import (
"context"
"fmt"
"io/ioutil"
"os"

log "github.com/sirupsen/logrus"
Expand All @@ -15,13 +14,13 @@ import (

// Validates a single GnuPG key and returns the key's ID
func validatePGPKey(keyData string) (*appsv1.GnuPGPublicKey, error) {
f, err := ioutil.TempFile("", "gpg-public-key")
f, err := os.CreateTemp("", "gpg-public-key")
if err != nil {
return nil, err
}
defer os.Remove(f.Name())

err = ioutil.WriteFile(f.Name(), []byte(keyData), 0600)
err = os.WriteFile(f.Name(), []byte(keyData), 0600)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions util/dex/dex.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dex
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httputil"
"net/url"
Expand Down Expand Up @@ -34,7 +34,7 @@ func NewDexHTTPReverseProxy(serverAddr string, baseHRef string) func(writer http
proxy := httputil.NewSingleHostReverseProxy(target)
proxy.ModifyResponse = func(resp *http.Response) error {
if resp.StatusCode == 500 {
b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
Expand All @@ -47,7 +47,7 @@ func NewDexHTTPReverseProxy(serverAddr string, baseHRef string) func(writer http
resp.Header.Set("Content-Length", strconv.Itoa(0))
resp.Header.Set("Location", fmt.Sprintf("%s?has_sso_error=true", path.Join(baseHRef, "login")))
resp.StatusCode = http.StatusSeeOther
resp.Body = ioutil.NopCloser(bytes.NewReader(make([]byte, 0)))
resp.Body = io.NopCloser(bytes.NewReader(make([]byte, 0)))
return nil
}
return nil
Expand Down
Loading

0 comments on commit f34687b

Please sign in to comment.