Skip to content
This repository was archived by the owner on Mar 22, 2024. It is now read-only.

Commit 0207226

Browse files
authored
Remove references to ioutil (elastic#5927)
Remove references to ioutil: io/ioutil is deprecated since Go 1.16 and reported as such when golangci-lint is upgraded to its latest version.
1 parent bb7c5bf commit 0207226

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+158
-173
lines changed

cmd/license-initializer/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"fmt"
99
"html/template"
1010
"io"
11-
"io/ioutil"
1211
"os"
1312
"strings"
1413

@@ -86,7 +85,7 @@ var publicKeyBytes = []byte{
8685
}
8786
`
8887

89-
bytes, err := ioutil.ReadFile(pubkeyFile)
88+
bytes, err := os.ReadFile(pubkeyFile)
9089
if err != nil {
9190
handleErr(errors.Wrapf(err, "Failed to read %v", pubkeyFile))
9291
}

cmd/license-initializer/main_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ package main
88

99
import (
1010
"bytes"
11-
"io/ioutil"
11+
"os"
1212
"path/filepath"
1313
"testing"
1414

@@ -17,7 +17,7 @@ import (
1717

1818
func Test_generateSrc(t *testing.T) {
1919
path := filepath.Join("testdata", "expected.src")
20-
expectedBytes, err := ioutil.ReadFile(path)
20+
expectedBytes, err := os.ReadFile(path)
2121
require.NoError(t, err)
2222
input := filepath.Join("testdata", "test.key")
2323
var out bytes.Buffer

hack/deployer/cmd/create.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package cmd
66

77
import (
88
"fmt"
9-
"io/ioutil"
109
"os"
1110
"path"
1211

@@ -69,7 +68,7 @@ func CreateCommand() *cobra.Command {
6968
}
7069

7170
fullPath := path.Join(filePath, fmt.Sprintf("deployer-config-%s.yml", provider))
72-
return ioutil.WriteFile(fullPath, []byte(cfgData), 0600)
71+
return os.WriteFile(fullPath, []byte(cfgData), 0600)
7372
},
7473
}
7574

hack/deployer/runner/eks.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"bytes"
99
"fmt"
1010
"html/template"
11-
"io/ioutil"
1211
"log"
1312
"os"
1413
"path/filepath"
@@ -117,7 +116,7 @@ func (e *EKSDriver) Execute() error {
117116
}
118117
createCfgFile := filepath.Join(e.ctx["WorkDir"].(string), "cluster.yaml") //nolint:forcetypeassert
119118
e.ctx["CreateCfgFile"] = createCfgFile
120-
if err := ioutil.WriteFile(createCfgFile, createCfg.Bytes(), 0600); err != nil {
119+
if err := os.WriteFile(createCfgFile, createCfg.Bytes(), 0600); err != nil {
121120
return fmt.Errorf("while writing create cfg %w", err)
122121
}
123122
if err := e.newCmd(`eksctl create cluster -v 0 -f {{.CreateCfgFile}}`).Run(); err != nil {
@@ -150,7 +149,7 @@ func (e *EKSDriver) ensureWorkDir() error {
150149
if e.ctx["WorkDir"] != "" {
151150
return nil
152151
}
153-
dir, err := ioutil.TempDir("", e.ctx["ClusterName"].(string))
152+
dir, err := os.MkdirTemp("", e.ctx["ClusterName"].(string))
154153
if err != nil {
155154
return err
156155
}
@@ -232,7 +231,7 @@ func (e *EKSDriver) writeAWSCredentials() error {
232231
}
233232
log.Printf("Writing aws credentials")
234233
fileContents := fmt.Sprintf(awsAuthTemplate, awsAccessKeyID, e.ctx[awsAccessKeyID], awsSecretAccessKey, e.ctx[awsSecretAccessKey])
235-
return ioutil.WriteFile(file, []byte(fileContents), 0600)
234+
return os.WriteFile(file, []byte(fileContents), 0600)
236235
}
237236

238237
var _ Driver = &EKSDriver{}

hack/deployer/runner/kind.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package runner
77
import (
88
"fmt"
99
"io/fs"
10-
"io/ioutil"
1110
"log"
1211
"os"
1312
"path/filepath"
@@ -149,7 +148,7 @@ func (k *KindDriver) delete() error {
149148

150149
func (k *KindDriver) createTmpManifest() (*os.File, error) {
151150
// HOME is shared between CI container and Kind container
152-
f, err := ioutil.TempFile(os.Getenv("HOME"), "kind-cluster")
151+
f, err := os.CreateTemp(os.Getenv("HOME"), "kind-cluster")
153152
if err != nil {
154153
return nil, err
155154
}
@@ -221,7 +220,7 @@ func (k *KindDriver) getKubeConfig() (*os.File, error) {
221220
}
222221

223222
// Persist kubeconfig for reliability in following kubectl commands
224-
kubeCfg, err := ioutil.TempFile("", "kubeconfig")
223+
kubeCfg, err := os.CreateTemp("", "kubeconfig")
225224
if err != nil {
226225
return nil, err
227226
}
@@ -248,7 +247,7 @@ func (k *KindDriver) GetCredentials() error {
248247

249248
func (k *KindDriver) createTmpStorageClass() (string, error) {
250249
tmpFile := filepath.Join(os.Getenv("HOME"), storageClassFileName)
251-
err := ioutil.WriteFile(tmpFile, []byte(storageClass), fs.ModePerm)
250+
err := os.WriteFile(tmpFile, []byte(storageClass), fs.ModePerm)
252251
return tmpFile, err
253252
}
254253

hack/deployer/runner/kubeconfig.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package runner
77
import (
88
"errors"
99
"fmt"
10-
"io/ioutil"
1110
"log"
1211
"os"
1312
"path/filepath"
@@ -35,7 +34,7 @@ func mergeKubeconfig(kubeConfig string) error {
3534
if err != nil {
3635
return err
3736
}
38-
return ioutil.WriteFile(hostKubeconfig, []byte(merged), 0600)
37+
return os.WriteFile(hostKubeconfig, []byte(merged), 0600)
3938
}
4039

4140
func removeKubeconfig(context, clusterName, userName string) error {

hack/deployer/runner/ocp.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"errors"
1010
"fmt"
1111
"io"
12-
"io/ioutil"
1312
"log"
1413
"os"
1514
"path/filepath"
@@ -171,7 +170,7 @@ func (d *OCPDriver) create() error {
171170
}
172171

173172
installConfig := filepath.Join(d.runtimeState.ClusterStateDir, "install-config.yaml")
174-
err := ioutil.WriteFile(installConfig, tpl.Bytes(), 0600)
173+
err := os.WriteFile(installConfig, tpl.Bytes(), 0600)
175174
if err != nil {
176175
return err
177176
}
@@ -260,7 +259,7 @@ func (d *OCPDriver) ensureWorkDir() error {
260259
// having the work dir in HOME also underlines the importance of the work dir contents. The work dir is the only
261260
// source to cleanly uninstall the cluster should the rsync fail.
262261
var err error
263-
workDir, err = ioutil.TempDir(os.Getenv("HOME"), d.plan.ClusterName)
262+
workDir, err = os.MkdirTemp(os.Getenv("HOME"), d.plan.ClusterName)
264263
if err != nil {
265264
return err
266265
}

hack/deployer/runner/ocp3.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package runner
66

77
import (
88
"fmt"
9-
"io/ioutil"
109
"log"
1110
"os"
1211
"path/filepath"
@@ -149,7 +148,7 @@ func (d OCP3Driver) writeAnsibleVarsFile() error {
149148
}
150149
varsFile := filepath.Join(os.Getenv("HOME"), AnsibleVarsFilename)
151150
/* #nosec */
152-
if err := ioutil.WriteFile(varsFile, varsBytes, 0644); err != nil {
151+
if err := os.WriteFile(varsFile, varsBytes, 0644); err != nil {
153152
return fmt.Errorf("while writing Ansible variables file %w", err)
154153
}
155154
return nil

hack/deployer/runner/settings.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
package runner
66

77
import (
8-
"io/ioutil"
8+
"os"
99

1010
"gopkg.in/yaml.v3"
1111

@@ -110,7 +110,7 @@ type RunConfig struct {
110110
}
111111

112112
func ParseFiles(plansFile, runConfigFile string) (Plans, RunConfig, error) {
113-
yml, err := ioutil.ReadFile(plansFile)
113+
yml, err := os.ReadFile(plansFile)
114114
if err != nil {
115115
return Plans{}, RunConfig{}, err
116116
}
@@ -121,7 +121,7 @@ func ParseFiles(plansFile, runConfigFile string) (Plans, RunConfig, error) {
121121
return Plans{}, RunConfig{}, err
122122
}
123123

124-
yml, err = ioutil.ReadFile(runConfigFile)
124+
yml, err = os.ReadFile(runConfigFile)
125125
if err != nil {
126126
return Plans{}, RunConfig{}, err
127127
}

hack/deployer/runner/tanzu.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"bytes"
99
"encoding/json"
1010
"fmt"
11-
"io/ioutil"
1211
"log"
1312
"os"
1413
"path/filepath"
@@ -187,7 +186,7 @@ func (t *TanzuDriver) ensureWorkDir() error {
187186
// base work dir in HOME dir otherwise mounting to container won't work without further settings adjustment
188187
// in macOS in local mode. In CI mode we need the workdir to be in the volume shared between containers.
189188
var err error
190-
workDir, err = ioutil.TempDir(os.Getenv("HOME"), t.plan.ClusterName)
189+
workDir, err = os.MkdirTemp(os.Getenv("HOME"), t.plan.ClusterName)
191190
if err != nil {
192191
return err
193192
}
@@ -261,7 +260,7 @@ func (t *TanzuDriver) createInstallerConfig() error {
261260
return err
262261
}
263262

264-
return ioutil.WriteFile(t.installerConfigFilePath(), cfg.Bytes(), 0600)
263+
return os.WriteFile(t.installerConfigFilePath(), cfg.Bytes(), 0600)
265264
}
266265

267266
// installerConfigFilePath returns the path to the installer config valid in the context of deployer.

hack/deployer/vault/vault.go

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

77
import (
88
"fmt"
9-
"io/ioutil"
109
"os"
1110
"path/filepath"
1211
"strings"
@@ -126,7 +125,7 @@ func readCachedToken() (string, error) {
126125
return "", err
127126
}
128127

129-
bytes, err := ioutil.ReadFile(path)
128+
bytes, err := os.ReadFile(path)
130129
if err != nil {
131130
return "", err
132131
}
@@ -150,7 +149,7 @@ func (v *Client) ReadIntoFile(fileName, secretPath, fieldName string) error {
150149
return fmt.Errorf("field %s at %s is not a string, that's unexpected", fieldName, secretPath)
151150
}
152151

153-
return ioutil.WriteFile(fileName, []byte(stringServiceAccount), 0600)
152+
return os.WriteFile(fileName, []byte(stringServiceAccount), 0600)
154153
}
155154

156155
// Get fetches contents of a single field at a specified path in Vault

hack/operatorhub/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"errors"
1313
"fmt"
1414
"io"
15-
"io/ioutil"
1615
"net/http"
1716
"os"
1817
"path/filepath"
@@ -176,7 +175,7 @@ func (c *config) HasDigestPinning() bool {
176175
}
177176

178177
func loadConfig(path string) (*config, error) {
179-
confBytes, err := ioutil.ReadFile(path)
178+
confBytes, err := os.ReadFile(path)
180179
if err != nil {
181180
return nil, fmt.Errorf("failed to open file %s: %w", path, err)
182181
}

hack/pipeline-validator/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"encoding/json"
1313
"errors"
1414
"fmt"
15-
"io/ioutil"
15+
"io"
1616
"log"
1717
"mime/multipart"
1818
"net/http"
@@ -96,7 +96,7 @@ func getToken() (*CSRFToken, error) {
9696
return nil, fmt.Errorf("received status code %d", resp.StatusCode)
9797
}
9898

99-
bytes, err := ioutil.ReadAll(resp.Body)
99+
bytes, err := io.ReadAll(resp.Body)
100100
if err != nil {
101101
return nil, err
102102
}
@@ -111,7 +111,7 @@ func getToken() (*CSRFToken, error) {
111111
}
112112

113113
func validate(token *CSRFToken, pipeline string) (string, error) {
114-
bytez, err := ioutil.ReadFile(pipeline)
114+
bytez, err := os.ReadFile(pipeline)
115115
if err != nil {
116116
return "", err
117117
}
@@ -147,7 +147,7 @@ func validate(token *CSRFToken, pipeline string) (string, error) {
147147
return "", fmt.Errorf("received status code %d", resp.StatusCode)
148148
}
149149

150-
bytez, err = ioutil.ReadAll(resp.Body)
150+
bytez, err = io.ReadAll(resp.Body)
151151
if err != nil {
152152
return "", err
153153
}

hack/release-notes/github/pr_loader.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"encoding/json"
1111
"fmt"
1212
"io"
13-
"io/ioutil"
1413
"net/http"
1514
"os"
1615
"time"
@@ -126,7 +125,7 @@ func (loader *prLoader) mkRequest(client *http.Client, cursor *string) (*apiResp
126125

127126
defer func() {
128127
if resp.Body != nil {
129-
_, _ = io.Copy(ioutil.Discard, resp.Body)
128+
_, _ = io.Copy(io.Discard, resp.Body)
130129
resp.Body.Close()
131130
}
132131
}()
@@ -135,7 +134,7 @@ func (loader *prLoader) mkRequest(client *http.Client, cursor *string) (*apiResp
135134
return nil, fmt.Errorf("received status %s from the API", resp.Status)
136135
}
137136

138-
body, err := ioutil.ReadAll(io.LimitReader(resp.Body, maxPayloadSize))
137+
body, err := io.ReadAll(io.LimitReader(resp.Body, maxPayloadSize))
139138
if err != nil {
140139
return nil, fmt.Errorf("failed to read response body: %w", err)
141140
}

hack/release-notes/github/pr_loader_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import (
88
"bytes"
99
"encoding/json"
1010
"io"
11-
"io/ioutil"
1211
"net/http"
1312
"net/http/httptest"
13+
"os"
1414
"testing"
1515

1616
"github.com/stretchr/testify/require"
@@ -36,10 +36,10 @@ var (
3636
)
3737

3838
func TestDoLoadPullRequests(t *testing.T) {
39-
firstPayload, err := ioutil.ReadFile("testdata/payload1.json")
39+
firstPayload, err := os.ReadFile("testdata/payload1.json")
4040
require.NoError(t, err)
4141

42-
secondPayload, err := ioutil.ReadFile("testdata/payload2.json")
42+
secondPayload, err := os.ReadFile("testdata/payload2.json")
4343
require.NoError(t, err)
4444

4545
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -48,7 +48,7 @@ func TestDoLoadPullRequests(t *testing.T) {
4848
Variables map[string]interface{} `json:"variables"`
4949
}
5050

51-
reqBytes, err := ioutil.ReadAll(r.Body)
51+
reqBytes, err := io.ReadAll(r.Body)
5252
if err != nil {
5353
http.Error(w, err.Error(), http.StatusInternalServerError)
5454
return

hack/release-notes/github/pr_processor_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
package github
66

77
import (
8-
"io/ioutil"
8+
"os"
99
"testing"
1010

1111
"github.com/stretchr/testify/require"
1212
)
1313

1414
func TestExtractIssues(t *testing.T) {
15-
issueBody, err := ioutil.ReadFile("testdata/issue_body.txt")
15+
issueBody, err := os.ReadFile("testdata/issue_body.txt")
1616
require.NoError(t, err)
1717

1818
want := []int{3040, 3042, 3133, 3134}

0 commit comments

Comments
 (0)