Skip to content

Commit 9ffd621

Browse files
committed
Remove io/ioutil
The package is still there, but golangci-lint recommends using io or os. Signed-off-by: Kazuyoshi Kato <[email protected]>
1 parent 9e421b9 commit 9ffd621

File tree

17 files changed

+40
-53
lines changed

17 files changed

+40
-53
lines changed

agent/drive_handler.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package main
1616
import (
1717
"context"
1818
"fmt"
19-
"io/ioutil"
2019
"os"
2120
"path/filepath"
2221
"strings"
@@ -124,7 +123,7 @@ func (d drive) Path() string {
124123

125124
func getListOfBlockDeviceNames(path string) ([]string, error) {
126125
names := []string{}
127-
infos, err := ioutil.ReadDir(path)
126+
infos, err := os.ReadDir(path)
128127
if err != nil {
129128
return nil, err
130129
}
@@ -144,7 +143,7 @@ func (dh driveHandler) buildDrive(name string) (drive, error) {
144143
DrivePath: dh.DrivePath,
145144
}
146145

147-
majorMinorStr, err := ioutil.ReadFile(filepath.Join(dh.BlockPath, name, blockMajorMinor))
146+
majorMinorStr, err := os.ReadFile(filepath.Join(dh.BlockPath, name, blockMajorMinor))
148147
if err != nil {
149148
return d, err
150149
}

config/config.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package config
1616
import (
1717
"encoding/json"
1818
"fmt"
19-
"io/ioutil"
2019
"os"
2120

2221
"github.com/firecracker-microvm/firecracker-containerd/internal"
@@ -78,7 +77,7 @@ func LoadConfig(path string) (*Config, error) {
7877
path = defaultConfigPath
7978
}
8079

81-
data, err := ioutil.ReadFile(path)
80+
data, err := os.ReadFile(path)
8281
if err != nil {
8382
return nil, fmt.Errorf("failed to read config from %q: %w", path, err)
8483
}

config/config_test.go

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

1616
import (
1717
"fmt"
18-
"io/ioutil"
1918
"os"
2019
"testing"
2120

@@ -63,11 +62,11 @@ func TestLoadConfigOverrides(t *testing.T) {
6362

6463
func createTempConfig(t *testing.T, contents string) (string, func()) {
6564
t.Helper()
66-
configFile, err := ioutil.TempFile("", "config")
65+
configFile, err := os.CreateTemp("", "config")
6766
if err != nil {
6867
t.Fatal(err, "failed to create temp config file")
6968
}
70-
err = ioutil.WriteFile(configFile.Name(), []byte(contents), 0644)
69+
err = os.WriteFile(configFile.Name(), []byte(contents), 0644)
7170
if err != nil {
7271
os.Remove(configFile.Name())
7372
t.Fatal(err, "failed to write contents to temp config file")

docker-credential-mmds/mmds/client.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"encoding/json"
1717
"errors"
1818
"fmt"
19-
"io/ioutil"
19+
"io"
2020
"net/http"
2121
"net/url"
2222
"strings"
@@ -83,7 +83,7 @@ func (c *Client) refreshToken() error {
8383
c.version = v1
8484
return nil
8585
}
86-
token, err := ioutil.ReadAll(resp.Body)
86+
token, err := io.ReadAll(resp.Body)
8787
if err != nil {
8888
return err
8989
}
@@ -131,11 +131,11 @@ func (c *Client) getMetadata(path string) ([]byte, error) {
131131
func readBody(resp *http.Response) ([]byte, error) {
132132
switch status := resp.StatusCode; {
133133
case status < 300:
134-
return ioutil.ReadAll(resp.Body)
134+
return io.ReadAll(resp.Body)
135135
case status == http.StatusUnauthorized:
136136
return []byte{}, errUnauthorized
137137
default:
138-
body, err := ioutil.ReadAll(resp.Body)
138+
body, err := io.ReadAll(resp.Body)
139139
return []byte{}, fmt.Errorf("unexpected http response: %d - (err %v) %s", status, err, string(body))
140140
}
141141
}

docker-credential-mmds/mmds/client_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"errors"
1818
"fmt"
1919
"io"
20-
"io/ioutil"
2120
"net/http"
2221
"testing"
2322

@@ -46,7 +45,7 @@ func v1TokenResponse() (*http.Response, error) {
4645
func v2TokenResponse() (*http.Response, error) {
4746
return &http.Response{
4847
StatusCode: http.StatusOK,
49-
Body: ioutil.NopCloser(bytes.NewBufferString(token)),
48+
Body: io.NopCloser(bytes.NewBufferString(token)),
5049
}, nil
5150
}
5251

docker-credential-mmds/mmds/helper_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ package mmds
1616
import (
1717
"bytes"
1818
"fmt"
19-
"io/ioutil"
19+
"io"
2020
"net/http"
2121
"testing"
2222

@@ -63,14 +63,14 @@ const (
6363
func validMetadataResponse() (*http.Response, error) {
6464
return &http.Response{
6565
StatusCode: http.StatusOK,
66-
Body: ioutil.NopCloser(bytes.NewBufferString(validDockerCredentials)),
66+
Body: io.NopCloser(bytes.NewBufferString(validDockerCredentials)),
6767
}, nil
6868
}
6969

7070
func metadataResponse() (*http.Response, error) {
7171
return &http.Response{
7272
StatusCode: http.StatusOK,
73-
Body: ioutil.NopCloser(bytes.NewBufferString(dockerCredentials)),
73+
Body: io.NopCloser(bytes.NewBufferString(dockerCredentials)),
7474
}, nil
7575
}
7676

examples/taskworkflow.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"context"
1818
"flag"
1919
"fmt"
20-
"io/ioutil"
20+
"io"
2121
"log"
2222
"net"
2323
"net/http"
@@ -197,7 +197,7 @@ func getResponse(containerIP string) error {
197197
}
198198
defer response.Body.Close()
199199

200-
contents, err := ioutil.ReadAll(response.Body)
200+
contents, err := io.ReadAll(response.Body)
201201
if err != nil {
202202
return fmt.Errorf("Unable to read response body from %s: %w", containerIP, err)
203203
}

internal/bundle/bundle.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package bundle
1616
import (
1717
"encoding/json"
1818
"fmt"
19-
"io/ioutil"
2019
"os"
2120
"path/filepath"
2221

@@ -91,7 +90,7 @@ func (c *OCIConfig) File() (*os.File, error) {
9190

9291
// Bytes returns the bytes of config.json
9392
func (c *OCIConfig) Bytes() ([]byte, error) {
94-
f, err := ioutil.ReadFile(c.path)
93+
f, err := os.ReadFile(c.path)
9594
if err != nil {
9695
return nil, fmt.Errorf("failed to read OCI config file %s: %w", c.path, err)
9796
}
@@ -101,7 +100,7 @@ func (c *OCIConfig) Bytes() ([]byte, error) {
101100

102101
// Write will create or overwrite the config.json with the provided bytes
103102
func (c *OCIConfig) Write(contents []byte) error {
104-
err := ioutil.WriteFile(c.path, contents, 0700)
103+
err := os.WriteFile(c.path, contents, 0700)
105104
if err != nil {
106105
return fmt.Errorf("failed to write OCI config file %s: %w", c.path, err)
107106
}

internal/fsutil.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package internal
1616
import (
1717
"context"
1818
"fmt"
19-
"io/ioutil"
2019
"os"
2120
"os/exec"
2221
"path/filepath"
@@ -52,7 +51,7 @@ type FSImgFile struct {
5251
func createTestExtImg(ctx context.Context, t *testing.T, extName string, testFiles ...FSImgFile) string {
5352
t.Helper()
5453

55-
tempdir, err := ioutil.TempDir("", "")
54+
tempdir, err := os.MkdirTemp("", "")
5655
require.NoError(t, err, "failed to create temp dir for ext img")
5756

5857
for _, testFile := range testFiles {
@@ -61,11 +60,11 @@ func createTestExtImg(ctx context.Context, t *testing.T, extName string, testFil
6160
err = os.MkdirAll(filepath.Dir(destPath), 0750)
6261
require.NoError(t, err, "failed to mkdir for contents of ext img file")
6362

64-
err = ioutil.WriteFile(destPath, []byte(testFile.Contents), 0750)
63+
err = os.WriteFile(destPath, []byte(testFile.Contents), 0750)
6564
require.NoError(t, err, "failed to write file for contents of ext img")
6665
}
6766

68-
imgFile, err := ioutil.TempFile("", "")
67+
imgFile, err := os.CreateTemp("", "")
6968
require.NoError(t, err, "failed to obtain temp file for ext img")
7069

7170
output, err := exec.CommandContext(ctx, "mkfs."+extName, "-d", tempdir, imgFile.Name(), "65536").CombinedOutput()
@@ -77,7 +76,7 @@ func createTestExtImg(ctx context.Context, t *testing.T, extName string, testFil
7776
func CreateBlockDevice(ctx context.Context, t *testing.T) (string, func()) {
7877
t.Helper()
7978

80-
f, err := ioutil.TempFile("", "")
79+
f, err := os.CreateTemp("", "")
8180
require.NoError(t, err)
8281

8382
err = f.Truncate(32 * mib)

internal/vm/ioproxy_test.go

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

1616
import (
1717
"context"
18-
"io/ioutil"
1918
"os"
2019
"path/filepath"
2120
"testing"
@@ -46,7 +45,7 @@ func TestProxy(t *testing.T) {
4645
ctx := context.Background()
4746
content := "hello world"
4847

49-
err := ioutil.WriteFile(filepath.Join(dir, "input"), []byte(content), 0600)
48+
err := os.WriteFile(filepath.Join(dir, "input"), []byte(content), 0600)
5049
require.NoError(t, err)
5150

5251
pair := &IOConnectorPair{
@@ -58,7 +57,7 @@ func TestProxy(t *testing.T) {
5857
assert.Nil(t, <-initCh)
5958
assert.Nil(t, <-copyCh)
6059

61-
bytes, err := ioutil.ReadFile(filepath.Join(dir, "output"))
60+
bytes, err := os.ReadFile(filepath.Join(dir, "output"))
6261
require.NoError(t, err)
6362
assert.Equal(t, content, string(bytes))
6463
}

0 commit comments

Comments
 (0)