Skip to content

Commit

Permalink
chore: remove refs to deprecated io/ioutil (#577)
Browse files Browse the repository at this point in the history
Signed-off-by: guoguangwu <guoguangwu@magic-shield.com>
  • Loading branch information
testwill committed Sep 14, 2023
1 parent d92bee8 commit 032dd4f
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 47 deletions.
4 changes: 2 additions & 2 deletions cmd/imgpkg/imgpkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package main

import (
"io/ioutil"
"io"
"log"
"os"

Expand All @@ -15,7 +15,7 @@ import (
)

func main() {
log.SetOutput(ioutil.Discard)
log.SetOutput(io.Discard)

// TODO logs
// TODO log flags used
Expand Down
3 changes: 1 addition & 2 deletions pkg/imgpkg/bundle/bundle_images_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"archive/tar"
"fmt"
"io"
"io/ioutil"
"path/filepath"
"sync"

Expand Down Expand Up @@ -211,7 +210,7 @@ func (o *SingleLayerReader) Read(img regv1.Image) (lockconfig.ImagesLock, error)
}
}

bs, err := ioutil.ReadAll(tarReader)
bs, err := io.ReadAll(tarReader)
if err != nil {
return conf, fmt.Errorf("Reading images.yml from layer: %s", err)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/imgpkg/bundle/image_locations_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package bundle

import (
"fmt"
"io/ioutil"
"os"
"sort"

"sigs.k8s.io/yaml"
Expand All @@ -29,7 +29,7 @@ type ImageLocation struct {
}

func NewLocationConfigFromPath(path string) (ImageLocationsConfig, error) {
bs, err := ioutil.ReadFile(path)
bs, err := os.ReadFile(path)
if err != nil {
return ImageLocationsConfig{}, fmt.Errorf("Reading path %s: %s", path, err)
}
Expand Down Expand Up @@ -79,7 +79,7 @@ func (c ImageLocationsConfig) WriteToPath(path string) error {
if err != nil {
return err
}
err = ioutil.WriteFile(path, bs, 0600)
err = os.WriteFile(path, bs, 0600)
if err != nil {
return fmt.Errorf("Writing image locations config: %s", err)
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/imgpkg/bundle/locations_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"archive/tar"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -221,7 +220,7 @@ func (o *locationsSingleLayerReader) Read(img regv1.Image) (ImageLocationsConfig
}
}

bs, err := ioutil.ReadAll(tarReader)
bs, err := io.ReadAll(tarReader)
if err != nil {
return conf, fmt.Errorf("Reading image-locations.yml from layer: %s", err)
}
Expand Down
9 changes: 4 additions & 5 deletions pkg/imgpkg/cmd/copy_repo_src_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -448,10 +447,10 @@ images:
annotations:
my-annotation: image-index
`, imageIndexRefDigest)
lockFile, err := ioutil.TempFile(assets.CreateTempFolder("images-lock-dir"), "images.lock.yml")
lockFile, err := os.CreateTemp(assets.CreateTempFolder("images-lock-dir"), "images.lock.yml")

require.NoError(t, err)
err = ioutil.WriteFile(lockFile.Name(), []byte(imageLockYAML), 0600)
err = os.WriteFile(lockFile.Name(), []byte(imageLockYAML), 0600)
require.NoError(t, err)

subject := subject
Expand Down Expand Up @@ -1076,10 +1075,10 @@ images:
annotations:
my-annotation: second-image
`, image1.RefDigest, image2RefDigest)
lockFile, err := ioutil.TempFile(assets.CreateTempFolder("images-lock-dir"), "images.lock.yml")
lockFile, err := os.CreateTemp(assets.CreateTempFolder("images-lock-dir"), "images.lock.yml")

require.NoError(t, err)
err = ioutil.WriteFile(lockFile.Name(), []byte(imageLockYAML), 0600)
err = os.WriteFile(lockFile.Name(), []byte(imageLockYAML), 0600)
require.NoError(t, err)

subject := subject
Expand Down
5 changes: 2 additions & 3 deletions pkg/imgpkg/cmd/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package cmd

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -181,7 +180,7 @@ func TestDuplicateFilepathError(t *testing.T) {
}

someFile := filepath.Join(fooDir, "some-file.yml")
err = ioutil.WriteFile(someFile, []byte("foo: bar"), 0600)
err = os.WriteFile(someFile, []byte("foo: bar"), 0600)
if err != nil {
t.Fatalf("Failed to setup test: %s", err)
}
Expand Down Expand Up @@ -342,7 +341,7 @@ func createBundleDir(loc, imagesYaml string) error {
if imagesYaml == "" {
imagesYaml = emptyImagesYaml
}
return ioutil.WriteFile(filepath.Join(bundleDir, "images.yml"), []byte(imagesYaml), 0600)
return os.WriteFile(filepath.Join(bundleDir, "images.yml"), []byte(imagesYaml), 0600)
}

func createEmptyBundleDir(loc string) error {
Expand Down
3 changes: 1 addition & 2 deletions pkg/imgpkg/image/tar_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"archive/tar"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand All @@ -29,7 +28,7 @@ func NewTarImage(files []string, excludePaths []string, logger Logger, keepPermi

// AsFileImage Creates an OCI Image representation of the provided folders
func (i *TarImage) AsFileImage(labels map[string]string) (*FileImage, error) {
tmpFile, err := ioutil.TempFile("", "imgpkg-tar-image")
tmpFile, err := os.CreateTemp("", "imgpkg-tar-image")
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/imgpkg/imagetar/tar_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package imagetar
import (
"fmt"
"io"
"io/ioutil"

"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
Expand Down Expand Up @@ -138,7 +137,7 @@ func (r TarReader) getIdsFromManifest(file tarFile) (*imagedesc.ImageRefDescript
}
defer manifestFile.Close()

manifestBytes, err := ioutil.ReadAll(manifestFile)
manifestBytes, err := io.ReadAll(manifestFile)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/imgpkg/imageutils/and/and_closer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package and

import (
"bytes"
"io/ioutil"
"io"
"testing"
)

Expand All @@ -38,7 +38,7 @@ func TestRead(t *testing.T) {
},
}

data, err := ioutil.ReadAll(rac)
data, err := io.ReadAll(rac)
if err != nil {
t.Errorf("ReadAll(rac) = %v", err)
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/imgpkg/imageutils/gzip/zip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ package gzip
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"strings"
"testing"
)

func TestReader(t *testing.T) {
want := "This is the input string."
buf := bytes.NewBufferString(want)
zipped := ReadCloser(ioutil.NopCloser(buf))
zipped := ReadCloser(io.NopCloser(buf))
unzipped, err := UnzipReadCloser(zipped)
if err != nil {
t.Error("UnzipReadCloser() =", err)
}

b, err := ioutil.ReadAll(unzipped)
b, err := io.ReadAll(unzipped)
if err != nil {
t.Error("ReadAll() =", err)
}
Expand Down Expand Up @@ -86,17 +86,17 @@ func TestReadErrors(t *testing.T) {
t.Error("Is: expected errRead, got", err)
}

frc := ioutil.NopCloser(fr)
frc := io.NopCloser(fr)
if _, err := UnzipReadCloser(frc); err != errRead {
t.Error("UnzipReadCloser: expected errRead, got", err)
}

zr := ReadCloser(ioutil.NopCloser(fr))
zr := ReadCloser(io.NopCloser(fr))
if _, err := zr.Read(nil); err != errRead {
t.Error("ReadCloser: expected errRead, got", err)
}

zr = ReadCloserLevel(ioutil.NopCloser(strings.NewReader("zip me")), -10)
zr = ReadCloserLevel(io.NopCloser(strings.NewReader("zip me")), -10)
if _, err := zr.Read(nil); err == nil {
t.Error("Expected invalid level error, got:", err)
}
Expand Down
20 changes: 10 additions & 10 deletions pkg/imgpkg/imageutils/verify/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"io"
"strings"
"testing"

Expand All @@ -43,11 +43,11 @@ func TestVerificationFailure(t *testing.T) {
want := "This is the input string."
buf := bytes.NewBufferString(want)

verified, err := ReadCloser(ioutil.NopCloser(buf), int64(len(want)), mustHash("not the same", t))
verified, err := ReadCloser(io.NopCloser(buf), int64(len(want)), mustHash("not the same", t))
if err != nil {
t.Fatal("ReadCloser() =", err)
}
if b, err := ioutil.ReadAll(verified); err == nil {
if b, err := io.ReadAll(verified); err == nil {
t.Errorf("ReadAll() = %q; want verification error", string(b))
}
}
Expand All @@ -56,11 +56,11 @@ func TestVerification(t *testing.T) {
want := "This is the input string."
buf := bytes.NewBufferString(want)

verified, err := ReadCloser(ioutil.NopCloser(buf), int64(len(want)), mustHash(want, t))
verified, err := ReadCloser(io.NopCloser(buf), int64(len(want)), mustHash(want, t))
if err != nil {
t.Fatal("ReadCloser() =", err)
}
if _, err := ioutil.ReadAll(verified); err != nil {
if _, err := io.ReadAll(verified); err != nil {
t.Error("ReadAll() =", err)
}
}
Expand All @@ -69,11 +69,11 @@ func TestVerificationSizeUnknown(t *testing.T) {
want := "This is the input string."
buf := bytes.NewBufferString(want)

verified, err := ReadCloser(ioutil.NopCloser(buf), SizeUnknown, mustHash(want, t))
verified, err := ReadCloser(io.NopCloser(buf), SizeUnknown, mustHash(want, t))
if err != nil {
t.Fatal("ReadCloser() =", err)
}
if _, err := ioutil.ReadAll(verified); err != nil {
if _, err := io.ReadAll(verified); err != nil {
t.Error("ReadAll() =", err)
}
}
Expand All @@ -83,7 +83,7 @@ func TestBadHash(t *testing.T) {
Algorithm: "fake256",
Hex: "whatever",
}
_, err := ReadCloser(ioutil.NopCloser(strings.NewReader("hi")), 0, h)
_, err := ReadCloser(io.NopCloser(strings.NewReader("hi")), 0, h)
if err == nil {
t.Errorf("ReadCloser() = %v, wanted err", err)
}
Expand All @@ -96,11 +96,11 @@ func TestBadSize(t *testing.T) {
for _, size := range []int64{3, 100} {
t.Run(fmt.Sprintf("expecting size %d", size), func(t *testing.T) {
buf := bytes.NewBufferString(want)
rc, err := ReadCloser(ioutil.NopCloser(buf), size, mustHash(want, t))
rc, err := ReadCloser(io.NopCloser(buf), size, mustHash(want, t))
if err != nil {
t.Fatal("ReadCloser() =", err)
}
if b, err := ioutil.ReadAll(rc); err == nil {
if b, err := io.ReadAll(rc); err == nil {
t.Errorf("ReadAll() = %q; want verification error", string(b))
}
})
Expand Down
6 changes: 3 additions & 3 deletions pkg/imgpkg/lockconfig/bundle_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package lockconfig

import (
"fmt"
"io/ioutil"
"os"

regname "github.com/google/go-containerregistry/pkg/name"
"sigs.k8s.io/yaml"
Expand All @@ -27,7 +27,7 @@ type BundleRef struct {
}

func NewBundleLockFromPath(path string) (BundleLock, error) {
bs, err := ioutil.ReadFile(path)
bs, err := os.ReadFile(path)
if err != nil {
return BundleLock{}, fmt.Errorf("Reading path %s: %s", path, err)
}
Expand Down Expand Up @@ -84,7 +84,7 @@ func (b BundleLock) WriteToPath(path string) error {
return err
}

err = ioutil.WriteFile(path, bs, 0600)
err = os.WriteFile(path, bs, 0600)
if err != nil {
return fmt.Errorf("Writing bundle config: %s", err)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/imgpkg/lockconfig/images_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package lockconfig

import (
"fmt"
"io/ioutil"
"os"

regname "github.com/google/go-containerregistry/pkg/name"
"sigs.k8s.io/yaml"
Expand Down Expand Up @@ -37,7 +37,7 @@ func NewEmptyImagesLock() ImagesLock {
}

func NewImagesLockFromPath(path string) (ImagesLock, error) {
bs, err := ioutil.ReadFile(path)
bs, err := os.ReadFile(path)
if err != nil {
return ImagesLock{}, fmt.Errorf("Reading path %s: %s", path, err)
}
Expand Down Expand Up @@ -125,7 +125,7 @@ func (i ImagesLock) WriteToPath(path string) error {
return err
}

err = ioutil.WriteFile(path, bs, 0600)
err = os.WriteFile(path, bs, 0600)
if err != nil {
return fmt.Errorf("Writing images config: %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/imgpkg/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"sync"
"time"
Expand Down Expand Up @@ -536,7 +536,7 @@ func newHTTPTransport(opts Opts) (*http.Transport, error) {

if len(opts.CACertPaths) > 0 {
for _, path := range opts.CACertPaths {
if certs, err := ioutil.ReadFile(path); err != nil {
if certs, err := os.ReadFile(path); err != nil {
return nil, fmt.Errorf("Reading CA certificates from '%s': %s", path, err)
} else if ok := pool.AppendCertsFromPEM(certs); !ok {
return nil, fmt.Errorf("Adding CA certificates from '%s': failed", path)
Expand Down

0 comments on commit 032dd4f

Please sign in to comment.