Skip to content

Commit

Permalink
Merge pull request #1521 from mtrmac/go1.16
Browse files Browse the repository at this point in the history
Update to benefit from Go 1.16
  • Loading branch information
vrothberg committed Apr 14, 2022
2 parents d20cf3a + f6e5415 commit d2d961d
Show file tree
Hide file tree
Showing 74 changed files with 282 additions and 326 deletions.
11 changes: 5 additions & 6 deletions copy/copy.go
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"reflect"
"strings"
Expand Down Expand Up @@ -199,7 +198,7 @@ func Image(ctx context.Context, policyContext *signature.PolicyContext, destRef,
return nil, err
}

reportWriter := ioutil.Discard
reportWriter := io.Discard

if options.ReportWriter != nil {
reportWriter = options.ReportWriter
Expand Down Expand Up @@ -232,7 +231,7 @@ func Image(ctx context.Context, policyContext *signature.PolicyContext, destRef,
// createProgressBar() will print a single line instead.
progressOutput := reportWriter
if !isTTY(reportWriter) {
progressOutput = ioutil.Discard
progressOutput = io.Discard
}

c := &copier{
Expand Down Expand Up @@ -1091,7 +1090,7 @@ func customPartialBlobDecorFunc(s decor.Statistics) string {
}

// createProgressBar creates a mpb.Bar in pool. Note that if the copier's reportWriter
// is ioutil.Discard, the progress bar's output will be discarded
// is io.Discard, the progress bar's output will be discarded
// NOTE: Every progress bar created within a progress pool must either successfully
// complete or be aborted, or pool.Wait() will hang. That is typically done
// using "defer bar.Abort(false)", which must happen BEFORE pool.Wait() is called.
Expand Down Expand Up @@ -1143,7 +1142,7 @@ func (c *copier) createProgressBar(pool *mpb.Progress, partial bool, info types.
),
)
}
if c.progressOutput == ioutil.Discard {
if c.progressOutput == io.Discard {
c.Printf("Copying %s %s\n", kind, info.Digest)
}
return bar
Expand Down Expand Up @@ -1669,7 +1668,7 @@ func (c *copier) copyBlobFromStream(ctx context.Context, srcStream io.Reader, sr
// sent there if we are not already at EOF.
if getOriginalLayerCopyWriter != nil {
logrus.Debugf("Consuming rest of the original blob to satisfy getOriginalLayerCopyWriter")
_, err := io.Copy(ioutil.Discard, originalLayerReader)
_, err := io.Copy(io.Discard, originalLayerReader)
if err != nil {
return types.BlobInfo{}, errors.Wrapf(err, "reading input blob %s", srcInfo.Digest)
}
Expand Down
6 changes: 3 additions & 3 deletions copy/sign_test.go
Expand Up @@ -2,7 +2,7 @@ package copy

import (
"context"
"io/ioutil"
"io"
"os"
"testing"

Expand Down Expand Up @@ -48,7 +48,7 @@ func TestCreateSignature(t *testing.T) {
defer dirDest.Close()
c := &copier{
dest: imagedestination.FromPublic(dirDest),
reportWriter: ioutil.Discard,
reportWriter: io.Discard,
}
_, err = c.createSignature(manifestBlob, testKeyFingerprint, "", nil)
assert.Error(t, err)
Expand All @@ -62,7 +62,7 @@ func TestCreateSignature(t *testing.T) {
defer dockerDest.Close()
c = &copier{
dest: imagedestination.FromPublic(dockerDest),
reportWriter: ioutil.Discard,
reportWriter: io.Discard,
}

// Signing with an unknown key fails
Expand Down
15 changes: 7 additions & 8 deletions directory/directory_dest.go
Expand Up @@ -3,7 +3,6 @@ package directory
import (
"context"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -62,7 +61,7 @@ func newImageDestination(sys *types.SystemContext, ref dirReference) (types.Imag
return nil, errors.Wrapf(err, "checking if path exists %q", d.ref.versionPath())
}
if versionExists {
contents, err := ioutil.ReadFile(d.ref.versionPath())
contents, err := os.ReadFile(d.ref.versionPath())
if err != nil {
return nil, err
}
Expand All @@ -86,7 +85,7 @@ func newImageDestination(sys *types.SystemContext, ref dirReference) (types.Imag
}
}
// create version file
err = ioutil.WriteFile(d.ref.versionPath(), []byte(version), 0644)
err = os.WriteFile(d.ref.versionPath(), []byte(version), 0644)
if err != nil {
return nil, errors.Wrapf(err, "creating version file %q", d.ref.versionPath())
}
Expand Down Expand Up @@ -149,7 +148,7 @@ func (d *dirImageDestination) HasThreadSafePutBlob() bool {
// to any other readers for download using the supplied digest.
// If stream.Read() at any time, ESPECIALLY at end of input, returns an error, PutBlob MUST 1) fail, and 2) delete any data stored so far.
func (d *dirImageDestination) PutBlob(ctx context.Context, stream io.Reader, inputInfo types.BlobInfo, cache types.BlobInfoCache, isConfig bool) (types.BlobInfo, error) {
blobFile, err := ioutil.TempFile(d.ref.path, "dir-put-blob")
blobFile, err := os.CreateTemp(d.ref.path, "dir-put-blob")
if err != nil {
return types.BlobInfo{}, err
}
Expand Down Expand Up @@ -232,15 +231,15 @@ func (d *dirImageDestination) TryReusingBlob(ctx context.Context, info types.Blo
// If the destination is in principle available, refuses this manifest type (e.g. it does not recognize the schema),
// but may accept a different manifest type, the returned error must be an ManifestTypeRejectedError.
func (d *dirImageDestination) PutManifest(ctx context.Context, manifest []byte, instanceDigest *digest.Digest) error {
return ioutil.WriteFile(d.ref.manifestPath(instanceDigest), manifest, 0644)
return os.WriteFile(d.ref.manifestPath(instanceDigest), manifest, 0644)
}

// PutSignatures writes a set of signatures to the destination.
// If instanceDigest is not nil, it contains a digest of the specific manifest instance to write or overwrite the signatures for
// (when the primary manifest is a manifest list); this should always be nil if the primary manifest is not a manifest list.
func (d *dirImageDestination) PutSignatures(ctx context.Context, signatures [][]byte, instanceDigest *digest.Digest) error {
for i, sig := range signatures {
if err := ioutil.WriteFile(d.ref.signaturePath(i, instanceDigest), sig, 0644); err != nil {
if err := os.WriteFile(d.ref.signaturePath(i, instanceDigest), sig, 0644); err != nil {
return err
}
}
Expand Down Expand Up @@ -272,7 +271,7 @@ func pathExists(path string) (bool, error) {

// returns true if directory is empty
func isDirEmpty(path string) (bool, error) {
files, err := ioutil.ReadDir(path)
files, err := os.ReadDir(path)
if err != nil {
return false, err
}
Expand All @@ -281,7 +280,7 @@ func isDirEmpty(path string) (bool, error) {

// deletes the contents of a directory
func removeDirContents(path string) error {
files, err := ioutil.ReadDir(path)
files, err := os.ReadDir(path)
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions directory/directory_src.go
Expand Up @@ -3,7 +3,6 @@ package directory
import (
"context"
"io"
"io/ioutil"
"os"

"github.com/containers/image/v5/manifest"
Expand Down Expand Up @@ -37,7 +36,7 @@ func (s *dirImageSource) Close() error {
// If instanceDigest is not nil, it contains a digest of the specific manifest instance to retrieve (when the primary manifest is a manifest list);
// this never happens if the primary manifest is not a manifest list (e.g. if the source never returns manifest lists).
func (s *dirImageSource) GetManifest(ctx context.Context, instanceDigest *digest.Digest) ([]byte, string, error) {
m, err := ioutil.ReadFile(s.ref.manifestPath(instanceDigest))
m, err := os.ReadFile(s.ref.manifestPath(instanceDigest))
if err != nil {
return nil, "", err
}
Expand Down Expand Up @@ -71,7 +70,7 @@ func (s *dirImageSource) GetBlob(ctx context.Context, info types.BlobInfo, cache
func (s *dirImageSource) GetSignatures(ctx context.Context, instanceDigest *digest.Digest) ([][]byte, error) {
signatures := [][]byte{}
for i := 0; ; i++ {
signature, err := ioutil.ReadFile(s.ref.signaturePath(i, instanceDigest))
signature, err := os.ReadFile(s.ref.signaturePath(i, instanceDigest))
if err != nil {
if os.IsNotExist(err) {
break
Expand Down
4 changes: 2 additions & 2 deletions directory/directory_test.go
Expand Up @@ -3,7 +3,7 @@ package directory
import (
"bytes"
"context"
"io/ioutil"
"io"
"os"
"testing"

Expand Down Expand Up @@ -92,7 +92,7 @@ func TestGetPutBlob(t *testing.T) {
rc, size, err := src.GetBlob(context.Background(), types.BlobInfo{Digest: digest, Size: int64(len(expectedBlob))}, cache)
assert.NoError(t, err)
defer rc.Close()
b, err := ioutil.ReadAll(rc)
b, err := io.ReadAll(rc)
assert.NoError(t, err)
assert.Equal(t, expectedBlob, b)
assert.Equal(t, int64(len(expectedBlob)), size)
Expand Down
3 changes: 1 addition & 2 deletions directory/directory_transport_test.go
Expand Up @@ -2,7 +2,6 @@ package directory

import (
"context"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -145,7 +144,7 @@ func TestReferenceNewImage(t *testing.T) {
dest, err := ref.NewImageDestination(context.Background(), nil)
require.NoError(t, err)
defer dest.Close()
mFixture, err := ioutil.ReadFile("../manifest/fixtures/v2s1.manifest.json")
mFixture, err := os.ReadFile("../manifest/fixtures/v2s1.manifest.json")
require.NoError(t, err)
err = dest.PutManifest(context.Background(), mFixture, nil)
assert.NoError(t, err)
Expand Down
3 changes: 1 addition & 2 deletions docker/archive/transport_test.go
Expand Up @@ -3,7 +3,6 @@ package archive
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -271,7 +270,7 @@ func TestReferenceDeleteImage(t *testing.T) {

for i, suffix := range []string{"", ":some-reference", ":@0"} {
testFile := filepath.Join(tmpDir, fmt.Sprintf("file%d.tar", i))
err := ioutil.WriteFile(testFile, []byte("nonempty"), 0644)
err := os.WriteFile(testFile, []byte("nonempty"), 0644)
require.NoError(t, err, suffix)

ref, err := ParseReference(testFile + suffix)
Expand Down
3 changes: 1 addition & 2 deletions docker/docker_client.go
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -654,7 +653,7 @@ func (c *dockerClient) getBearerTokenOAuth2(ctx context.Context, challenge chall
params.Add("refresh_token", c.auth.IdentityToken)
params.Add("client_id", "containers/image")

authReq.Body = ioutil.NopCloser(bytes.NewBufferString(params.Encode()))
authReq.Body = io.NopCloser(bytes.NewBufferString(params.Encode()))
authReq.Header.Add("User-Agent", c.userAgent)
authReq.Header.Add("Content-Type", "application/x-www-form-urlencoded")
logrus.Debugf("%s %s", authReq.Method, authReq.URL.Redacted())
Expand Down
3 changes: 1 addition & 2 deletions docker/docker_image_dest.go
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -592,7 +591,7 @@ func (d *dockerImageDestination) putOneSignature(url *url.URL, signature []byte)
if err != nil {
return err
}
err = ioutil.WriteFile(url.Path, signature, 0644)
err = os.WriteFile(url.Path, signature, 0644)
if err != nil {
return err
}
Expand Down
9 changes: 4 additions & 5 deletions docker/docker_image_src.go
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"mime"
"mime/multipart"
"net/http"
Expand Down Expand Up @@ -308,15 +307,15 @@ func splitHTTP200ResponseToPartial(streams chan io.ReadCloser, errs chan error,
break
}
toSkip := c.Offset - currentOffset
if _, err := io.Copy(ioutil.Discard, io.LimitReader(body, int64(toSkip))); err != nil {
if _, err := io.Copy(io.Discard, io.LimitReader(body, int64(toSkip))); err != nil {
errs <- err
break
}
currentOffset += toSkip
}
s := signalCloseReader{
closed: make(chan interface{}),
stream: ioutil.NopCloser(io.LimitReader(body, int64(c.Length))),
stream: io.NopCloser(io.LimitReader(body, int64(c.Length))),
consumeStream: true,
}
streams <- s
Expand Down Expand Up @@ -515,7 +514,7 @@ func (s *dockerImageSource) getOneSignature(ctx context.Context, url *url.URL) (
switch url.Scheme {
case "file":
logrus.Debugf("Reading %s", url.Path)
sig, err := ioutil.ReadFile(url.Path)
sig, err := os.ReadFile(url.Path)
if err != nil {
if os.IsNotExist(err) {
return nil, true, nil
Expand Down Expand Up @@ -765,7 +764,7 @@ func (s signalCloseReader) Read(p []byte) (int, error) {
func (s signalCloseReader) Close() error {
defer close(s.closed)
if s.consumeStream {
if _, err := io.Copy(ioutil.Discard, s.stream); err != nil {
if _, err := io.Copy(io.Discard, s.stream); err != nil {
s.stream.Close()
return err
}
Expand Down
13 changes: 6 additions & 7 deletions docker/docker_image_src_test.go
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -51,11 +50,11 @@ location = "with-mirror.example.com"
[[registry.mirror]]
location = "@REGISTRY@/with-mirror"
`, "@REGISTRY@", registry, -1)
registriesConf, err := ioutil.TempFile("", "docker-image-src")
registriesConf, err := os.CreateTemp("", "docker-image-src")
require.NoError(t, err)
defer registriesConf.Close()
defer os.Remove(registriesConf.Name())
err = ioutil.WriteFile(registriesConf.Name(), []byte(mirrorConfiguration), 0600)
err = os.WriteFile(registriesConf.Name(), []byte(mirrorConfiguration), 0600)
require.NoError(t, err)

for _, c := range []struct{ input, physical string }{
Expand Down Expand Up @@ -108,7 +107,7 @@ func readNextStream(streams chan io.ReadCloser, errs chan error) ([]byte, error)
return nil, nil
}
defer r.Close()
return ioutil.ReadAll(r)
return io.ReadAll(r)
case err := <-errs:
return nil, err
}
Expand All @@ -128,7 +127,7 @@ func verifyGetBlobAtOutput(t *testing.T, streams chan io.ReadCloser, errs chan e
}

func TestSplitHTTP200ResponseToPartial(t *testing.T) {
body := ioutil.NopCloser(bytes.NewReader([]byte("123456789")))
body := io.NopCloser(bytes.NewReader([]byte("123456789")))
defer body.Close()
streams := make(chan io.ReadCloser)
errs := make(chan error)
Expand All @@ -148,7 +147,7 @@ func TestSplitHTTP200ResponseToPartial(t *testing.T) {
}

func TestHandle206Response(t *testing.T) {
body := ioutil.NopCloser(bytes.NewReader([]byte("--AAA\r\n\r\n23\r\n--AAA\r\n\r\n5\r\n--AAA--")))
body := io.NopCloser(bytes.NewReader([]byte("--AAA\r\n\r\n23\r\n--AAA\r\n\r\n5\r\n--AAA--")))
defer body.Close()
streams := make(chan io.ReadCloser)
errs := make(chan error)
Expand All @@ -169,7 +168,7 @@ func TestHandle206Response(t *testing.T) {
}
verifyGetBlobAtOutput(t, streams, errs, expected)

body = ioutil.NopCloser(bytes.NewReader([]byte("HELLO")))
body = io.NopCloser(bytes.NewReader([]byte("HELLO")))
defer body.Close()
streams = make(chan io.ReadCloser)
errs = make(chan error)
Expand Down
3 changes: 1 addition & 2 deletions docker/internal/tarfile/reader.go
Expand Up @@ -4,7 +4,6 @@ import (
"archive/tar"
"encoding/json"
"io"
"io/ioutil"
"os"
"path"

Expand Down Expand Up @@ -53,7 +52,7 @@ func NewReaderFromFile(sys *types.SystemContext, path string) (*Reader, error) {
// The caller should call .Close() on the returned archive when done.
func NewReaderFromStream(sys *types.SystemContext, inputStream io.Reader) (*Reader, error) {
// Save inputStream to a temporary file
tarCopyFile, err := ioutil.TempFile(tmpdir.TemporaryDirectoryForBigFiles(sys), "docker-tar")
tarCopyFile, err := os.CreateTemp(tmpdir.TemporaryDirectoryForBigFiles(sys), "docker-tar")
if err != nil {
return nil, errors.Wrap(err, "creating temporary file")
}
Expand Down

0 comments on commit d2d961d

Please sign in to comment.