Skip to content

Commit c55a4ac

Browse files
committed
refactor: move from io/ioutil to io and os package
The io/ioutil package has been deprecated in Go 1.16. This commit replaces the existing io/ioutil functions with their new definitions in io and os packages. Signed-off-by: Eng Zer Jun <[email protected]>
1 parent 2b70006 commit c55a4ac

397 files changed

Lines changed: 1371 additions & 1606 deletions

File tree

Some content is hidden

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

builder/builder-next/adapters/containerimage/pull.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/json"
66
"fmt"
77
"io"
8-
"io/ioutil"
98
"path"
109
"runtime"
1110
"sync"
@@ -640,7 +639,7 @@ func (ld *layerDescriptor) Download(ctx context.Context, progressOutput pkgprogr
640639
return nil, 0, err
641640
}
642641

643-
return ioutil.NopCloser(content.NewReader(ra)), ld.desc.Size, nil
642+
return io.NopCloser(content.NewReader(ra)), ld.desc.Size, nil
644643
}
645644

646645
func (ld *layerDescriptor) Close() {

builder/builder-next/executor_unix.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package buildkit
55

66
import (
7-
"io/ioutil"
87
"os"
98
"path/filepath"
109
"strconv"
@@ -35,7 +34,7 @@ func newExecutor(root, cgroupParent string, net libnetwork.NetworkController, dn
3534
}
3635

3736
// make sure net state directory is cleared from previous state
38-
fis, err := ioutil.ReadDir(netRoot)
37+
fis, err := os.ReadDir(netRoot)
3938
if err == nil {
4039
for _, fi := range fis {
4140
fp := filepath.Join(netRoot, fi.Name())

builder/builder-next/worker/worker.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"io"
7-
"io/ioutil"
87
nethttp "net/http"
98
"runtime"
109
"strings"
@@ -445,7 +444,7 @@ func (ld *layerDescriptor) Download(ctx context.Context, progressOutput pkgprogr
445444
return nil, 0, err
446445
}
447446

448-
return ioutil.NopCloser(content.NewReader(ra)), ld.desc.Size, nil
447+
return io.NopCloser(content.NewReader(ra)), ld.desc.Size, nil
449448
}
450449

451450
func (ld *layerDescriptor) Close() {

builder/dockerfile/builder.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"context"
66
"fmt"
77
"io"
8-
"io/ioutil"
98
"sort"
109
"strings"
1110

@@ -346,8 +345,8 @@ func BuildFromConfig(config *container.Config, changes []string, os string) (*co
346345
}
347346
}
348347

349-
b.Stdout = ioutil.Discard
350-
b.Stderr = ioutil.Discard
348+
b.Stdout = io.Discard
349+
b.Stderr = io.Discard
351350
b.disableCommit = true
352351

353352
var commands []instructions.Command

builder/dockerfile/utils_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package dockerfile // import "github.com/docker/docker/builder/dockerfile"
22

33
import (
4-
"io/ioutil"
54
"os"
65
"path/filepath"
76
"testing"
@@ -11,7 +10,7 @@ import (
1110
// It returns the created path and a cleanup function which is meant to be used as deferred call.
1211
// When an error occurs, it terminates the test.
1312
func createTestTempDir(t *testing.T, dir, prefix string) (string, func()) {
14-
path, err := ioutil.TempDir(dir, prefix)
13+
path, err := os.MkdirTemp(dir, prefix)
1514

1615
if err != nil {
1716
t.Fatalf("Error when creating directory %s with prefix %s: %s", dir, prefix, err)
@@ -30,7 +29,7 @@ func createTestTempDir(t *testing.T, dir, prefix string) (string, func()) {
3029
// When an error occurs, it terminates the test
3130
func createTestTempFile(t *testing.T, dir, filename, contents string, perm os.FileMode) string {
3231
filePath := filepath.Join(dir, filename)
33-
err := ioutil.WriteFile(filePath, []byte(contents), perm)
32+
err := os.WriteFile(filePath, []byte(contents), perm)
3433

3534
if err != nil {
3635
t.Fatalf("Error when creating %s file: %s", filename, err)

builder/remotecontext/detect_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package remotecontext // import "github.com/docker/docker/builder/remotecontext"
22

33
import (
44
"errors"
5-
"io/ioutil"
65
"log"
76
"os"
87
"sort"
@@ -20,7 +19,7 @@ const (
2019

2120
const shouldStayFilename = "should_stay"
2221

23-
func extractFilenames(files []os.FileInfo) []string {
22+
func extractFilenames(files []os.DirEntry) []string {
2423
filenames := make([]string, len(files))
2524

2625
for i, file := range files {
@@ -31,7 +30,7 @@ func extractFilenames(files []os.FileInfo) []string {
3130
}
3231

3332
func checkDirectory(t *testing.T, dir string, expectedFiles []string) {
34-
files, err := ioutil.ReadDir(dir)
33+
files, err := os.ReadDir(dir)
3534

3635
if err != nil {
3736
t.Fatalf("Could not read directory: %s", err)

builder/remotecontext/git/gitutils.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package git // import "github.com/docker/docker/builder/remotecontext/git"
22

33
import (
4-
"io/ioutil"
54
"net/http"
65
"net/url"
76
"os"
@@ -34,7 +33,7 @@ func Clone(remoteURL string) (string, error) {
3433
func cloneGitRepo(repo gitRepo) (checkoutDir string, err error) {
3534
fetch := fetchArgs(repo.remote, repo.ref)
3635

37-
root, err := ioutil.TempDir("", "docker-build-git")
36+
root, err := os.MkdirTemp("", "docker-build-git")
3837
if err != nil {
3938
return "", err
4039
}

builder/remotecontext/git/gitutils_test.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package git // import "github.com/docker/docker/builder/remotecontext/git"
22

33
import (
44
"fmt"
5-
"io/ioutil"
65
"net/http"
76
"net/http/httptest"
87
"net/url"
@@ -171,7 +170,7 @@ func gitGetConfig(name string) string {
171170
}
172171

173172
func TestCheckoutGit(t *testing.T) {
174-
root, err := ioutil.TempDir("", "docker-build-git-checkout")
173+
root, err := os.MkdirTemp("", "docker-build-git-checkout")
175174
assert.NilError(t, err)
176175
defer os.RemoveAll(root)
177176

@@ -195,13 +194,13 @@ func TestCheckoutGit(t *testing.T) {
195194
_, err = gitWithinDir(gitDir, "config", "user.name", "Docker test")
196195
assert.NilError(t, err)
197196

198-
err = ioutil.WriteFile(filepath.Join(gitDir, "Dockerfile"), []byte("FROM scratch"), 0644)
197+
err = os.WriteFile(filepath.Join(gitDir, "Dockerfile"), []byte("FROM scratch"), 0644)
199198
assert.NilError(t, err)
200199

201200
subDir := filepath.Join(gitDir, "subdir")
202201
assert.NilError(t, os.Mkdir(subDir, 0755))
203202

204-
err = ioutil.WriteFile(filepath.Join(subDir, "Dockerfile"), []byte("FROM scratch\nEXPOSE 5000"), 0644)
203+
err = os.WriteFile(filepath.Join(subDir, "Dockerfile"), []byte("FROM scratch\nEXPOSE 5000"), 0644)
205204
assert.NilError(t, err)
206205

207206
if runtime.GOOS != "windows" {
@@ -223,10 +222,10 @@ func TestCheckoutGit(t *testing.T) {
223222
_, err = gitWithinDir(gitDir, "checkout", "-b", "test")
224223
assert.NilError(t, err)
225224

226-
err = ioutil.WriteFile(filepath.Join(gitDir, "Dockerfile"), []byte("FROM scratch\nEXPOSE 3000"), 0644)
225+
err = os.WriteFile(filepath.Join(gitDir, "Dockerfile"), []byte("FROM scratch\nEXPOSE 3000"), 0644)
227226
assert.NilError(t, err)
228227

229-
err = ioutil.WriteFile(filepath.Join(subDir, "Dockerfile"), []byte("FROM busybox\nEXPOSE 5000"), 0644)
228+
err = os.WriteFile(filepath.Join(subDir, "Dockerfile"), []byte("FROM busybox\nEXPOSE 5000"), 0644)
230229
assert.NilError(t, err)
231230

232231
_, err = gitWithinDir(gitDir, "add", "-A")
@@ -249,7 +248,7 @@ func TestCheckoutGit(t *testing.T) {
249248
_, err = gitWithinDir(subrepoDir, "config", "user.name", "Docker test")
250249
assert.NilError(t, err)
251250

252-
err = ioutil.WriteFile(filepath.Join(subrepoDir, "subfile"), []byte("subcontents"), 0644)
251+
err = os.WriteFile(filepath.Join(subrepoDir, "subfile"), []byte("subcontents"), 0644)
253252
assert.NilError(t, err)
254253

255254
_, err = gitWithinDir(subrepoDir, "add", "-A")
@@ -310,7 +309,7 @@ func TestCheckoutGit(t *testing.T) {
310309
assert.NilError(t, err)
311310
defer os.RemoveAll(r)
312311
if c.submodule {
313-
b, err := ioutil.ReadFile(filepath.Join(r, "sub/subfile"))
312+
b, err := os.ReadFile(filepath.Join(r, "sub/subfile"))
314313
assert.NilError(t, err)
315314
assert.Check(t, is.Equal("subcontents", string(b)))
316315
} else {
@@ -319,7 +318,7 @@ func TestCheckoutGit(t *testing.T) {
319318
assert.Assert(t, os.IsNotExist(err))
320319
}
321320

322-
b, err := ioutil.ReadFile(filepath.Join(r, "Dockerfile"))
321+
b, err := os.ReadFile(filepath.Join(r, "Dockerfile"))
323322
assert.NilError(t, err)
324323
assert.Check(t, is.Equal(c.exp, string(b)))
325324
}

builder/remotecontext/remote.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"fmt"
66
"io"
7-
"io/ioutil"
87
"net"
98
"net/http"
109
"net/url"
@@ -58,7 +57,7 @@ func GetWithStatusError(address string) (resp *http.Response, err error) {
5857
return resp, nil
5958
}
6059
msg := fmt.Sprintf("failed to GET %s with status %s", address, resp.Status)
61-
body, err := ioutil.ReadAll(resp.Body)
60+
body, err := io.ReadAll(resp.Body)
6261
resp.Body.Close()
6362
if err != nil {
6463
return nil, errdefs.System(errors.New(msg + ": error reading body"))

builder/remotecontext/remote_test.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package remotecontext // import "github.com/docker/docker/builder/remotecontext"
33
import (
44
"bytes"
55
"io"
6-
"io/ioutil"
76
"net/http"
87
"net/http/httptest"
98
"net/url"
@@ -52,15 +51,15 @@ func TestSelectAcceptableMIME(t *testing.T) {
5251

5352
func TestInspectEmptyResponse(t *testing.T) {
5453
ct := "application/octet-stream"
55-
br := ioutil.NopCloser(bytes.NewReader([]byte("")))
54+
br := io.NopCloser(bytes.NewReader([]byte("")))
5655
contentType, bReader, err := inspectResponse(ct, br, 0)
5756
if err == nil {
5857
t.Fatal("Should have generated an error for an empty response")
5958
}
6059
if contentType != "application/octet-stream" {
6160
t.Fatalf("Content type should be 'application/octet-stream' but is %q", contentType)
6261
}
63-
body, err := ioutil.ReadAll(bReader)
62+
body, err := io.ReadAll(bReader)
6463
if err != nil {
6564
t.Fatal(err)
6665
}
@@ -71,15 +70,15 @@ func TestInspectEmptyResponse(t *testing.T) {
7170

7271
func TestInspectResponseBinary(t *testing.T) {
7372
ct := "application/octet-stream"
74-
br := ioutil.NopCloser(bytes.NewReader(binaryContext))
73+
br := io.NopCloser(bytes.NewReader(binaryContext))
7574
contentType, bReader, err := inspectResponse(ct, br, int64(len(binaryContext)))
7675
if err != nil {
7776
t.Fatal(err)
7877
}
7978
if contentType != "application/octet-stream" {
8079
t.Fatalf("Content type should be 'application/octet-stream' but is %q", contentType)
8180
}
82-
body, err := ioutil.ReadAll(bReader)
81+
body, err := io.ReadAll(bReader)
8382
if err != nil {
8483
t.Fatal(err)
8584
}
@@ -96,7 +95,7 @@ func TestInspectResponseBinary(t *testing.T) {
9695
func TestResponseUnsupportedContentType(t *testing.T) {
9796
content := []byte(dockerfileContents)
9897
ct := "application/json"
99-
br := ioutil.NopCloser(bytes.NewReader(content))
98+
br := io.NopCloser(bytes.NewReader(content))
10099
contentType, bReader, err := inspectResponse(ct, br, int64(len(dockerfileContents)))
101100

102101
if err == nil {
@@ -105,7 +104,7 @@ func TestResponseUnsupportedContentType(t *testing.T) {
105104
if contentType != ct {
106105
t.Fatalf("Should not have altered content-type: orig: %s, altered: %s", ct, contentType)
107106
}
108-
body, err := ioutil.ReadAll(bReader)
107+
body, err := io.ReadAll(bReader)
109108
if err != nil {
110109
t.Fatal(err)
111110
}
@@ -117,15 +116,15 @@ func TestResponseUnsupportedContentType(t *testing.T) {
117116
func TestInspectResponseTextSimple(t *testing.T) {
118117
content := []byte(dockerfileContents)
119118
ct := "text/plain"
120-
br := ioutil.NopCloser(bytes.NewReader(content))
119+
br := io.NopCloser(bytes.NewReader(content))
121120
contentType, bReader, err := inspectResponse(ct, br, int64(len(content)))
122121
if err != nil {
123122
t.Fatal(err)
124123
}
125124
if contentType != "text/plain" {
126125
t.Fatalf("Content type should be 'text/plain' but is %q", contentType)
127126
}
128-
body, err := ioutil.ReadAll(bReader)
127+
body, err := io.ReadAll(bReader)
129128
if err != nil {
130129
t.Fatal(err)
131130
}
@@ -136,15 +135,15 @@ func TestInspectResponseTextSimple(t *testing.T) {
136135

137136
func TestInspectResponseEmptyContentType(t *testing.T) {
138137
content := []byte(dockerfileContents)
139-
br := ioutil.NopCloser(bytes.NewReader(content))
138+
br := io.NopCloser(bytes.NewReader(content))
140139
contentType, bodyReader, err := inspectResponse("", br, int64(len(content)))
141140
if err != nil {
142141
t.Fatal(err)
143142
}
144143
if contentType != "text/plain" {
145144
t.Fatalf("Content type should be 'text/plain' but is %q", contentType)
146145
}
147-
body, err := ioutil.ReadAll(bodyReader)
146+
body, err := io.ReadAll(bodyReader)
148147
if err != nil {
149148
t.Fatal(err)
150149
}
@@ -156,15 +155,15 @@ func TestInspectResponseEmptyContentType(t *testing.T) {
156155
func TestUnknownContentLength(t *testing.T) {
157156
content := []byte(dockerfileContents)
158157
ct := "text/plain"
159-
br := ioutil.NopCloser(bytes.NewReader(content))
158+
br := io.NopCloser(bytes.NewReader(content))
160159
contentType, bReader, err := inspectResponse(ct, br, -1)
161160
if err != nil {
162161
t.Fatal(err)
163162
}
164163
if contentType != "text/plain" {
165164
t.Fatalf("Content type should be 'text/plain' but is %q", contentType)
166165
}
167-
body, err := ioutil.ReadAll(bReader)
166+
body, err := io.ReadAll(bReader)
168167
if err != nil {
169168
t.Fatal(err)
170169
}
@@ -191,7 +190,7 @@ func TestDownloadRemote(t *testing.T) {
191190
assert.NilError(t, err)
192191

193192
assert.Check(t, is.Equal(mimeTypes.TextPlain, contentType))
194-
raw, err := ioutil.ReadAll(content)
193+
raw, err := io.ReadAll(content)
195194
assert.NilError(t, err)
196195
assert.Check(t, is.Equal(dockerfileContents, string(raw)))
197196
}
@@ -238,5 +237,5 @@ func TestGetWithStatusError(t *testing.T) {
238237

239238
func readBody(b io.ReadCloser) ([]byte, error) {
240239
defer b.Close()
241-
return ioutil.ReadAll(b)
240+
return io.ReadAll(b)
242241
}

0 commit comments

Comments
 (0)