Remove uses of deprecated io/ioutil

The package has been deprecated since Go 1.16: https://go.dev/doc/go1.16#ioutil

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-03-10 01:46:06 +01:00
parent c8f7c1e93f
commit fc7ba75fd7
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
13 changed files with 35 additions and 42 deletions

View File

@ -4,7 +4,7 @@ import (
"context" "context"
"encoding/csv" "encoding/csv"
"fmt" "fmt"
"io/ioutil" "io"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -65,12 +65,12 @@ func ReadLocalFiles(names []string) ([]File, error) {
var dt []byte var dt []byte
var err error var err error
if n == "-" { if n == "-" {
dt, err = ioutil.ReadAll(os.Stdin) dt, err = io.ReadAll(os.Stdin)
if err != nil { if err != nil {
return nil, err return nil, err
} }
} else { } else {
dt, err = ioutil.ReadFile(n) dt, err = os.ReadFile(n)
if err != nil { if err != nil {
if isDefault && errors.Is(err, os.ErrNotExist) { if isDefault && errors.Is(err, os.ErrNotExist) {
continue continue

View File

@ -8,7 +8,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
@ -771,7 +770,7 @@ func Build(ctx context.Context, drivers []DriverInfo, opt map[string]Options, do
dgst = v dgst = v
} }
if opt.ImageIDFile != "" { if opt.ImageIDFile != "" {
return ioutil.WriteFile(opt.ImageIDFile, []byte(dgst), 0644) return os.WriteFile(opt.ImageIDFile, []byte(dgst), 0644)
} }
return nil return nil
} }
@ -820,7 +819,7 @@ func Build(ctx context.Context, drivers []DriverInfo, opt map[string]Options, do
return err return err
} }
if opt.ImageIDFile != "" { if opt.ImageIDFile != "" {
if err := ioutil.WriteFile(opt.ImageIDFile, []byte(desc.Digest), 0644); err != nil { if err := os.WriteFile(opt.ImageIDFile, []byte(desc.Digest), 0644); err != nil {
return err return err
} }
} }
@ -1086,7 +1085,7 @@ func remoteDigestWithMoby(ctx context.Context, d driver.Driver, name string) (st
} }
func createTempDockerfile(r io.Reader) (string, error) { func createTempDockerfile(r io.Reader) (string, error) {
dir, err := ioutil.TempDir("", "dockerfile") dir, err := os.MkdirTemp("", "dockerfile")
if err != nil { if err != nil {
return "", err return "", err
} }
@ -1145,7 +1144,7 @@ func LoadInputs(ctx context.Context, d driver.Driver, inp Inputs, pw progress.Wr
} }
// stdin is dockerfile // stdin is dockerfile
dockerfileReader = buf dockerfileReader = buf
inp.ContextPath, _ = ioutil.TempDir("", "empty-dir") inp.ContextPath, _ = os.MkdirTemp("", "empty-dir")
toRemove = append(toRemove, inp.ContextPath) toRemove = append(toRemove, inp.ContextPath)
target.LocalDirs["context"] = inp.ContextPath target.LocalDirs["context"] = inp.ContextPath
} }
@ -1464,13 +1463,13 @@ func tryNodeIdentifier(configDir string) (out string) {
if _, err := rand.Read(b); err != nil { if _, err := rand.Read(b); err != nil {
return out return out
} }
if err := ioutil.WriteFile(sessionFile, []byte(hex.EncodeToString(b)), 0600); err != nil { if err := os.WriteFile(sessionFile, []byte(hex.EncodeToString(b)), 0600); err != nil {
return out return out
} }
} }
} }
dt, err := ioutil.ReadFile(sessionFile) dt, err := os.ReadFile(sessionFile)
if err == nil { if err == nil {
return string(dt) return string(dt)
} }

View File

