Skip to content

Commit

Permalink
Remove ioutil (#395)
Browse files Browse the repository at this point in the history
Signed-off-by: inosato <[email protected]>
  • Loading branch information
inosato authored Aug 3, 2022
1 parent 49b3603 commit 1482587
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 36 deletions.
4 changes: 2 additions & 2 deletions assets/embed_gzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package assets

import (
"embed"
"io/ioutil"
"io"
"strings"
"testing"
)
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestFS(t *testing.T) {
return
}

content, err := ioutil.ReadAll(f)
content, err := io.ReadAll(f)
if err != nil {
t.Fatal(err)
}
Expand Down
8 changes: 4 additions & 4 deletions config/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import (
"encoding/pem"
"fmt"
"io"
"io/ioutil"
"log"
"math/big"
"net"
"os"
"time"
)

Expand Down Expand Up @@ -170,7 +170,7 @@ func writeCertificateAndKey(path string, cert *x509.Certificate, key *rsa.Privat
return err
}

if err := ioutil.WriteFile(fmt.Sprintf("%s.crt", path), b.Bytes(), 0644); err != nil {
if err := os.WriteFile(fmt.Sprintf("%s.crt", path), b.Bytes(), 0644); err != nil {
return err
}

Expand All @@ -179,7 +179,7 @@ func writeCertificateAndKey(path string, cert *x509.Certificate, key *rsa.Privat
return err
}

if err := ioutil.WriteFile(fmt.Sprintf("%s.key", path), b.Bytes(), 0644); err != nil {
if err := os.WriteFile(fmt.Sprintf("%s.key", path), b.Bytes(), 0644); err != nil {
return err
}

Expand Down Expand Up @@ -239,7 +239,7 @@ func main() {
log.Fatal(err)
}

if err := ioutil.WriteFile("testdata/tls-ca-chain.pem", b.Bytes(), 0644); err != nil {
if err := os.WriteFile("testdata/tls-ca-chain.pem", b.Bytes(), 0644); err != nil {
log.Fatal(err)
}
}
10 changes: 5 additions & 5 deletions config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import (
"crypto/x509"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -571,7 +571,7 @@ func NewAuthorizationCredentialsFileRoundTripper(authType, authCredentialsFile s

func (rt *authorizationCredentialsFileRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
if len(req.Header.Get("Authorization")) == 0 {
b, err := ioutil.ReadFile(rt.authCredentialsFile)
b, err := os.ReadFile(rt.authCredentialsFile)
if err != nil {
return nil, fmt.Errorf("unable to read authorization credentials file %s: %s", rt.authCredentialsFile, err)
}
Expand Down Expand Up @@ -609,7 +609,7 @@ func (rt *basicAuthRoundTripper) RoundTrip(req *http.Request) (*http.Response, e
}
req = cloneRequest(req)
if rt.passwordFile != "" {
bs, err := ioutil.ReadFile(rt.passwordFile)
bs, err := os.ReadFile(rt.passwordFile)
if err != nil {
return nil, fmt.Errorf("unable to read basic auth password file %s: %s", rt.passwordFile, err)
}
Expand Down Expand Up @@ -651,7 +651,7 @@ func (rt *oauth2RoundTripper) RoundTrip(req *http.Request) (*http.Response, erro
)

if rt.config.ClientSecretFile != "" {
data, err := ioutil.ReadFile(rt.config.ClientSecretFile)
data, err := os.ReadFile(rt.config.ClientSecretFile)
if err != nil {
return nil, fmt.Errorf("unable to read oauth2 client secret file %s: %s", rt.config.ClientSecretFile, err)
}
Expand Down Expand Up @@ -836,7 +836,7 @@ func (c *TLSConfig) getClientCertificate(*tls.CertificateRequestInfo) (*tls.Cert

// readCAFile reads the CA cert file from disk.
func readCAFile(f string) ([]byte, error) {
data, err := ioutil.ReadFile(f)
data, err := os.ReadFile(f)
if err != nil {
return nil, fmt.Errorf("unable to load specified CA cert %s: %s", f, err)
}
Expand Down
22 changes: 11 additions & 11 deletions config/http_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -124,7 +124,7 @@ var invalidHTTPClientConfigs = []struct {
func newTestServer(handler func(w http.ResponseWriter, r *http.Request)) (*httptest.Server, error) {
testServer := httptest.NewUnstartedServer(http.HandlerFunc(handler))

tlsCAChain, err := ioutil.ReadFile(TLSCAChainPath)
tlsCAChain, err := os.ReadFile(TLSCAChainPath)
if err != nil {
return nil, fmt.Errorf("Can't read %s", TLSCAChainPath)
}
Expand Down Expand Up @@ -432,7 +432,7 @@ func TestNewClientFromConfig(t *testing.T) {
continue
}

message, err := ioutil.ReadAll(response.Body)
message, err := io.ReadAll(response.Body)
response.Body.Close()
if err != nil {
t.Errorf("Can't read the server response body using this config: %+v", validConfig.clientConfig)
Expand Down Expand Up @@ -630,7 +630,7 @@ func TestTLSConfig(t *testing.T) {
InsecureSkipVerify: false,
}

tlsCAChain, err := ioutil.ReadFile(TLSCAChainPath)
tlsCAChain, err := os.ReadFile(TLSCAChainPath)
if err != nil {
t.Fatalf("Can't read the CA certificate chain (%s)",
TLSCAChainPath)
Expand Down Expand Up @@ -833,7 +833,7 @@ func getCertificateBlobs(t *testing.T) map[string][]byte {
}
bs := make(map[string][]byte, len(files)+1)
for _, f := range files {
b, err := ioutil.ReadFile(f)
b, err := os.ReadFile(f)
if err != nil {
t.Fatal(err)
}
Expand All @@ -848,15 +848,15 @@ func writeCertificate(bs map[string][]byte, src string, dst string) {
if !ok {
panic(fmt.Sprintf("Couldn't find %q in bs", src))
}
if err := ioutil.WriteFile(dst, b, 0664); err != nil {
if err := os.WriteFile(dst, b, 0664); err != nil {
panic(err)
}
}

func TestTLSRoundTripper(t *testing.T) {
bs := getCertificateBlobs(t)

tmpDir, err := ioutil.TempDir("", "tlsroundtripper")
tmpDir, err := os.MkdirTemp("", "tlsroundtripper")
if err != nil {
t.Fatal("Failed to create tmp dir", err)
}
Expand Down Expand Up @@ -976,7 +976,7 @@ func TestTLSRoundTripper(t *testing.T) {
t.Fatalf("Can't connect to the test server")
}

b, err := ioutil.ReadAll(r.Body)
b, err := io.ReadAll(r.Body)
r.Body.Close()
if err != nil {
t.Errorf("Can't read the server response body")
Expand All @@ -993,7 +993,7 @@ func TestTLSRoundTripper(t *testing.T) {
func TestTLSRoundTripperRaces(t *testing.T) {
bs := getCertificateBlobs(t)

tmpDir, err := ioutil.TempDir("", "tlsroundtripper")
tmpDir, err := os.MkdirTemp("", "tlsroundtripper")
if err != nil {
t.Fatal("Failed to create tmp dir", err)
}
Expand Down Expand Up @@ -1137,7 +1137,7 @@ func LoadHTTPConfig(s string) (*HTTPClientConfig, error) {

// LoadHTTPConfigFile parses the given YAML file into a HTTPClientConfig.
func LoadHTTPConfigFile(filename string) (*HTTPClientConfig, []byte, error) {
content, err := ioutil.ReadFile(filename)
content, err := os.ReadFile(filename)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -1305,7 +1305,7 @@ func TestOAuth2WithFile(t *testing.T) {
}))
defer ts.Close()

secretFile, err := ioutil.TempFile("", "oauth2_secret")
secretFile, err := os.CreateTemp("", "oauth2_secret")
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions config/tls_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"bytes"
"crypto/tls"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"
Expand All @@ -29,7 +29,7 @@ import (

// LoadTLSConfig parses the given file into a tls.Config.
func LoadTLSConfig(filename string) (*tls.Config, error) {
content, err := ioutil.ReadFile(filename)
content, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
Expand Down
12 changes: 6 additions & 6 deletions expfmt/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"bytes"
"compress/gzip"
"io"
"io/ioutil"
"os"
"testing"

"github.com/matttproud/golang_protobuf_extensions/pbutil"
Expand Down Expand Up @@ -47,7 +47,7 @@ var parser TextParser
// family DTOs.
func BenchmarkParseText(b *testing.B) {
b.StopTimer()
data, err := ioutil.ReadFile("testdata/text")
data, err := os.ReadFile("testdata/text")
if err != nil {
b.Fatal(err)
}
Expand All @@ -64,7 +64,7 @@ func BenchmarkParseText(b *testing.B) {
// into metric family DTOs.
func BenchmarkParseTextGzip(b *testing.B) {
b.StopTimer()
data, err := ioutil.ReadFile("testdata/text.gz")
data, err := os.ReadFile("testdata/text.gz")
if err != nil {
b.Fatal(err)
}
Expand All @@ -89,7 +89,7 @@ func BenchmarkParseTextGzip(b *testing.B) {
// protobuf-format guarantees bundling at one place.)
func BenchmarkParseProto(b *testing.B) {
b.StopTimer()
data, err := ioutil.ReadFile("testdata/protobuf")
data, err := os.ReadFile("testdata/protobuf")
if err != nil {
b.Fatal(err)
}
Expand All @@ -114,7 +114,7 @@ func BenchmarkParseProto(b *testing.B) {
// protobuf format.
func BenchmarkParseProtoGzip(b *testing.B) {
b.StopTimer()
data, err := ioutil.ReadFile("testdata/protobuf.gz")
data, err := os.ReadFile("testdata/protobuf.gz")
if err != nil {
b.Fatal(err)
}
Expand Down Expand Up @@ -144,7 +144,7 @@ func BenchmarkParseProtoGzip(b *testing.B) {
// separate it from the overhead of the text format parsing.
func BenchmarkParseProtoMap(b *testing.B) {
b.StopTimer()
data, err := ioutil.ReadFile("testdata/protobuf")
data, err := os.ReadFile("testdata/protobuf")
if err != nil {
b.Fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions expfmt/text_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"bufio"
"fmt"
"io"
"io/ioutil"
"math"
"strconv"
"strings"
Expand All @@ -44,7 +43,7 @@ const (
var (
bufPool = sync.Pool{
New: func() interface{} {
return bufio.NewWriter(ioutil.Discard)
return bufio.NewWriter(io.Discard)
},
}
numBufPool = sync.Pool{
Expand Down
3 changes: 1 addition & 2 deletions sigv4/sigv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/textproto"
"path"
Expand Down Expand Up @@ -114,7 +113,7 @@ func (rt *sigV4RoundTripper) RoundTrip(req *http.Request) (*http.Response, error
defer func() {
_, _ = seeker.Seek(0, io.SeekStart)
}()
req.Body = ioutil.NopCloser(seeker)
req.Body = io.NopCloser(seeker)

// Clean path like documented in AWS documentation.
// https://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html
Expand Down
4 changes: 2 additions & 2 deletions sigv4/sigv4_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
package sigv4

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

"gopkg.in/yaml.v2"
)

func loadSigv4Config(filename string) (*SigV4Config, error) {
content, err := ioutil.ReadFile(filename)
content, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 1482587

Please sign in to comment.