Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove references to io/ioutil package #2547

Merged
merged 1 commit into from Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions example/commitpr/main.go
Expand Up @@ -25,7 +25,6 @@ import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
Expand Down Expand Up @@ -118,7 +117,7 @@ func getFileContent(fileArg string) (targetName string, b []byte, err error) {
targetName = files[1]
}

b, err = ioutil.ReadFile(localFile)
b, err = os.ReadFile(localFile)
return targetName, b, err
}

Expand Down
4 changes: 2 additions & 2 deletions example/newfilewithappauth/main.go
Expand Up @@ -10,9 +10,9 @@ package main

import (
"context"
"io/ioutil"
"log"
"net/http"
"os"
"time"

"github.com/bradleyfalzon/ghinstallation/v2"
Expand All @@ -23,7 +23,7 @@ import (
func main() {
const gitHost = "https://git.api.com"

privatePem, err := ioutil.ReadFile("path/to/pem")
privatePem, err := os.ReadFile("path/to/pem")
if err != nil {
log.Fatalf("failed to read pem: %v", err)
}
Expand Down
3 changes: 1 addition & 2 deletions github/gen-accessors.go
Expand Up @@ -21,7 +21,6 @@ import (
"go/format"
"go/parser"
"go/token"
"io/ioutil"
"log"
"os"
"sort"
Expand Down Expand Up @@ -190,7 +189,7 @@ func (t *templateData) dump() error {
return fmt.Errorf("os.Chmod(%q, 0644): %v", filename, err)
}

if err := ioutil.WriteFile(filename, clean, 0444); err != nil {
if err := os.WriteFile(filename, clean, 0444); err != nil {
return err
}

Expand Down
3 changes: 1 addition & 2 deletions github/gen-stringify-test.go
Expand Up @@ -24,7 +24,6 @@ import (
"go/format"
"go/parser"
"go/token"
"io/ioutil"
"log"
"os"
"strings"
Expand Down Expand Up @@ -356,7 +355,7 @@ func (t *templateData) dump() error {
return fmt.Errorf("os.Chmod(%q, 0644): %v", t.filename, err)
}

if err := ioutil.WriteFile(t.filename, clean, 0444); err != nil {
if err := os.WriteFile(t.filename, clean, 0444); err != nil {
return err
}

Expand Down
8 changes: 4 additions & 4 deletions github/git_trees_test.go
Expand Up @@ -9,7 +9,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"io"
"net/http"
"testing"

Expand Down Expand Up @@ -106,7 +106,7 @@ func TestGitService_CreateTree(t *testing.T) {
}

mux.HandleFunc("/repos/o/r/git/trees", func(w http.ResponseWriter, r *http.Request) {
got, err := ioutil.ReadAll(r.Body)
got, err := io.ReadAll(r.Body)
if err != nil {
t.Fatalf("unable to read body: %v", err)
}
Expand Down Expand Up @@ -184,7 +184,7 @@ func TestGitService_CreateTree_Content(t *testing.T) {
}

mux.HandleFunc("/repos/o/r/git/trees", func(w http.ResponseWriter, r *http.Request) {
got, err := ioutil.ReadAll(r.Body)
got, err := io.ReadAll(r.Body)
if err != nil {
t.Fatalf("unable to read body: %v", err)
}
Expand Down Expand Up @@ -264,7 +264,7 @@ func TestGitService_CreateTree_Delete(t *testing.T) {
}

mux.HandleFunc("/repos/o/r/git/trees", func(w http.ResponseWriter, r *http.Request) {
got, err := ioutil.ReadAll(r.Body)
got, err := io.ReadAll(r.Body)
if err != nil {
t.Fatalf("unable to read body: %v", err)
}
Expand Down
9 changes: 4 additions & 5 deletions github/github.go
Expand Up @@ -15,7 +15,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"reflect"
Expand Down Expand Up @@ -710,7 +709,7 @@ func (c *Client) BareDo(ctx context.Context, req *http.Request) (*Response, erro
// Issue #1022
aerr, ok := err.(*AcceptedError)
if ok {
b, readErr := ioutil.ReadAll(resp.Body)
b, readErr := io.ReadAll(resp.Body)
if readErr != nil {
return response, readErr
}
Expand Down Expand Up @@ -770,7 +769,7 @@ func (c *Client) checkRateLimitBeforeDo(req *http.Request, rateLimitCategory rat
StatusCode: http.StatusForbidden,
Request: req,
Header: make(http.Header),
Body: ioutil.NopCloser(strings.NewReader("")),
Body: io.NopCloser(strings.NewReader("")),
}
return &RateLimitError{
Rate: rate,
Expand Down Expand Up @@ -1032,14 +1031,14 @@ func CheckResponse(r *http.Response) error {
}

errorResponse := &ErrorResponse{Response: r}
data, err := ioutil.ReadAll(r.Body)
data, err := io.ReadAll(r.Body)
if err == nil && data != nil {
json.Unmarshal(data, errorResponse)
}
// Re-populate error response body because GitHub error responses are often
// undocumented and inconsistent.
// Issue #1136, #540.
r.Body = ioutil.NopCloser(bytes.NewBuffer(data))
r.Body = io.NopCloser(bytes.NewBuffer(data))
switch {
case r.StatusCode == http.StatusUnauthorized && strings.HasPrefix(r.Header.Get(headerOTP), "required"):
return (*TwoFactorAuthError)(errorResponse)
Expand Down
26 changes: 13 additions & 13 deletions github/github_test.go
Expand Up @@ -10,7 +10,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -70,7 +70,7 @@ func setup() (client *Client, mux *http.ServeMux, serverURL string, teardown fun
// directory, and create the file in that directory. It is the caller's
// responsibility to remove the directory and its contents when no longer needed.
func openTestFile(name, content string) (file *os.File, dir string, err error) {
dir, err = ioutil.TempDir("", "go-github")
dir, err = os.MkdirTemp("", "go-github")
if err != nil {
return nil, dir, err
}
Expand Down Expand Up @@ -133,7 +133,7 @@ func testURLParseError(t *testing.T, err error) {

func testBody(t *testing.T, r *http.Request, want string) {
t.Helper()
b, err := ioutil.ReadAll(r.Body)
b, err := io.ReadAll(r.Body)
if err != nil {
t.Errorf("Error reading request body: %v", err)
}
Expand Down Expand Up @@ -502,7 +502,7 @@ func TestNewRequest(t *testing.T) {
}

// test that body was JSON encoded
body, _ := ioutil.ReadAll(req.Body)
body, _ := io.ReadAll(req.Body)
if got, want := string(body), outBody; got != want {
t.Errorf("NewRequest(%q) Body is %v, want %v", inBody, got, want)
}
Expand Down Expand Up @@ -617,7 +617,7 @@ func TestNewFormRequest(t *testing.T) {
}

// test that body was form encoded
body, _ := ioutil.ReadAll(req.Body)
body, _ := io.ReadAll(req.Body)
if got, want := string(body), outBody; got != want {
t.Errorf("NewFormRequest(%q) Body is %v, want %v", inBody, got, want)
}
Expand Down Expand Up @@ -1398,7 +1398,7 @@ func TestCheckResponse(t *testing.T) {
res := &http.Response{
Request: &http.Request{},
StatusCode: http.StatusBadRequest,
Body: ioutil.NopCloser(strings.NewReader(`{"message":"m",
Body: io.NopCloser(strings.NewReader(`{"message":"m",
"errors": [{"resource": "r", "field": "f", "code": "c"}],
"block": {"reason": "dmca", "created_at": "2016-03-17T15:39:46Z"}}`)),
}
Expand Down Expand Up @@ -1427,7 +1427,7 @@ func TestCheckResponse_RateLimit(t *testing.T) {
Request: &http.Request{},
StatusCode: http.StatusForbidden,
Header: http.Header{},
Body: ioutil.NopCloser(strings.NewReader(`{"message":"m",
Body: io.NopCloser(strings.NewReader(`{"message":"m",
"documentation_url": "url"}`)),
}
res.Header.Set(headerRateLimit, "60")
Expand All @@ -1454,7 +1454,7 @@ func TestCheckResponse_AbuseRateLimit(t *testing.T) {
res := &http.Response{
Request: &http.Request{},
StatusCode: http.StatusForbidden,
Body: ioutil.NopCloser(strings.NewReader(`{"message":"m",
Body: io.NopCloser(strings.NewReader(`{"message":"m",
"documentation_url": "docs.github.com/en/rest/overview/resources-in-the-rest-api#abuse-rate-limits"}`)),
}
err := CheckResponse(res).(*AbuseRateLimitError)
Expand Down Expand Up @@ -1847,7 +1847,7 @@ func TestCheckResponse_noBody(t *testing.T) {
res := &http.Response{
Request: &http.Request{},
StatusCode: http.StatusBadRequest,
Body: ioutil.NopCloser(strings.NewReader("")),
Body: io.NopCloser(strings.NewReader("")),
}
err := CheckResponse(res).(*ErrorResponse)

Expand All @@ -1868,7 +1868,7 @@ func TestCheckResponse_unexpectedErrorStructure(t *testing.T) {
res := &http.Response{
Request: &http.Request{},
StatusCode: http.StatusBadRequest,
Body: ioutil.NopCloser(strings.NewReader(httpBody)),
Body: io.NopCloser(strings.NewReader(httpBody)),
}
err := CheckResponse(res).(*ErrorResponse)

Expand All @@ -1884,7 +1884,7 @@ func TestCheckResponse_unexpectedErrorStructure(t *testing.T) {
if !errors.Is(err, want) {
t.Errorf("Error = %#v, want %#v", err, want)
}
data, err2 := ioutil.ReadAll(err.Response.Body)
data, err2 := io.ReadAll(err.Response.Body)
if err2 != nil {
t.Fatalf("failed to read response body: %v", err)
}
Expand Down Expand Up @@ -2373,9 +2373,9 @@ func TestBareDo_returnsOpenBody(t *testing.T) {
t.Fatalf("client.BareDo returned error: %v", err)
}

got, err := ioutil.ReadAll(resp.Body)
got, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("ioutil.ReadAll returned error: %v", err)
t.Fatalf("io.ReadAll returned error: %v", err)
}
if string(got) != expectedBody {
t.Fatalf("Expected %q, got %q", expectedBody, string(got))
Expand Down
5 changes: 2 additions & 3 deletions github/messages.go
Expand Up @@ -19,7 +19,6 @@ import (
"fmt"
"hash"
"io"
"io/ioutil"
"mime"
"net/http"
"net/url"
Expand Down Expand Up @@ -171,7 +170,7 @@ func ValidatePayloadFromBody(contentType string, readable io.Reader, signature s
switch contentType {
case "application/json":
var err error
if body, err = ioutil.ReadAll(readable); err != nil {
if body, err = io.ReadAll(readable); err != nil {
return nil, err
}

Expand All @@ -185,7 +184,7 @@ func ValidatePayloadFromBody(contentType string, readable io.Reader, signature s
const payloadFormParam = "payload"

var err error
if body, err = ioutil.ReadAll(readable); err != nil {
if body, err = io.ReadAll(readable); err != nil {
return nil, err
}

Expand Down
10 changes: 5 additions & 5 deletions github/repos_contents_test.go
Expand Up @@ -9,7 +9,7 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"testing"
Expand Down Expand Up @@ -146,7 +146,7 @@ func TestRepositoriesService_DownloadContents_Success(t *testing.T) {
t.Errorf("Repositories.DownloadContents returned status code %v, want %v", got, want)
}

bytes, err := ioutil.ReadAll(r)
bytes, err := io.ReadAll(r)
if err != nil {
t.Errorf("Error reading response body: %v", err)
}
Expand Down Expand Up @@ -198,7 +198,7 @@ func TestRepositoriesService_DownloadContents_FailedResponse(t *testing.T) {
t.Errorf("Repositories.DownloadContents returned status code %v, want %v", got, want)
}

bytes, err := ioutil.ReadAll(r)
bytes, err := io.ReadAll(r)
if err != nil {
t.Errorf("Error reading response body: %v", err)
}
Expand Down Expand Up @@ -276,7 +276,7 @@ func TestRepositoriesService_DownloadContentsWithMeta_Success(t *testing.T) {
t.Errorf("Repositories.DownloadContentsWithMeta returned status code %v, want %v", got, want)
}

bytes, err := ioutil.ReadAll(r)
bytes, err := io.ReadAll(r)
if err != nil {
t.Errorf("Error reading response body: %v", err)
}
Expand Down Expand Up @@ -339,7 +339,7 @@ func TestRepositoriesService_DownloadContentsWithMeta_FailedResponse(t *testing.
t.Errorf("Repositories.DownloadContentsWithMeta returned status code %v, want %v", got, want)
}

bytes, err := ioutil.ReadAll(r)
bytes, err := io.ReadAll(r)
if err != nil {
t.Errorf("Error reading response body: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions github/repos_pages_test.go
Expand Up @@ -10,7 +10,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"testing"

Expand Down Expand Up @@ -118,7 +118,7 @@ func TestRepositoriesService_UpdatePages_NullCNAME(t *testing.T) {
}

mux.HandleFunc("/repos/o/r/pages", func(w http.ResponseWriter, r *http.Request) {
got, err := ioutil.ReadAll(r.Body)
got, err := io.ReadAll(r.Body)
if err != nil {
t.Fatalf("unable to read body: %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions github/repos_releases_test.go
Expand Up @@ -10,7 +10,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"os"
"strings"
Expand Down Expand Up @@ -442,7 +442,7 @@ func TestRepositoriesService_DownloadReleaseAsset_Stream(t *testing.T) {
t.Errorf("Repositories.DownloadReleaseAsset returned error: %v", err)
}
want := []byte("Hello World")
content, err := ioutil.ReadAll(reader)
content, err := io.ReadAll(reader)
if err != nil {
t.Errorf("Repositories.DownloadReleaseAsset returned bad reader: %v", err)
}
Expand Down Expand Up @@ -498,7 +498,7 @@ func TestRepositoriesService_DownloadReleaseAsset_FollowRedirect(t *testing.T) {

ctx := context.Background()
reader, _, err := client.Repositories.DownloadReleaseAsset(ctx, "o", "r", 1, http.DefaultClient)
content, err := ioutil.ReadAll(reader)
content, err := io.ReadAll(reader)
if err != nil {
t.Errorf("Repositories.DownloadReleaseAsset returned error: %v", err)
}
Expand Down