Skip to content

Commit

Permalink
Drop io/ioutil and fix staticcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
fsouza committed Apr 26, 2022
1 parent 6c62754 commit fa277a9
Show file tree
Hide file tree
Showing 18 changed files with 81 additions and 86 deletions.
5 changes: 2 additions & 3 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
Expand Down Expand Up @@ -272,7 +271,7 @@ func (c *Client) AuthCheck(conf *AuthConfiguration) (AuthStatus, error) {
return authStatus, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
data, err := io.ReadAll(resp.Body)
if err != nil {
return authStatus, err
}
Expand Down Expand Up @@ -318,7 +317,7 @@ func NewAuthConfigurationsFromCredsHelpers(registry string) (*AuthConfiguration,

func getHelperProviderFromDockerCfg(pathsToTry []string, registry string) (string, error) {
for _, path := range pathsToTry {
content, err := ioutil.ReadFile(path)
content, err := os.ReadFile(path)
if err != nil {
// if we can't read the file keep going
continue
Expand Down
13 changes: 6 additions & 7 deletions auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"path"
Expand Down Expand Up @@ -43,15 +42,15 @@ func TestAuthConfigurationSearchPath(t *testing.T) {

func TestAuthConfigurationsFromFile(t *testing.T) {
t.Parallel()
tmpDir, err := ioutil.TempDir("", "go-dockerclient-auth-test")
tmpDir, err := os.MkdirTemp("", "go-dockerclient-auth-test")
if err != nil {
t.Fatalf("Unable to create temporary directory for TestAuthConfigurationsFromFile: %s", err)
}
defer os.RemoveAll(tmpDir)
authString := base64.StdEncoding.EncodeToString([]byte("user:pass"))
content := fmt.Sprintf(`{"auths":{"foo": {"auth": "%s"}}}`, authString)
configFile := path.Join(tmpDir, "docker_config")
if err = ioutil.WriteFile(configFile, []byte(content), 0o600); err != nil {
if err = os.WriteFile(configFile, []byte(content), 0o600); err != nil {
t.Errorf("Error writing auth config for TestAuthConfigurationsFromFile: %s", err)
}
auths, err := NewAuthConfigurationsFromFile(configFile)
Expand All @@ -65,7 +64,7 @@ func TestAuthConfigurationsFromFile(t *testing.T) {

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

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

Expand Down
8 changes: 5 additions & 3 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"bytes"
"errors"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -54,6 +53,9 @@ func TestBuildImageContextDirDockerignoreParsing(t *testing.T) {
}
}()
workingdir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}

var buf bytes.Buffer
opts := BuildImageOptions{
Expand Down Expand Up @@ -82,7 +84,7 @@ func TestBuildImageContextDirDockerignoreParsing(t *testing.T) {
}
}()

files, err := ioutil.ReadDir(tmpdir)
files, err := os.ReadDir(tmpdir)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -153,7 +155,7 @@ func TestBuildImageSendXRegistryConfig(t *testing.T) {
}

func unpackBodyTarball(req io.Reader) (tmpdir string, err error) {
tmpdir, err = ioutil.TempDir("", "go-dockerclient-test")
tmpdir, err = os.MkdirTemp("", "go-dockerclient-test")
if err != nil {
return
}
Expand Down
17 changes: 8 additions & 9 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httputil"
Expand Down Expand Up @@ -240,19 +239,19 @@ func NewVersionedTLSClient(endpoint string, cert, key, ca, apiVersionString stri
var keyPEMBlock []byte
var caPEMCert []byte
if _, err := os.Stat(cert); !os.IsNotExist(err) {
certPEMBlock, err = ioutil.ReadFile(cert)
certPEMBlock, err = os.ReadFile(cert)
if err != nil {
return nil, err
}
}
if _, err := os.Stat(key); !os.IsNotExist(err) {
keyPEMBlock, err = ioutil.ReadFile(key)
keyPEMBlock, err = os.ReadFile(key)
if err != nil {
return nil, err
}
}
if _, err := os.Stat(ca); !os.IsNotExist(err) {
caPEMCert, err = ioutil.ReadFile(ca)
caPEMCert, err = os.ReadFile(ca)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -565,10 +564,10 @@ func (c *Client) streamURL(method, url string, streamOptions streamOptions) erro
protocol := c.endpointURL.Scheme
address := c.endpointURL.Path
if streamOptions.stdout == nil {
streamOptions.stdout = ioutil.Discard
streamOptions.stdout = io.Discard
}
if streamOptions.stderr == nil {
streamOptions.stderr = ioutil.Discard
streamOptions.stderr = io.Discard
}

if protocol == unixProtocol || protocol == namedPipeProtocol {
Expand Down Expand Up @@ -798,10 +797,10 @@ func (c *Client) hijack(method, path string, hijackOptions hijackOptions) (Close
// will "hang" until the container terminates, even though you're not reading
// stdout/stderr
if hijackOptions.stdout == nil {
hijackOptions.stdout = ioutil.Discard
hijackOptions.stdout = io.Discard
}
if hijackOptions.stderr == nil {
hijackOptions.stderr = ioutil.Discard
hijackOptions.stderr = io.Discard
}

go func() {
Expand Down Expand Up @@ -1024,7 +1023,7 @@ func newError(resp *http.Response) *Error {
Message string `json:"message"`
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
data, err := io.ReadAll(resp.Body)
if err != nil {
return &Error{Status: resp.StatusCode, Message: fmt.Sprintf("cannot read body, err: %v", err)}
}
Expand Down
6 changes: 3 additions & 3 deletions client_stress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ package docker

import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"reflect"
"sort"
"sync"
Expand Down Expand Up @@ -66,11 +66,11 @@ func TestClientDoConcurrentStress(t *testing.T) {
}
defer tt.srv.Close()
if tt.withTLSClient {
certPEMBlock, certErr := ioutil.ReadFile("testing/data/cert.pem")
certPEMBlock, certErr := os.ReadFile("testing/data/cert.pem")
if certErr != nil {
t.Fatal(certErr)
}
keyPEMBlock, certErr := ioutil.ReadFile("testing/data/key.pem")
keyPEMBlock, certErr := os.ReadFile("testing/data/key.pem")
if certErr != nil {
t.Fatal(certErr)
}
Expand Down
5 changes: 2 additions & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -346,7 +345,7 @@ func TestGetFakeNativeURL(t *testing.T) {

func TestError(t *testing.T) {
t.Parallel()
fakeBody := ioutil.NopCloser(bytes.NewBufferString("bad parameter"))
fakeBody := io.NopCloser(bytes.NewBufferString("bad parameter"))
resp := &http.Response{
StatusCode: 400,
Body: fakeBody,
Expand Down Expand Up @@ -893,7 +892,7 @@ func (rt *FakeRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
rt.requests = append(rt.requests, r)
res := &http.Response{
StatusCode: rt.status,
Body: ioutil.NopCloser(body),
Body: io.NopCloser(body),
Header: make(http.Header),
}
for k, v := range rt.header {
Expand Down
6 changes: 3 additions & 3 deletions client_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
package docker

import (
"io/ioutil"
"io"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -46,7 +46,7 @@ func TestNewTSLAPIClientUnixEndpoint(t *testing.T) {
if err != nil {
t.Fatal(err)
}
data, err := ioutil.ReadAll(rsp.Body)
data, err := io.ReadAll(rsp.Body)
if err != nil {
t.Fatal(err)
}
Expand All @@ -56,7 +56,7 @@ func TestNewTSLAPIClientUnixEndpoint(t *testing.T) {
}

func newNativeServer(handler http.Handler) (*httptest.Server, func(), error) {
tmpdir, err := ioutil.TempDir("", "socket")
tmpdir, err := os.MkdirTemp("", "socket")
if err != nil {
return nil, nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions container_commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package docker
import (
"bytes"
"encoding/json"
"io/ioutil"
"io"
"net/http"
"reflect"
"testing"
Expand Down Expand Up @@ -66,7 +66,7 @@ func TestCommitContainerParams(t *testing.T) {
t.Errorf("Wrong HTTP method. Want POST. Got %s.", meth)
}
if test.body != nil {
if requestBody, err := ioutil.ReadAll(fakeRT.requests[0].Body); err == nil {
if requestBody, err := io.ReadAll(fakeRT.requests[0].Body); err == nil {
if !bytes.Equal(requestBody, test.body) {
t.Errorf("Expected body %#v, got %#v", test.body, requestBody)
}
Expand Down
4 changes: 2 additions & 2 deletions container_start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package docker
import (
"context"
"errors"
"io/ioutil"
"io"
"net/http"
"net/url"
"reflect"
Expand Down Expand Up @@ -57,7 +57,7 @@ func TestStartContainerHostConfigAPI124(t *testing.T) {
t.Errorf("StartContainer(%q): Unepected %q Content-Type in request.", id, contentType)
}
if req.Body != nil {
data, _ := ioutil.ReadAll(req.Body)
data, _ := io.ReadAll(req.Body)
t.Errorf("StartContainer(%q): Unexpected data sent: %s", id, data)
}
}
Expand Down
3 changes: 1 addition & 2 deletions container_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ package docker
import (
"bufio"
"bytes"
"io/ioutil"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -54,7 +53,7 @@ func TestExportContainerViaUnixSocket(t *testing.T) {

func TestStatsTimeoutUnixSocket(t *testing.T) {
t.Parallel()
tmpdir, err := ioutil.TempDir("", "socket")
tmpdir, err := os.MkdirTemp("", "socket")
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"bufio"
"crypto/tls"
"crypto/x509"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
Expand All @@ -33,7 +33,7 @@ func TestTLSEventListeners(t *testing.T) {
t.Fatalf("Error loading server key pair: %s", err)
}

caCert, err := ioutil.ReadFile("testing/data/ca.pem")
caCert, err := os.ReadFile("testing/data/ca.pem")
if err != nil {
t.Fatalf("Error loading ca certificate: %s", err)
}
Expand Down
10 changes: 5 additions & 5 deletions image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"encoding/base64"
"encoding/json"
"errors"
"io/ioutil"
"io"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -682,7 +682,7 @@ func TestImportImageFromInput(t *testing.T) {
if !reflect.DeepEqual(got, expected) {
t.Errorf("ImportImage: wrong query string. Want %#v. Got %#v.", expected, got)
}
body, err := ioutil.ReadAll(req.Body)
body, err := io.ReadAll(req.Body)
if err != nil {
t.Errorf("ImportImage: caugth error while reading body %#v", err.Error())
}
Expand Down Expand Up @@ -712,7 +712,7 @@ func TestImportImageDoesNotPassInputIfSourceIsNotDash(t *testing.T) {
if !reflect.DeepEqual(got, expected) {
t.Errorf("ImportImage: wrong query string. Want %#v. Got %#v.", expected, got)
}
body, err := ioutil.ReadAll(req.Body)
body, err := io.ReadAll(req.Body)
if err != nil {
t.Errorf("ImportImage: caugth error while reading body %#v", err.Error())
}
Expand Down Expand Up @@ -740,11 +740,11 @@ func TestImportImageShouldPassTarContentToBodyWhenSourceIsFilePath(t *testing.T)
t.Fatal(err)
}
req := fakeRT.requests[0]
tarContent, err := ioutil.ReadAll(tar)
tarContent, err := io.ReadAll(tar)
if err != nil {
t.Fatal(err)
}
body, err := ioutil.ReadAll(req.Body)
body, err := io.ReadAll(req.Body)
if err != nil {
t.Fatal(err)
}
Expand Down
Loading

0 comments on commit fa277a9

Please sign in to comment.