Skip to content

Commit fa277a9

Browse files
committed
Drop io/ioutil and fix staticcheck
1 parent 6c62754 commit fa277a9

18 files changed

+81
-86
lines changed

auth.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"encoding/json"
1111
"errors"
1212
"io"
13-
"io/ioutil"
1413
"net/http"
1514
"os"
1615
"os/exec"
@@ -272,7 +271,7 @@ func (c *Client) AuthCheck(conf *AuthConfiguration) (AuthStatus, error) {
272271
return authStatus, err
273272
}
274273
defer resp.Body.Close()
275-
data, err := ioutil.ReadAll(resp.Body)
274+
data, err := io.ReadAll(resp.Body)
276275
if err != nil {
277276
return authStatus, err
278277
}
@@ -318,7 +317,7 @@ func NewAuthConfigurationsFromCredsHelpers(registry string) (*AuthConfiguration,
318317

319318
func getHelperProviderFromDockerCfg(pathsToTry []string, registry string) (string, error) {
320319
for _, path := range pathsToTry {
321-
content, err := ioutil.ReadFile(path)
320+
content, err := os.ReadFile(path)
322321
if err != nil {
323322
// if we can't read the file keep going
324323
continue

auth_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"encoding/base64"
99
"errors"
1010
"fmt"
11-
"io/ioutil"
1211
"net/http"
1312
"os"
1413
"path"
@@ -43,15 +42,15 @@ func TestAuthConfigurationSearchPath(t *testing.T) {
4342

4443
func TestAuthConfigurationsFromFile(t *testing.T) {
4544
t.Parallel()
46-
tmpDir, err := ioutil.TempDir("", "go-dockerclient-auth-test")
45+
tmpDir, err := os.MkdirTemp("", "go-dockerclient-auth-test")
4746
if err != nil {
4847
t.Fatalf("Unable to create temporary directory for TestAuthConfigurationsFromFile: %s", err)
4948
}
5049
defer os.RemoveAll(tmpDir)
5150
authString := base64.StdEncoding.EncodeToString([]byte("user:pass"))
5251
content := fmt.Sprintf(`{"auths":{"foo": {"auth": "%s"}}}`, authString)
5352
configFile := path.Join(tmpDir, "docker_config")
54-
if err = ioutil.WriteFile(configFile, []byte(content), 0o600); err != nil {
53+
if err = os.WriteFile(configFile, []byte(content), 0o600); err != nil {
5554
t.Errorf("Error writing auth config for TestAuthConfigurationsFromFile: %s", err)
5655
}
5756
auths, err := NewAuthConfigurationsFromFile(configFile)
@@ -65,7 +64,7 @@ func TestAuthConfigurationsFromFile(t *testing.T) {
6564

6665
func TestAuthConfigurationsFromDockerCfg(t *testing.T) {
6766
t.Parallel()
68-
tmpDir, err := ioutil.TempDir("", "go-dockerclient-auth-dockercfg-test")
67+
tmpDir, err := os.MkdirTemp("", "go-dockerclient-auth-dockercfg-test")
6968
if err != nil {
7069
t.Fatalf("Unable to create temporary directory for TestAuthConfigurationsFromDockerCfg: %s", err)
7170
}
@@ -80,7 +79,7 @@ func TestAuthConfigurationsFromDockerCfg(t *testing.T) {
8079
authString := base64.StdEncoding.EncodeToString([]byte("user:pass"))
8180
content := fmt.Sprintf(`{"auths":{"%s": {"auth": "%s"}}}`, key, authString)
8281
configFile := path.Join(tmpDir, fmt.Sprintf("docker_config_%d.json", i))
83-
if err = ioutil.WriteFile(configFile, []byte(content), 0o600); err != nil {
82+
if err = os.WriteFile(configFile, []byte(content), 0o600); err != nil {
8483
t.Errorf("Error writing auth config for TestAuthConfigurationsFromFile: %s", err)
8584
}
8685
pathsToTry = append(pathsToTry, configFile)
@@ -376,15 +375,15 @@ func TestAuthConfigurationsMerge(t *testing.T) {
376375

377376
func TestGetHelperProviderFromDockerCfg(t *testing.T) {
378377
t.Parallel()
379-
tmpDir, err := ioutil.TempDir("", "go-dockerclient-creds-test")
378+
tmpDir, err := os.MkdirTemp("", "go-dockerclient-creds-test")
380379
if err != nil {
381380
t.Fatalf("Unable to create temporary directory for TestGetHelperProviderFromDockerCfg: %s", err)
382381
}
383382
defer os.RemoveAll(tmpDir)
384383
expectedProvider := "ecr-login-test"
385384
content := fmt.Sprintf(`{"credsStore": "ecr-login","credHelpers":{"docker.io":"%s"}}`, expectedProvider)
386385
configFile := path.Join(tmpDir, "docker_config")
387-
if err = ioutil.WriteFile(configFile, []byte(content), 0o600); err != nil {
386+
if err = os.WriteFile(configFile, []byte(content), 0o600); err != nil {
388387
t.Errorf("Error writing auth config for TestGetHelperProviderFromDockerCfg: %s", err)
389388
}
390389

build_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"bytes"
99
"errors"
1010
"io"
11-
"io/ioutil"
1211
"net/http"
1312
"os"
1413
"path/filepath"
@@ -54,6 +53,9 @@ func TestBuildImageContextDirDockerignoreParsing(t *testing.T) {
5453
}
5554
}()
5655
workingdir, err := os.Getwd()
56+
if err != nil {
57+
t.Fatal(err)
58+
}
5759

5860
var buf bytes.Buffer
5961
opts := BuildImageOptions{
@@ -82,7 +84,7 @@ func TestBuildImageContextDirDockerignoreParsing(t *testing.T) {
8284
}
8385
}()
8486

85-
files, err := ioutil.ReadDir(tmpdir)
87+
files, err := os.ReadDir(tmpdir)
8688
if err != nil {
8789
t.Fatal(err)
8890
}
@@ -153,7 +155,7 @@ func TestBuildImageSendXRegistryConfig(t *testing.T) {
153155
}
154156

155157
func unpackBodyTarball(req io.Reader) (tmpdir string, err error) {
156-
tmpdir, err = ioutil.TempDir("", "go-dockerclient-test")
158+
tmpdir, err = os.MkdirTemp("", "go-dockerclient-test")
157159
if err != nil {
158160
return
159161
}

client.go

Lines changed: 8 additions & 9 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"
2221
"net/http"
2322
"net/http/httputil"
@@ -240,19 +239,19 @@ func NewVersionedTLSClient(endpoint string, cert, key, ca, apiVersionString stri
240239
var keyPEMBlock []byte
241240
var caPEMCert []byte
242241
if _, err := os.Stat(cert); !os.IsNotExist(err) {
243-
certPEMBlock, err = ioutil.ReadFile(cert)
242+
certPEMBlock, err = os.ReadFile(cert)
244243
if err != nil {
245244
return nil, err
246245
}
247246
}
248247
if _, err := os.Stat(key); !os.IsNotExist(err) {
249-
keyPEMBlock, err = ioutil.ReadFile(key)
248+
keyPEMBlock, err = os.ReadFile(key)
250249
if err != nil {
251250
return nil, err
252251
}
253252
}
254253
if _, err := os.Stat(ca); !os.IsNotExist(err) {
255-
caPEMCert, err = ioutil.ReadFile(ca)
254+
caPEMCert, err = os.ReadFile(ca)
256255
if err != nil {
257256
return nil, err
258257
}
@@ -565,10 +564,10 @@ func (c *Client) streamURL(method, url string, streamOptions streamOptions) erro
565564
protocol := c.endpointURL.Scheme
566565
address := c.endpointURL.Path
567566
if streamOptions.stdout == nil {
568-
streamOptions.stdout = ioutil.Discard
567+
streamOptions.stdout = io.Discard
569568
}
570569
if streamOptions.stderr == nil {
571-
streamOptions.stderr = ioutil.Discard
570+
streamOptions.stderr = io.Discard
572571
}
573572

574573
if protocol == unixProtocol || protocol == namedPipeProtocol {
@@ -798,10 +797,10 @@ func (c *Client) hijack(method, path string, hijackOptions hijackOptions) (Close
798797
// will "hang" until the container terminates, even though you're not reading
799798
// stdout/stderr
800799
if hijackOptions.stdout == nil {
801-
hijackOptions.stdout = ioutil.Discard
800+
hijackOptions.stdout = io.Discard
802801
}
803802
if hijackOptions.stderr == nil {
804-
hijackOptions.stderr = ioutil.Discard
803+
hijackOptions.stderr = io.Discard
805804
}
806805

807806
go func() {
@@ -1024,7 +1023,7 @@ func newError(resp *http.Response) *Error {
10241023
Message string `json:"message"`
10251024
}
10261025
defer resp.Body.Close()
1027-
data, err := ioutil.ReadAll(resp.Body)
1026+
data, err := io.ReadAll(resp.Body)
10281027
if err != nil {
10291028
return &Error{Status: resp.StatusCode, Message: fmt.Sprintf("cannot read body, err: %v", err)}
10301029
}

client_stress_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ package docker
99

1010
import (
1111
"fmt"
12-
"io/ioutil"
1312
"net/http"
1413
"net/http/httptest"
14+
"os"
1515
"reflect"
1616
"sort"
1717
"sync"
@@ -66,11 +66,11 @@ func TestClientDoConcurrentStress(t *testing.T) {
6666
}
6767
defer tt.srv.Close()
6868
if tt.withTLSClient {
69-
certPEMBlock, certErr := ioutil.ReadFile("testing/data/cert.pem")
69+
certPEMBlock, certErr := os.ReadFile("testing/data/cert.pem")
7070
if certErr != nil {
7171
t.Fatal(certErr)
7272
}
73-
keyPEMBlock, certErr := ioutil.ReadFile("testing/data/key.pem")
73+
keyPEMBlock, certErr := os.ReadFile("testing/data/key.pem")
7474
if certErr != nil {
7575
t.Fatal(certErr)
7676
}

client_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"errors"
1111
"fmt"
1212
"io"
13-
"io/ioutil"
1413
"net/http"
1514
"net/http/httptest"
1615
"net/url"
@@ -346,7 +345,7 @@ func TestGetFakeNativeURL(t *testing.T) {
346345

347346
func TestError(t *testing.T) {
348347
t.Parallel()
349-
fakeBody := ioutil.NopCloser(bytes.NewBufferString("bad parameter"))
348+
fakeBody := io.NopCloser(bytes.NewBufferString("bad parameter"))
350349
resp := &http.Response{
351350
StatusCode: 400,
352351
Body: fakeBody,
@@ -893,7 +892,7 @@ func (rt *FakeRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
893892
rt.requests = append(rt.requests, r)
894893
res := &http.Response{
895894
StatusCode: rt.status,
896-
Body: ioutil.NopCloser(body),
895+
Body: io.NopCloser(body),
897896
Header: make(http.Header),
898897
}
899898
for k, v := range rt.header {

client_unix_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
package docker
99

1010
import (
11-
"io/ioutil"
11+
"io"
1212
"net"
1313
"net/http"
1414
"net/http/httptest"
@@ -46,7 +46,7 @@ func TestNewTSLAPIClientUnixEndpoint(t *testing.T) {
4646
if err != nil {
4747
t.Fatal(err)
4848
}
49-
data, err := ioutil.ReadAll(rsp.Body)
49+
data, err := io.ReadAll(rsp.Body)
5050
if err != nil {
5151
t.Fatal(err)
5252
}
@@ -56,7 +56,7 @@ func TestNewTSLAPIClientUnixEndpoint(t *testing.T) {
5656
}
5757

5858
func newNativeServer(handler http.Handler) (*httptest.Server, func(), error) {
59-
tmpdir, err := ioutil.TempDir("", "socket")
59+
tmpdir, err := os.MkdirTemp("", "socket")
6060
if err != nil {
6161
return nil, nil, err
6262
}

container_commit_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package docker
33
import (
44
"bytes"
55
"encoding/json"
6-
"io/ioutil"
6+
"io"
77
"net/http"
88
"reflect"
99
"testing"
@@ -66,7 +66,7 @@ func TestCommitContainerParams(t *testing.T) {
6666
t.Errorf("Wrong HTTP method. Want POST. Got %s.", meth)
6767
}
6868
if test.body != nil {
69-
if requestBody, err := ioutil.ReadAll(fakeRT.requests[0].Body); err == nil {
69+
if requestBody, err := io.ReadAll(fakeRT.requests[0].Body); err == nil {
7070
if !bytes.Equal(requestBody, test.body) {
7171
t.Errorf("Expected body %#v, got %#v", test.body, requestBody)
7272
}

container_start_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package docker
33
import (
44
"context"
55
"errors"
6-
"io/ioutil"
6+
"io"
77
"net/http"
88
"net/url"
99
"reflect"
@@ -57,7 +57,7 @@ func TestStartContainerHostConfigAPI124(t *testing.T) {
5757
t.Errorf("StartContainer(%q): Unepected %q Content-Type in request.", id, contentType)
5858
}
5959
if req.Body != nil {
60-
data, _ := ioutil.ReadAll(req.Body)
60+
data, _ := io.ReadAll(req.Body)
6161
t.Errorf("StartContainer(%q): Unexpected data sent: %s", id, data)
6262
}
6363
}

container_unix_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ package docker
1010
import (
1111
"bufio"
1212
"bytes"
13-
"io/ioutil"
1413
"net"
1514
"net/http"
1615
"os"
@@ -54,7 +53,7 @@ func TestExportContainerViaUnixSocket(t *testing.T) {
5453

5554
func TestStatsTimeoutUnixSocket(t *testing.T) {
5655
t.Parallel()
57-
tmpdir, err := ioutil.TempDir("", "socket")
56+
tmpdir, err := os.MkdirTemp("", "socket")
5857
if err != nil {
5958
t.Fatal(err)
6059
}

event_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import (
88
"bufio"
99
"crypto/tls"
1010
"crypto/x509"
11-
"io/ioutil"
1211
"net/http"
1312
"net/http/httptest"
13+
"os"
1414
"strings"
1515
"testing"
1616
"time"
@@ -33,7 +33,7 @@ func TestTLSEventListeners(t *testing.T) {
3333
t.Fatalf("Error loading server key pair: %s", err)
3434
}
3535

36-
caCert, err := ioutil.ReadFile("testing/data/ca.pem")
36+
caCert, err := os.ReadFile("testing/data/ca.pem")
3737
if err != nil {
3838
t.Fatalf("Error loading ca certificate: %s", err)
3939
}

image_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"encoding/base64"
1010
"encoding/json"
1111
"errors"
12-
"io/ioutil"
12+
"io"
1313
"net"
1414
"net/http"
1515
"net/url"
@@ -682,7 +682,7 @@ func TestImportImageFromInput(t *testing.T) {
682682
if !reflect.DeepEqual(got, expected) {
683683
t.Errorf("ImportImage: wrong query string. Want %#v. Got %#v.", expected, got)
684684
}
685-
body, err := ioutil.ReadAll(req.Body)
685+
body, err := io.ReadAll(req.Body)
686686
if err != nil {
687687
t.Errorf("ImportImage: caugth error while reading body %#v", err.Error())
688688
}
@@ -712,7 +712,7 @@ func TestImportImageDoesNotPassInputIfSourceIsNotDash(t *testing.T) {
712712
if !reflect.DeepEqual(got, expected) {
713713
t.Errorf("ImportImage: wrong query string. Want %#v. Got %#v.", expected, got)
714714
}
715-
body, err := ioutil.ReadAll(req.Body)
715+
body, err := io.ReadAll(req.Body)
716716
if err != nil {
717717
t.Errorf("ImportImage: caugth error while reading body %#v", err.Error())
718718
}
@@ -740,11 +740,11 @@ func TestImportImageShouldPassTarContentToBodyWhenSourceIsFilePath(t *testing.T)
740740
t.Fatal(err)
741741
}
742742
req := fakeRT.requests[0]
743-
tarContent, err := ioutil.ReadAll(tar)
743+
tarContent, err := io.ReadAll(tar)
744744
if err != nil {
745745
t.Fatal(err)
746746
}
747-
body, err := ioutil.ReadAll(req.Body)
747+
body, err := io.ReadAll(req.Body)
748748
if err != nil {
749749
t.Fatal(err)
750750
}

0 commit comments

Comments
 (0)