refactor: move from io/ioutil to io and os package

The io/ioutil package has been deprecated as of Go 1.16, see
https://golang.org/doc/go1.16#ioutil. This commit replaces the existing
io/ioutil functions with their new definitions in io and os packages.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
Eng Zer Jun
2021-09-10 20:28:11 +08:00
parent c16be1a5e2
commit 50da673592
126 changed files with 291 additions and 399 deletions

View File

@@ -20,7 +20,6 @@ package config
import (
"context"
"crypto/tls"
"io/ioutil"
"net"
"net/http"
"net/url"
@@ -170,7 +169,7 @@ func ConfigureHosts(ctx context.Context, options HostOptions) docker.RegistryHos
tlsConfig.RootCAs = rootPool
}
for _, f := range host.caCerts {
data, err := ioutil.ReadFile(f)
data, err := os.ReadFile(f)
if err != nil {
return nil, errors.Wrapf(err, "unable to read CA cert %q", f)
}
@@ -182,13 +181,13 @@ func ConfigureHosts(ctx context.Context, options HostOptions) docker.RegistryHos
if host.clientPairs != nil {
for _, pair := range host.clientPairs {
certPEMBlock, err := ioutil.ReadFile(pair[0])
certPEMBlock, err := os.ReadFile(pair[0])
if err != nil {
return nil, errors.Wrapf(err, "unable to read CERT file %q", pair[0])
}
var keyPEMBlock []byte
if pair[1] != "" {
keyPEMBlock, err = ioutil.ReadFile(pair[1])
keyPEMBlock, err = os.ReadFile(pair[1])
if err != nil {
return nil, errors.Wrapf(err, "unable to read CERT file %q", pair[1])
}
@@ -251,7 +250,7 @@ func hostDirectory(host string) string {
}
func loadHostDir(ctx context.Context, hostsDir string) ([]hostConfig, error) {
b, err := ioutil.ReadFile(filepath.Join(hostsDir, "hosts.toml"))
b, err := os.ReadFile(filepath.Join(hostsDir, "hosts.toml"))
if err != nil && !os.IsNotExist(err) {
return nil, err
}
@@ -536,7 +535,7 @@ func makeAbsPath(p string, base string) string {
// the ".cert", which may contain the private key. If the ".cert" file
// does not contain the private key, the caller should detect and error.
func loadCertFiles(ctx context.Context, certsDir string) ([]hostConfig, error) {
fs, err := ioutil.ReadDir(certsDir)
fs, err := os.ReadDir(certsDir)
if err != nil && !os.IsNotExist(err) {
return nil, err
}

View File

@@ -20,7 +20,6 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
@@ -209,7 +208,7 @@ ca = "/etc/path/default"
}
func TestLoadCertFiles(t *testing.T) {
dir, err := ioutil.TempDir("", t.Name())
dir, err := os.MkdirTemp("", t.Name())
if err != nil {
t.Fatal(err)
}
@@ -257,16 +256,16 @@ func TestLoadCertFiles(t *testing.T) {
defer os.RemoveAll(hostDir)
for _, f := range tc.input.caCerts {
if err := ioutil.WriteFile(f, testKey, 0600); err != nil {
if err := os.WriteFile(f, testKey, 0600); err != nil {
t.Fatal(err)
}
}
for _, pair := range tc.input.clientPairs {
if err := ioutil.WriteFile(pair[0], testKey, 0600); err != nil {
if err := os.WriteFile(pair[0], testKey, 0600); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(pair[1], testKey, 0600); err != nil {
if err := os.WriteFile(pair[1], testKey, 0600); err != nil {
t.Fatal(err)
}
}