Skip to content

Commit

Permalink
Remove use of io/ioutil
Browse files Browse the repository at this point in the history
This is a follow-up to #3403
io/ioutil has been deprecated since go 1.16
There are replacements for the functions it provides either in io or os.
  • Loading branch information
cfergeau authored and praveenkumar committed Nov 4, 2022
1 parent 5847f99 commit 641ff18
Show file tree
Hide file tree
Showing 46 changed files with 117 additions and 143 deletions.
3 changes: 1 addition & 2 deletions cmd/crc-embedder/cmd/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -46,7 +45,7 @@ func runEmbed(args []string) {
var err error
executablePath := args[0]
if cacheDir == "" {
cacheDir, err = ioutil.TempDir("", "crc-embedder")
cacheDir, err = os.MkdirTemp("", "crc-embedder")
if err != nil {
logging.Fatalf("Failed to create temporary directory: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/crc/cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -193,7 +193,7 @@ func run(configuration *types.Configuration) error {

if watchdog {
go func() {
if _, err := ioutil.ReadAll(os.Stdin); err != nil {
if _, err := io.ReadAll(os.Stdin); err != nil {
logging.Errorf("unexpected error while reading stdin: %v", err)
}
logging.Error("stdin is closed, shutdown...")
Expand Down
14 changes: 7 additions & 7 deletions cmd/crc/cmd/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
Expand Down Expand Up @@ -51,7 +51,7 @@ func TestPlainStatus(t *testing.T) {

client := setUpClient(t)

require.NoError(t, ioutil.WriteFile(filepath.Join(cacheDir, "crc.qcow2"), make([]byte, 10000), 0600))
require.NoError(t, os.WriteFile(filepath.Join(cacheDir, "crc.qcow2"), make([]byte, 10000), 0600))

out := new(bytes.Buffer)
assert.NoError(t, runStatus(out, &daemonclient.Client{
Expand All @@ -73,7 +73,7 @@ func TestStatusWithoutPodman(t *testing.T) {
cacheDir := t.TempDir()

client := mocks.NewClient(t)
require.NoError(t, ioutil.WriteFile(filepath.Join(cacheDir, "crc.qcow2"), make([]byte, 10000), 0600))
require.NoError(t, os.WriteFile(filepath.Join(cacheDir, "crc.qcow2"), make([]byte, 10000), 0600))

client.On("Status").Return(apiClient.ClusterStatusResult{
CrcStatus: string(state.Running),
Expand Down Expand Up @@ -104,7 +104,7 @@ func TestJsonStatus(t *testing.T) {

client := setUpClient(t)

require.NoError(t, ioutil.WriteFile(filepath.Join(cacheDir, "crc.qcow2"), make([]byte, 10000), 0600))
require.NoError(t, os.WriteFile(filepath.Join(cacheDir, "crc.qcow2"), make([]byte, 10000), 0600))

out := new(bytes.Buffer)
assert.NoError(t, runStatus(out, &daemonclient.Client{
Expand Down Expand Up @@ -132,7 +132,7 @@ func TestPlainStatusWithError(t *testing.T) {

client := setUpFailingClient(t)

require.NoError(t, ioutil.WriteFile(filepath.Join(cacheDir, "crc.qcow2"), make([]byte, 10000), 0600))
require.NoError(t, os.WriteFile(filepath.Join(cacheDir, "crc.qcow2"), make([]byte, 10000), 0600))

out := new(bytes.Buffer)
assert.EqualError(t, runStatus(out, &daemonclient.Client{
Expand All @@ -146,7 +146,7 @@ func TestJsonStatusWithError(t *testing.T) {

client := setUpFailingClient(t)

require.NoError(t, ioutil.WriteFile(filepath.Join(cacheDir, "crc.qcow2"), make([]byte, 10000), 0600))
require.NoError(t, os.WriteFile(filepath.Join(cacheDir, "crc.qcow2"), make([]byte, 10000), 0600))

out := new(bytes.Buffer)
assert.NoError(t, runStatus(out, &daemonclient.Client{
Expand All @@ -166,7 +166,7 @@ func TestStatusWithMemoryPodman(t *testing.T) {
cacheDir := t.TempDir()

client := mocks.NewClient(t)
require.NoError(t, ioutil.WriteFile(filepath.Join(cacheDir, "crc.qcow2"), make([]byte, 10000), 0600))
require.NoError(t, os.WriteFile(filepath.Join(cacheDir, "crc.qcow2"), make([]byte, 10000), 0600))

client.On("Status").Return(apiClient.ClusterStatusResult{
CrcStatus: string(state.Running),
Expand Down
3 changes: 1 addition & 2 deletions pkg/compress/compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package compress

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -121,7 +120,7 @@ func checkFiles(destDir string, files fileMap) error {
}
delete(files, archivePath)

data, err := ioutil.ReadFile(path) // #nosec G304
data, err := os.ReadFile(path) // #nosec G304
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/crc/api/api_client_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package api

import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -277,7 +277,7 @@ func TestPullSecret(t *testing.T) {
assert.False(t, defined)

pullSecretFile := filepath.Join(dir, "pull-secret.json")
assert.NoError(t, ioutil.WriteFile(pullSecretFile, []byte(constants.OkdPullSecret), 0600))
assert.NoError(t, os.WriteFile(pullSecretFile, []byte(constants.OkdPullSecret), 0600))
_, err = config.Set(crcConfig.PullSecretFile, pullSecretFile)
assert.NoError(t, err)

Expand Down
5 changes: 2 additions & 3 deletions pkg/crc/api/api_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package api
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/http/httputil"
Expand All @@ -27,7 +26,7 @@ type mockServer struct {
}

func createDummyPullSecret(t *testing.T) string {
f, err := ioutil.TempFile("", "kubeconfig")
f, err := os.CreateTemp("", "kubeconfig")
assert.NoError(t, err)
_, err = f.WriteString(constants.OkdPullSecret)
assert.NoError(t, err)
Expand Down Expand Up @@ -464,7 +463,7 @@ func testOne(t *testing.T, testCase *testCase, server *mockServer) {
require.Equal(t, testCase.response.statusCode, resp.StatusCode, testCase.request)
require.Equal(t, testCase.response.protoMajor, resp.ProtoMajor, testCase.request)
require.Equal(t, testCase.response.protoMinor, resp.ProtoMinor, testCase.request)
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err, testCase.request)
require.Equal(t, testCase.response.body, string(body), testCase.request)
fmt.Println("-----")
Expand Down
5 changes: 2 additions & 3 deletions pkg/crc/api/client/client.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/http"
"net/url"
"strings"
Expand Down Expand Up @@ -218,7 +217,7 @@ func (c *client) sendGetRequest(url string) ([]byte, error) {
}
defer res.Body.Close()

body, err := ioutil.ReadAll(res.Body)
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("Unknown error reading response: %w", err)
}
Expand Down Expand Up @@ -267,7 +266,7 @@ func (c *client) sendRequest(url string, method string, data io.Reader) ([]byte,
}
}

body, err := ioutil.ReadAll(res.Body)
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("Unknown error reading response: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/crc/api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package api

import (
"encoding/json"
"io/ioutil"
"io"
"net/http"
"net/url"
"sync"
Expand Down Expand Up @@ -99,7 +99,7 @@ func (s *server) Handler() http.Handler {
}
s.routesLock.RUnlock()

requestBody, err := ioutil.ReadAll(r.Body)
requestBody, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand Down
3 changes: 1 addition & 2 deletions pkg/crc/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cache

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -108,7 +107,7 @@ func (c *Cache) CacheExecutable() error {
}

// Create tmp dir to download the requested tarball
tmpDir, err := ioutil.TempDir("", "crc")
tmpDir, err := os.MkdirTemp("", "crc")
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/crc/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -105,7 +105,7 @@ func GetRAMUsage(sshRunner *ssh.Runner) (int64, int64, error) {
}

func EnsureSSHKeyPresentInTheCluster(ctx context.Context, ocConfig oc.Config, sshPublicKeyPath string) error {
sshPublicKeyByte, err := ioutil.ReadFile(sshPublicKeyPath)
sshPublicKeyByte, err := os.ReadFile(sshPublicKeyPath)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/crc/cluster/clusteroperator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cluster
import (
"context"
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -40,7 +40,7 @@ type mockLister struct {
}

func (r *mockLister) List(ctx context.Context, opts metav1.ListOptions) (*v1.ClusterOperatorList, error) {
bin, err := ioutil.ReadFile(r.file)
bin, err := os.ReadFile(r.file)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/crc/cluster/kubeadmin_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"crypto/rand"
"encoding/base64"
"fmt"
"io/ioutil"
"math/big"
"os"
"strings"

"github.com/crc-org/crc/pkg/crc/constants"
Expand All @@ -25,14 +25,14 @@ func GenerateKubeAdminUserPassword() error {
if err != nil {
return fmt.Errorf("Cannot generate the kubeadmin user password: %w", err)
}
return ioutil.WriteFile(kubeAdminPasswordFile, []byte(kubeAdminPassword), 0600)
return os.WriteFile(kubeAdminPasswordFile, []byte(kubeAdminPassword), 0600)
}

// UpdateKubeAdminUserPassword updates the htpasswd secret
func UpdateKubeAdminUserPassword(ctx context.Context, ocConfig oc.Config, newPassword string) error {
if newPassword != "" {
logging.Infof("Overriding password for kubeadmin user")
if err := ioutil.WriteFile(constants.GetKubeAdminPasswordPath(), []byte(strings.TrimSpace(newPassword)), 0600); err != nil {
if err := os.WriteFile(constants.GetKubeAdminPasswordPath(), []byte(strings.TrimSpace(newPassword)), 0600); err != nil {
return err
}
}
Expand Down Expand Up @@ -79,7 +79,7 @@ func UpdateKubeAdminUserPassword(ctx context.Context, ocConfig oc.Config, newPas

func GetKubeadminPassword() (string, error) {
kubeAdminPasswordFile := constants.GetKubeAdminPasswordPath()
rawData, err := ioutil.ReadFile(kubeAdminPasswordFile)
rawData, err := os.ReadFile(kubeAdminPasswordFile)
if err != nil {
return "", err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/crc/cluster/pullsecret.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"strings"

crcConfig "github.com/crc-org/crc/pkg/crc/config"
Expand Down Expand Up @@ -156,7 +156,7 @@ func loadFile(path string) (string, error) {
if path == "" {
return "", errors.New("empty path")
}
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
return "", err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/crc/cluster/pullsecret_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cluster

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

Expand Down Expand Up @@ -39,15 +39,15 @@ func TestLoadPullSecret(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, secret3, val)

assert.NoError(t, ioutil.WriteFile(filepath.Join(dir, "file2"), []byte(secret2), 0600))
assert.NoError(t, os.WriteFile(filepath.Join(dir, "file2"), []byte(secret2), 0600))
_, err = cfg.Set(config.PullSecretFile, filepath.Join(dir, "file2"))
assert.NoError(t, err)

val, err = loader.Value()
assert.NoError(t, err)
assert.Equal(t, secret2, val)

assert.NoError(t, ioutil.WriteFile(filepath.Join(dir, "file2"), []byte(secret1), 0600))
assert.NoError(t, os.WriteFile(filepath.Join(dir, "file2"), []byte(secret1), 0600))

val, err = loader.Value()
assert.NoError(t, err)
Expand Down
11 changes: 5 additions & 6 deletions pkg/crc/config/viper_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package config
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -68,7 +67,7 @@ func (c *ViperStorage) Set(key string, value interface{}) error {
if err := ensureConfigFileExists(c.configFile); err != nil {
return err
}
in, err := ioutil.ReadFile(c.configFile)
in, err := os.ReadFile(c.configFile)
if err != nil {
return err
}
Expand All @@ -90,7 +89,7 @@ func (c *ViperStorage) Unset(key string) error {
if err := ensureConfigFileExists(c.configFile); err != nil {
return err
}
in, err := ioutil.ReadFile(c.configFile)
in, err := os.ReadFile(c.configFile)
if err != nil {
return err
}
Expand Down Expand Up @@ -118,15 +117,15 @@ func (c *ViperStorage) BindFlagSet(flagSet *pflag.FlagSet) error {
func ensureConfigFileExists(file string) error {
_, err := os.Stat(file)
if os.IsNotExist(err) {
return ioutil.WriteFile(file, []byte("{}\n"), 0600)
return os.WriteFile(file, []byte("{}\n"), 0600)
}
return err
}

func atomicWrite(bin []byte, configFile string) error {
ext := filepath.Ext(configFile)
pattern := fmt.Sprintf("%s*%s", strings.TrimSuffix(filepath.Base(configFile), ext), ext)
tmpFile, err := ioutil.TempFile(filepath.Dir(configFile), pattern)
tmpFile, err := os.CreateTemp(filepath.Dir(configFile), pattern)
if err != nil {
return err
}
Expand All @@ -136,7 +135,7 @@ func atomicWrite(bin []byte, configFile string) error {
if err := tmpFile.Close(); err != nil {
return err
}
if err := ioutil.WriteFile(tmpFile.Name(), bin, 0600); err != nil {
if err := os.WriteFile(tmpFile.Name(), bin, 0600); err != nil {
return err
}
return os.Rename(tmpFile.Name(), configFile)
Expand Down
Loading

0 comments on commit 641ff18

Please sign in to comment.