@ -2,7 +2,7 @@ package build
import ( import (
"context" "context"
"io/ioutil" "os"
"path/filepath" "path/filepath"
"github.com/docker/buildx/driver" "github.com/docker/buildx/driver"
@ -53,11 +53,11 @@ func createTempDockerfileFromURL(ctx context.Context, d driver.Driver, url strin
if err != nil { if err != nil {
return nil, err return nil, err
} }
dir, err := ioutil.TempDir("", "buildx") dir, err := os.MkdirTemp("", "buildx")
if err != nil { if err != nil {
return nil, err return nil, err
} }
if err := ioutil.WriteFile(filepath.Join(dir, "Dockerfile"), dt, 0600); err != nil { if err := os.WriteFile(filepath.Join(dir, "Dockerfile"), dt, 0600); err != nil {
return nil, err return nil, err
} }
out = dir out = dir

View File

@ -3,7 +3,7 @@ package commands
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "os"
"strings" "strings"
"github.com/docker/buildx/store" "github.com/docker/buildx/store"
@ -38,7 +38,7 @@ func runCreate(dockerCli command.Cli, in createOptions, args []string) error {
fileArgs := make([]string, len(in.files)) fileArgs := make([]string, len(in.files))
for i, f := range in.files { for i, f := range in.files {
dt, err := ioutil.ReadFile(f) dt, err := os.ReadFile(f)
if err != nil { if err != nil {
return err return err
} }

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"context" "context"
"io" "io"
"io/ioutil"
"net" "net"
"os" "os"
"path" "path"
@ -90,7 +89,7 @@ func (d *Driver) create(ctx context.Context, l progress.SubLogger) error {
if err != nil { if err != nil {
return err return err
} }
_, err = io.Copy(ioutil.Discard, rc) _, err = io.Copy(io.Discard, rc)
return err return err
}); err != nil { }); err != nil {
// image pulling failed, check if it exists in local image store. // image pulling failed, check if it exists in local image store.

View File

@ -1,7 +1,6 @@
package context package context
import ( import (
"io/ioutil"
"os" "os"
"testing" "testing"
@ -42,7 +41,7 @@ var testStoreCfg = store.NewConfig(
) )
func TestSaveLoadContexts(t *testing.T) { func TestSaveLoadContexts(t *testing.T) {
storeDir, err := ioutil.TempDir("", "test-load-save-k8-context") storeDir, err := os.MkdirTemp("", "test-load-save-k8-context")
require.NoError(t, err) require.NoError(t, err)
defer os.RemoveAll(storeDir) defer os.RemoveAll(storeDir)
store := store.New(storeDir, testStoreCfg) store := store.New(storeDir, testStoreCfg)
@ -50,7 +49,7 @@ func TestSaveLoadContexts(t *testing.T) {
require.NoError(t, save(store, testEndpoint("https://test", "test", nil, nil, nil, true), "raw-notls-skip")) require.NoError(t, save(store, testEndpoint("https://test", "test", nil, nil, nil, true), "raw-notls-skip"))
require.NoError(t, save(store, testEndpoint("https://test", "test", []byte("ca"), []byte("cert"), []byte("key"), true), "raw-tls")) require.NoError(t, save(store, testEndpoint("https://test", "test", []byte("ca"), []byte("cert"), []byte("key"), true), "raw-tls"))
kcFile, err := ioutil.TempFile(os.TempDir(), "test-load-save-k8-context") kcFile, err := os.CreateTemp(os.TempDir(), "test-load-save-k8-context")
require.NoError(t, err) require.NoError(t, err)
defer os.Remove(kcFile.Name()) defer os.Remove(kcFile.Name())
defer kcFile.Close() defer kcFile.Close()
@ -147,7 +146,7 @@ func save(s store.Writer, ep Endpoint, name string) error {
} }
func TestSaveLoadGKEConfig(t *testing.T) { func TestSaveLoadGKEConfig(t *testing.T) {
storeDir, err := ioutil.TempDir("", t.Name()) storeDir, err := os.MkdirTemp("", t.Name())
require.NoError(t, err) require.NoError(t, err)
defer os.RemoveAll(storeDir) defer os.RemoveAll(storeDir)
store := store.New(storeDir, testStoreCfg) store := store.New(storeDir, testStoreCfg)
@ -172,7 +171,7 @@ func TestSaveLoadGKEConfig(t *testing.T) {
} }
func TestSaveLoadEKSConfig(t *testing.T) { func TestSaveLoadEKSConfig(t *testing.T) {
storeDir, err := ioutil.TempDir("", t.Name()) storeDir, err := os.MkdirTemp("", t.Name())
require.NoError(t, err) require.NoError(t, err)
defer os.RemoveAll(storeDir) defer os.RemoveAll(storeDir)
store := store.New(storeDir, testStoreCfg) store := store.New(storeDir, testStoreCfg)
@ -197,7 +196,7 @@ func TestSaveLoadEKSConfig(t *testing.T) {
} }
func TestSaveLoadK3SConfig(t *testing.T) { func TestSaveLoadK3SConfig(t *testing.T) {
storeDir, err := ioutil.TempDir("", t.Name()) storeDir, err := os.MkdirTemp("", t.Name())
require.NoError(t, err) require.NoError(t, err)
defer os.RemoveAll(storeDir) defer os.RemoveAll(storeDir)
store := store.New(storeDir, testStoreCfg) store := store.New(storeDir, testStoreCfg)

View File

@ -1,7 +1,7 @@
package context package context
import ( import (
"io/ioutil" "os"
"github.com/docker/cli/cli/context" "github.com/docker/cli/cli/context"
"k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/tools/clientcmd"
@ -63,7 +63,7 @@ func FromKubeConfig(kubeconfig, kubeContext, namespaceOverride string) (Endpoint
func readFileOrDefault(path string, defaultValue []byte) ([]byte, error) { func readFileOrDefault(path string, defaultValue []byte) ([]byte, error) {
if path != "" { if path != "" {
return ioutil.ReadFile(path) return os.ReadFile(path)
} }
return defaultValue, nil return defaultValue, nil
} }

View File

@ -2,7 +2,7 @@ package driver
import ( import (
"context" "context"
"io/ioutil" "os"
"sort" "sort"
"strings" "strings"
"sync" "sync"
@ -40,7 +40,7 @@ func (k KubeClientConfigInCluster) ClientConfig() (*rest.Config, error) {
} }
func (k KubeClientConfigInCluster) Namespace() (string, bool, error) { func (k KubeClientConfigInCluster) Namespace() (string, bool, error) {
namespace, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace") namespace, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
if err != nil { if err != nil {
return "", false, err return "", false, err
} }

View File

@ -2,7 +2,6 @@ package store
import ( import (
"encoding/json" "encoding/json"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
@ -46,7 +45,7 @@ type Txn struct {
func (t *Txn) List() ([]*NodeGroup, error) { func (t *Txn) List() ([]*NodeGroup, error) {
pp := filepath.Join(t.s.root, "instances") pp := filepath.Join(t.s.root, "instances")
fis, err := ioutil.ReadDir(pp) fis, err := os.ReadDir(pp)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -75,7 +74,7 @@ func (t *Txn) NodeGroupByName(name string) (*NodeGroup, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
dt, err := ioutil.ReadFile(filepath.Join(t.s.root, "instances", name)) dt, err := os.ReadFile(filepath.Join(t.s.root, "instances", name))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -144,7 +143,7 @@ func (t *Txn) reset(key string) error {
} }
func (t *Txn) Current(key string) (*NodeGroup, error) { func (t *Txn) Current(key string) (*NodeGroup, error) {
dt, err := ioutil.ReadFile(filepath.Join(t.s.root, "current")) dt, err := os.ReadFile(filepath.Join(t.s.root, "current"))
if err != nil { if err != nil {
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
return nil, err return nil, err
@ -175,7 +174,7 @@ func (t *Txn) Current(key string) (*NodeGroup, error) {
h := toHash(key) h := toHash(key)
dt, err = ioutil.ReadFile(filepath.Join(t.s.root, "defaults", h)) dt, err = os.ReadFile(filepath.Join(t.s.root, "defaults", h))
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
t.reset(key) t.reset(key)

View File

@ -1,7 +1,6 @@
package store package store
import ( import (
"io/ioutil"
"os" "os"
"testing" "testing"
"time" "time"
@ -12,7 +11,7 @@ import (
func TestEmptyStartup(t *testing.T) { func TestEmptyStartup(t *testing.T) {
t.Parallel() t.Parallel()
tmpdir, err := ioutil.TempDir("", "buildx-store") tmpdir, err := os.MkdirTemp("", "buildx-store")
require.NoError(t, err) require.NoError(t, err)
defer os.RemoveAll(tmpdir) defer os.RemoveAll(tmpdir)
@ -30,7 +29,7 @@ func TestEmptyStartup(t *testing.T) {
func TestNodeLocking(t *testing.T) { func TestNodeLocking(t *testing.T) {
t.Parallel() t.Parallel()
tmpdir, err := ioutil.TempDir("", "buildx-store") tmpdir, err := os.MkdirTemp("", "buildx-store")
require.NoError(t, err) require.NoError(t, err)
defer os.RemoveAll(tmpdir) defer os.RemoveAll(tmpdir)
@ -65,7 +64,7 @@ func TestNodeLocking(t *testing.T) {
func TestNodeManagement(t *testing.T) { func TestNodeManagement(t *testing.T) {
t.Parallel() t.Parallel()
tmpdir, err := ioutil.TempDir("", "buildx-store") tmpdir, err := os.MkdirTemp("", "buildx-store")
require.NoError(t, err) require.NoError(t, err)
defer os.RemoveAll(tmpdir) defer os.RemoveAll(tmpdir)

View File

@ -1,7 +1,7 @@
package logutil package logutil
import ( import (
"io/ioutil" "io"
"strings" "strings"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -9,7 +9,7 @@ import (
func NewFilter(levels []logrus.Level, filters ...string) logrus.Hook { func NewFilter(levels []logrus.Level, filters ...string) logrus.Hook {
dl := logrus.New() dl := logrus.New()
dl.SetOutput(ioutil.Discard) dl.SetOutput(io.Discard)
return &logsFilter{ return &logsFilter{
levels: levels, levels: levels,
filters: filters, filters: filters,

View File

@ -2,7 +2,6 @@ package progress
import ( import (
"io" "io"
"io/ioutil"
"time" "time"
"github.com/moby/buildkit/client" "github.com/moby/buildkit/client"
@ -24,7 +23,7 @@ func FromReader(w Writer, name string, rc io.ReadCloser) {
Vertexes: []*client.Vertex{&vtx}, Vertexes: []*client.Vertex{&vtx},
}) })
_, err := io.Copy(ioutil.Discard, rc) _, err := io.Copy(io.Discard, rc)
tm2 := time.Now() tm2 := time.Now()
vtx2 := vtx vtx2 := vtx

View File

@ -3,7 +3,6 @@ package progress
import ( import (
"context" "context"
"io" "io"
"io/ioutil"
"os" "os"
"sync" "sync"
@ -88,7 +87,7 @@ func NewPrinter(ctx context.Context, w io.Writer, out console.File, mode string)
var c console.Console var c console.Console
switch mode { switch mode {
case PrinterModeQuiet: case PrinterModeQuiet:
w = ioutil.Discard w = io.Discard
case PrinterModeAuto, PrinterModeTty: case PrinterModeAuto, PrinterModeTty:
if cons, err := console.ConsoleFromFile(out); err == nil { if cons, err := console.ConsoleFromFile(out); err == nil {
c = cons c = cons