Skip to content

Commit

Permalink
chore: replace ioutil with io or os package (#1310)
Browse files Browse the repository at this point in the history
  • Loading branch information
nakamasato authored Aug 15, 2024
1 parent b9d4317 commit 25fefc8
Show file tree
Hide file tree
Showing 14 changed files with 36 additions and 40 deletions.
6 changes: 3 additions & 3 deletions chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"io/ioutil"
"io"
"net/http"
"net/url"
"regexp"
Expand Down Expand Up @@ -226,11 +226,11 @@ func (api *Client) SendMessageContext(ctx context.Context, channelID string, opt
}

if api.Debug() {
reqBody, err := ioutil.ReadAll(req.Body)
reqBody, err := io.ReadAll(req.Body)
if err != nil {
return "", "", "", err
}
req.Body = ioutil.NopCloser(bytes.NewBuffer(reqBody))
req.Body = io.NopCloser(bytes.NewBuffer(reqBody))
api.Debugf("Sending request: %s", redactToken(reqBody))
}

Expand Down
10 changes: 5 additions & 5 deletions chat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package slack
import (
"bytes"
"encoding/json"
"io/ioutil"
"io"
"log"
"net/http"
"net/url"
Expand Down Expand Up @@ -216,7 +216,7 @@ func TestPostMessage(t *testing.T) {
t.Run(name, func(t *testing.T) {
http.DefaultServeMux = new(http.ServeMux)
http.HandleFunc(test.endpoint, func(rw http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
t.Errorf("unexpected error: %v", err)
return
Expand All @@ -242,7 +242,7 @@ func TestPostMessageWithBlocksWhenMsgOptionResponseURLApplied(t *testing.T) {

http.DefaultServeMux = new(http.ServeMux)
http.HandleFunc("/response-url", func(rw http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
t.Errorf("unexpected error: %v", err)
return
Expand Down Expand Up @@ -270,7 +270,7 @@ func TestPostMessageWithBlocksWhenMsgOptionResponseURLApplied(t *testing.T) {
func TestPostMessageWhenMsgOptionReplaceOriginalApplied(t *testing.T) {
http.DefaultServeMux = new(http.ServeMux)
http.HandleFunc("/response-url", func(rw http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
t.Errorf("unexpected error: %v", err)
return
Expand All @@ -297,7 +297,7 @@ func TestPostMessageWhenMsgOptionReplaceOriginalApplied(t *testing.T) {
func TestPostMessageWhenMsgOptionDeleteOriginalApplied(t *testing.T) {
http.DefaultServeMux = new(http.ServeMux)
http.HandleFunc("/response-url", func(rw http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
t.Errorf("unexpected error: %v", err)
return
Expand Down
4 changes: 2 additions & 2 deletions examples/dialog/dialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"encoding/json"
"io/ioutil"
"io"
"log"
"net/http"
"net/url"
Expand All @@ -26,7 +26,7 @@ func handler(w http.ResponseWriter, r *http.Request) {

// Read request body
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("[ERROR] Fail to read request body: %v", err)
Expand Down
4 changes: 2 additions & 2 deletions examples/eventsapi/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"os"

Expand All @@ -18,7 +18,7 @@ func main() {
signingSecret := os.Getenv("SLACK_SIGNING_SECRET")

http.HandleFunc("/events-endpoint", func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
Expand Down
6 changes: 3 additions & 3 deletions examples/modal/modal.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"

"github.com/slack-go/slack"
Expand Down Expand Up @@ -95,13 +95,13 @@ func verifySigningSecret(r *http.Request) error {
return err
}

body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
fmt.Println(err.Error())
return err
}
// Need to use r.Body again when unmarshalling SlashCommand and InteractionCallback
r.Body = ioutil.NopCloser(bytes.NewBuffer(body))
r.Body = io.NopCloser(bytes.NewBuffer(body))

verifier.Write(body)
if err = verifier.Ensure(); err != nil {
Expand Down
3 changes: 1 addition & 2 deletions examples/slash/slash.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"

"github.com/slack-go/slack"
Expand All @@ -27,7 +26,7 @@ func main() {
return
}

r.Body = ioutil.NopCloser(io.TeeReader(r.Body, &verifier))
r.Body = io.NopCloser(io.TeeReader(r.Body, &verifier))
s, err := slack.SlashCommandParse(r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
Expand Down
6 changes: 3 additions & 3 deletions examples/workflow_step/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"net/url"
Expand All @@ -25,7 +25,7 @@ func handleMyWorkflowStep(w http.ResponseWriter, r *http.Request) {
}

// see: https://github.com/slack-go/slack/blob/master/examples/eventsapi/events.go
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
Expand Down Expand Up @@ -88,7 +88,7 @@ func handleInteraction(w http.ResponseWriter, r *http.Request) {
return
}

body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
Expand Down
6 changes: 3 additions & 3 deletions examples/workflow_step/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ package main

import (
"bytes"
"io/ioutil"
"io"
"net/http"

"github.com/slack-go/slack"
)

func (v *SecretsVerifierMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
r.Body.Close()
r.Body = ioutil.NopCloser(bytes.NewBuffer(body))
r.Body = io.NopCloser(bytes.NewBuffer(body))

sv, err := slack.NewSecretsVerifier(r.Header, appCtx.config.signingSecret)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package slack
import (
"bytes"
"encoding/json"
"io/ioutil"
"io"
"log"
"net/http"
"net/url"
Expand Down Expand Up @@ -44,7 +44,7 @@ func (h *fileCommentHandler) handler(w http.ResponseWriter, r *http.Request) {
type mockHTTPClient struct{}

func (m *mockHTTPClient) Do(*http.Request) (*http.Response, error) {
return &http.Response{StatusCode: 200, Body: ioutil.NopCloser(bytes.NewBufferString(`OK`))}, nil
return &http.Response{StatusCode: 200, Body: io.NopCloser(bytes.NewBufferString(`OK`))}, nil
}

func TestSlack_GetFile(t *testing.T) {
Expand Down
5 changes: 2 additions & 3 deletions misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"mime"
"mime/multipart"
"net/http"
Expand Down Expand Up @@ -127,7 +126,7 @@ func jsonReq(ctx context.Context, endpoint string, body interface{}) (req *http.
}

func parseResponseBody(body io.ReadCloser, intf interface{}, d Debug) error {
response, err := ioutil.ReadAll(body)
response, err := io.ReadAll(body)
if err != nil {
return err
}
Expand Down Expand Up @@ -316,7 +315,7 @@ func newJSONParser(dst interface{}) responseParser {

func newTextParser(dst interface{}) responseParser {
return func(resp *http.Response) error {
b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions reminders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package slack

import (
"bytes"
"io/ioutil"
"io"
"net/http"
"reflect"
"testing"
Expand Down Expand Up @@ -185,7 +185,7 @@ func (m *mockRemindersListHTTPClient) Do(*http.Request) (*http.Response, error)
]
}`

return &http.Response{StatusCode: 200, Body: ioutil.NopCloser(bytes.NewBufferString(responseString))}, nil
return &http.Response{StatusCode: 200, Body: io.NopCloser(bytes.NewBufferString(responseString))}, nil
}

func TestSlack_ListReminders(t *testing.T) {
Expand Down
10 changes: 5 additions & 5 deletions slacktest/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"net/url"
Expand Down Expand Up @@ -46,7 +46,7 @@ type GroupConversationResponse struct {
}

func (sts *Server) conversationsInfoHandler(w http.ResponseWriter, r *http.Request) {
data, err := ioutil.ReadAll(r.Body)
data, err := io.ReadAll(r.Body)
if err != nil {
msg := fmt.Sprintf("error reading body: %s", err.Error())
log.Printf(msg)
Expand Down Expand Up @@ -126,7 +126,7 @@ func reactionAddHandler(w http.ResponseWriter, _ *http.Request) {
// handle chat.postMessage
func (sts *Server) postMessageHandler(w http.ResponseWriter, r *http.Request) {
serverAddr := r.Context().Value(ServerBotHubNameContextKey).(string)
data, err := ioutil.ReadAll(r.Body)
data, err := io.ReadAll(r.Body)
if err != nil {
msg := fmt.Sprintf("error reading body: %s", err.Error())
log.Printf(msg)
Expand Down Expand Up @@ -218,7 +218,7 @@ func (sts *Server) postMessageHandler(w http.ResponseWriter, r *http.Request) {

// RTMConnectHandler generates a valid connection
func RTMConnectHandler(w http.ResponseWriter, r *http.Request) {
_, err := ioutil.ReadAll(r.Body)
_, err := io.ReadAll(r.Body)
if err != nil {
msg := fmt.Sprintf("Error reading body: %s", err.Error())
log.Printf(msg)
Expand Down Expand Up @@ -248,7 +248,7 @@ func RTMConnectHandler(w http.ResponseWriter, r *http.Request) {
}

func rtmStartHandler(w http.ResponseWriter, r *http.Request) {
_, err := ioutil.ReadAll(r.Body)
_, err := io.ReadAll(r.Body)
if err != nil {
msg := fmt.Sprintf("Error reading body: %s", err.Error())
log.Printf(msg)
Expand Down
5 changes: 2 additions & 3 deletions users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"image/draw"
"image/png"
"io"
"io/ioutil"
"net/http"
"os"
"reflect"
Expand Down Expand Up @@ -556,7 +555,7 @@ func setUserPhotoHandler(wantBytes []byte, wantParams UserSetPhotoParams) http.H
httpTestErrReply(w, true, fmt.Sprintf("failed to open uploaded file: %+v", err))
return
}
gotBytes, err := ioutil.ReadAll(file)
gotBytes, err := io.ReadAll(file)
if err != nil {
httpTestErrReply(w, true, fmt.Sprintf("failed to read uploaded file: %+v", err))
return
Expand All @@ -577,7 +576,7 @@ func createUserPhoto(t *testing.T) (*os.File, []byte, func()) {
photo := image.NewRGBA(image.Rect(0, 0, 64, 64))
draw.Draw(photo, photo.Bounds(), image.Black, image.ZP, draw.Src)

f, err := ioutil.TempFile(os.TempDir(), "profile.png")
f, err := os.CreateTemp(os.TempDir(), "profile.png")
if err != nil {
t.Fatalf("failed to create test photo: %+v\n", err)
}
Expand Down
3 changes: 1 addition & 2 deletions webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
)

Expand Down Expand Up @@ -57,7 +56,7 @@ func PostWebhookCustomHTTPContext(ctx context.Context, url string, httpClient *h
return fmt.Errorf("failed to post webhook: %w", err)
}
defer func() {
io.Copy(ioutil.Discard, resp.Body)
io.Copy(io.Discard, resp.Body)
resp.Body.Close()
}()

Expand Down

0 comments on commit 25fefc8

Please sign in to comment.