Skip to content

Commit

Permalink
chore: remove refs to deprecated io/ioutil (#2008)
Browse files Browse the repository at this point in the history
  • Loading branch information
testwill authored Jun 7, 2023
1 parent 95d3fd1 commit db9dde2
Show file tree
Hide file tree
Showing 26 changed files with 65 additions and 81 deletions.
7 changes: 3 additions & 4 deletions examples/debug.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"
)
Expand All @@ -22,11 +21,11 @@ func (t *logTransport) RoundTrip(req *http.Request) (*http.Response, error) {

os.Stdout.Write([]byte("\n[request]\n"))
if req.Body != nil {
req.Body = ioutil.NopCloser(&readButCopy{req.Body, &buf})
req.Body = io.NopCloser(&readButCopy{req.Body, &buf})
}
req.Write(os.Stdout)
if req.Body != nil {
req.Body = ioutil.NopCloser(&buf)
req.Body = io.NopCloser(&buf)
}
os.Stdout.Write([]byte("\n[/request]\n"))

Expand All @@ -40,7 +39,7 @@ func (t *logTransport) RoundTrip(req *http.Request) (*http.Response, error) {
res.Body = nil
res.Write(os.Stdout)
if body != nil {
res.Body = ioutil.NopCloser(&echoAsRead{body})
res.Body = io.NopCloser(&echoAsRead{body})
}
}

Expand Down
3 changes: 1 addition & 2 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"flag"
"fmt"
"hash/fnv"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -198,7 +197,7 @@ func valueOrFileContents(value string, filename string) string {
if value != "" {
return value
}
slurp, err := ioutil.ReadFile(filename)
slurp, err := os.ReadFile(filename)
if err != nil {
log.Fatalf("Error reading %q: %v", filename, err)
}
Expand Down
4 changes: 2 additions & 2 deletions google-api-go-generator/clients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"math"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -47,7 +47,7 @@ func (h *myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if h.location != "" {
w.Header().Set("Location", h.location)
}
h.body, h.err = ioutil.ReadAll(r.Body)
h.body, h.err = io.ReadAll(r.Body)
fmt.Fprintf(w, "{}")
}

Expand Down
15 changes: 7 additions & 8 deletions google-api-go-generator/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"fmt"
"go/format"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
Expand Down Expand Up @@ -214,7 +213,7 @@ func getAPIs() []*API {
log.Fatalf("-cache=true not compatible with -publiconly=false")
}
var err error
bytes, err = ioutil.ReadFile(apiListFile)
bytes, err = os.ReadFile(apiListFile)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -275,7 +274,7 @@ func getAPIsFromFile() []*API {
}

func apiFromFile(file string) (*API, error) {
jsonBytes, err := ioutil.ReadFile(file)
jsonBytes, err := os.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("Error reading %s: %v", file, err)
}
Expand All @@ -298,7 +297,7 @@ func checkAndUpdateSpecFile(file string, contents []byte) error {
if _, err := os.Stat(file); os.IsNotExist(err) {
return writeFile(file, contents)
}
existing, err := ioutil.ReadFile(file)
existing, err := os.ReadFile(file)
if err != nil {
return err
}
Expand Down Expand Up @@ -329,15 +328,15 @@ func isNewerRevision(old []byte, new []byte) error {

func writeFile(file string, contents []byte) error {
// Don't write it if the contents are identical.
existing, err := ioutil.ReadFile(file)
existing, err := os.ReadFile(file)
if err == nil && (bytes.Equal(existing, contents) || basicallyEqual(existing, contents)) {
return nil
}
outdir := filepath.Dir(file)
if err = os.MkdirAll(outdir, 0755); err != nil {
return fmt.Errorf("failed to Mkdir %s: %v", outdir, err)
}
return ioutil.WriteFile(file, contents, 0644)
return os.WriteFile(file, contents, 0644)
}

var ignoreLines = regexp.MustCompile(`(?m)^\s+"(?:etag|revision)": ".+\n`)
Expand Down Expand Up @@ -368,7 +367,7 @@ func slurpURL(urlStr string) []byte {
log.Printf("WARNING: URL %s served status code %d", urlStr, res.StatusCode)
return nil
}
bs, err := ioutil.ReadAll(res.Body)
bs, err := io.ReadAll(res.Body)
if err != nil {
log.Fatalf("Error reading body of URL %s: %v", urlStr, err)
}
Expand Down Expand Up @@ -523,7 +522,7 @@ func (a *API) jsonBytes() []byte {
var slurp []byte
var err error
if *useCache {
slurp, err = ioutil.ReadFile(a.JSONFile())
slurp, err = os.ReadFile(a.JSONFile())
if err != nil {
log.Fatal(err)
}
Expand Down
16 changes: 8 additions & 8 deletions google-api-go-generator/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
Expand Down Expand Up @@ -63,17 +63,17 @@ func TestAPIs(t *testing.T) {
goldenFile := filepath.Join("testdata", name+".want")
if *updateGolden {
clean := strings.Replace(string(clean), fmt.Sprintf("gdcl/%s", internal.Version), "gdcl/00000000", -1)
if err := ioutil.WriteFile(goldenFile, []byte(clean), 0644); err != nil {
if err := os.WriteFile(goldenFile, []byte(clean), 0644); err != nil {
t.Fatal(err)
}
}
want, err := ioutil.ReadFile(goldenFile)
want, err := os.ReadFile(goldenFile)
if err != nil {
t.Fatal(err)
}
wantStr := strings.Replace(string(want), "gdcl/00000000", fmt.Sprintf("gdcl/%s", internal.Version), -1)
if !bytes.Equal([]byte(wantStr), clean) {
tf, _ := ioutil.TempFile("", "api-"+name+"-got-json.")
tf, _ := os.CreateTemp("", "api-"+name+"-got-json.")
if _, err := tf.Write(clean); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -219,13 +219,13 @@ func TestSupportsPaging(t *testing.T) {

func TestIsNewerRevision(t *testing.T) {
olderBytesPath, newerBytesPath := filepath.Join("testdata", "rev20200415.json"), filepath.Join("testdata", "rev20200416.json")
olderBytes, err := ioutil.ReadFile(olderBytesPath)
olderBytes, err := os.ReadFile(olderBytesPath)
if err != nil {
t.Fatalf("ioutil.ReadFile(%q) = %v; want nil", olderBytesPath, err)
t.Fatalf("os.ReadFile(%q) = %v; want nil", olderBytesPath, err)
}
newerBytes, err := ioutil.ReadFile(newerBytesPath)
newerBytes, err := os.ReadFile(newerBytesPath)
if err != nil {
t.Fatalf("ioutil.ReadFile(%q) = %v; want nil", newerBytesPath, err)
t.Fatalf("os.ReadFile(%q) = %v; want nil", newerBytesPath, err)
}

// newBytes > oldBytes
Expand Down
6 changes: 3 additions & 3 deletions google-api-go-generator/internal/disco/disco_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package disco

import (
"io/ioutil"
"os"
"reflect"
"strings"
"testing"
Expand All @@ -17,7 +17,7 @@ var stringSchema = &Schema{
}

func TestDocument(t *testing.T) {
bytes, err := ioutil.ReadFile("testdata/test-api.json")
bytes, err := os.ReadFile("testdata/test-api.json")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -265,7 +265,7 @@ func TestSchemaErrors(t *testing.T) {
}

func TestErrorDoc(t *testing.T) {
bytes, err := ioutil.ReadFile("testdata/error.json")
bytes, err := os.ReadFile("testdata/error.json")
if err != nil {
t.Fatal(err)
}
Expand Down
5 changes: 2 additions & 3 deletions googleapi/googleapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -144,7 +143,7 @@ func CheckResponse(res *http.Response) error {
if res.StatusCode >= 200 && res.StatusCode <= 299 {
return nil
}
slurp, err := ioutil.ReadAll(res.Body)
slurp, err := io.ReadAll(res.Body)
if err == nil {
jerr := new(errorReply)
err = json.Unmarshal(slurp, jerr)
Expand Down Expand Up @@ -184,7 +183,7 @@ func CheckMediaResponse(res *http.Response) error {
if res.StatusCode >= 200 && res.StatusCode <= 299 {
return nil
}
slurp, _ := ioutil.ReadAll(io.LimitReader(res.Body, 1<<20))
slurp, _ := io.ReadAll(io.LimitReader(res.Body, 1<<20))
return &Error{
Code: res.StatusCode,
Body: string(slurp),
Expand Down
4 changes: 2 additions & 2 deletions googleapi/googleapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package googleapi

import (
"encoding/json"
"io/ioutil"
"io"
"net/http"
"net/url"
"reflect"
Expand Down Expand Up @@ -318,7 +318,7 @@ func TestCheckResponse(t *testing.T) {
for _, test := range checkResponseTests {
res := test.in
if test.bodyText != "" {
res.Body = ioutil.NopCloser(strings.NewReader(test.bodyText))
res.Body = io.NopCloser(strings.NewReader(test.bodyText))
}
g := CheckResponse(res)
if !reflect.DeepEqual(g, test.want) {
Expand Down
3 changes: 1 addition & 2 deletions header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package api

import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -43,7 +42,7 @@ func TestLicense(t *testing.T) {
return nil
}

src, err := ioutil.ReadFile(path)
src, err := os.ReadFile(path)
if err != nil {
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions idtoken/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"crypto/rsa"
"encoding/base64"
"encoding/json"
"io/ioutil"
"io"
"math/big"
"net/http"
"testing"
Expand Down Expand Up @@ -97,7 +97,7 @@ func TestValidateRS256(t *testing.T) {
}
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewReader(b)),
Body: io.NopCloser(bytes.NewReader(b)),
Header: make(http.Header),
}
}),
Expand Down Expand Up @@ -207,7 +207,7 @@ func TestValidateES256(t *testing.T) {
}
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewReader(b)),
Body: io.NopCloser(bytes.NewReader(b)),
Header: make(http.Header),
}
}),
Expand Down
3 changes: 1 addition & 2 deletions impersonate/idtoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"time"

Expand Down Expand Up @@ -109,7 +108,7 @@ func (i impersonatedIDTokenSource) Token() (*oauth2.Token, error) {
return nil, fmt.Errorf("impersonate: unable to generate ID token: %v", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20))
body, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
if err != nil {
return nil, fmt.Errorf("impersonate: unable to read body: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions impersonate/idtoken_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"bytes"
"context"
"encoding/json"
"io/ioutil"
"io"
"net/http"
"testing"

Expand Down Expand Up @@ -56,7 +56,7 @@ func TestIDTokenSource(t *testing.T) {
}
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewReader(b)),
Body: io.NopCloser(bytes.NewReader(b)),
Header: make(http.Header),
}
}),
Expand Down
3 changes: 1 addition & 2 deletions impersonate/impersonate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"time"

Expand Down Expand Up @@ -161,7 +160,7 @@ func (i impersonatedTokenSource) Token() (*oauth2.Token, error) {
return nil, fmt.Errorf("impersonate: unable to generate access token: %v", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20))
body, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
if err != nil {
return nil, fmt.Errorf("impersonate: unable to read body: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions impersonate/impersonate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"bytes"
"context"
"encoding/json"
"io/ioutil"
"io"
"net/http"
"strings"
"testing"
Expand Down Expand Up @@ -67,7 +67,7 @@ func TestTokenSource_serviceAccount(t *testing.T) {
}
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewReader(b)),
Body: io.NopCloser(bytes.NewReader(b)),
Header: http.Header{},
}
}
Expand Down
5 changes: 2 additions & 3 deletions impersonate/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -123,7 +122,7 @@ func (u userTokenSource) signJWT() (string, error) {
if err != nil {
return "", fmt.Errorf("impersonate: unable to sign JWT: %v", err)
}
body, err := ioutil.ReadAll(io.LimitReader(rawResp.Body, 1<<20))
body, err := io.ReadAll(io.LimitReader(rawResp.Body, 1<<20))
if err != nil {
return "", fmt.Errorf("impersonate: unable to read body: %v", err)
}
Expand All @@ -148,7 +147,7 @@ func (u userTokenSource) exchangeToken(signedJWT string) (*oauth2.Token, error)
if err != nil {
return nil, fmt.Errorf("impersonate: unable to exchange token: %v", err)
}
body, err := ioutil.ReadAll(io.LimitReader(rawResp.Body, 1<<20))
body, err := io.ReadAll(io.LimitReader(rawResp.Body, 1<<20))
if err != nil {
return nil, fmt.Errorf("impersonate: unable to read body: %v", err)
}
Expand Down
Loading

0 comments on commit db9dde2

Please sign in to comment.