Skip to content

Commit

Permalink
chore: remove refs to deprecated io/ioutil
Browse files Browse the repository at this point in the history
Signed-off-by: guoguangwu <[email protected]>
  • Loading branch information
testwill authored and muety committed May 7, 2024
1 parent f2bffe0 commit fd5bdf9
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 28 deletions.
13 changes: 6 additions & 7 deletions middlewares/custom/wakatime.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
routeutils "github.com/muety/wakapi/routes/utils"
"github.com/patrickmn/go-cache"
"io"
"io/ioutil"
"net/http"
"time"
)
Expand Down Expand Up @@ -67,9 +66,9 @@ func (m *WakatimeRelayMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Reque
return
}

body, _ := ioutil.ReadAll(r.Body)
body, _ := io.ReadAll(r.Body)
r.Body.Close()
r.Body = ioutil.NopCloser(bytes.NewBuffer(body))
r.Body = io.NopCloser(bytes.NewBuffer(body))

// prevent cycles
downstreamInstanceId := ownInstanceId
Expand Down Expand Up @@ -149,12 +148,12 @@ func (m *WakatimeRelayMiddleware) filterByCache(r *http.Request) error {
return err
}

body, _ := ioutil.ReadAll(r.Body)
body, _ := io.ReadAll(r.Body)
r.Body.Close()
r.Body = ioutil.NopCloser(bytes.NewBuffer(body))
r.Body = io.NopCloser(bytes.NewBuffer(body))

var rawData interface{}
if err := json.NewDecoder(ioutil.NopCloser(bytes.NewBuffer(body))).Decode(&rawData); err != nil {
if err := json.NewDecoder(io.NopCloser(bytes.NewBuffer(body))).Decode(&rawData); err != nil {
return err
}

Expand Down Expand Up @@ -190,7 +189,7 @@ func (m *WakatimeRelayMiddleware) filterByCache(r *http.Request) error {
if err := json.NewEncoder(&buf).Encode(newData); err != nil {
return err
}
r.Body = ioutil.NopCloser(&buf)
r.Body = io.NopCloser(&buf)

return nil
}
8 changes: 4 additions & 4 deletions routes/api/badge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/muety/wakapi/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"regexp"
Expand Down Expand Up @@ -69,7 +69,7 @@ func TestBadgeHandler_Get(t *testing.T) {

assert.Equal(t, http.StatusOK, res.StatusCode)

data, err := ioutil.ReadAll(res.Body)
data, err := io.ReadAll(res.Body)
if err != nil {
t.Errorf("unextected error. Error: %s", err)
}
Expand All @@ -90,7 +90,7 @@ func TestBadgeHandler_Get(t *testing.T) {

assert.Equal(t, http.StatusForbidden, res.StatusCode)

data, err := ioutil.ReadAll(res.Body)
data, err := io.ReadAll(res.Body)
if err != nil {
t.Errorf("unextected error. Error: %s", err)
}
Expand All @@ -110,7 +110,7 @@ func TestBadgeHandler_Get(t *testing.T) {

assert.Equal(t, http.StatusForbidden, res.StatusCode)

data, err := ioutil.ReadAll(res.Body)
data, err := io.ReadAll(res.Body)
if err != nil {
t.Errorf("unextected error. Error: %s", err)
}
Expand Down
8 changes: 4 additions & 4 deletions routes/compat/wakatime/v1/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/muety/wakapi/middlewares"
"github.com/muety/wakapi/mocks"
"github.com/muety/wakapi/models"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -78,7 +78,7 @@ func TestUsersHandler_Get(t *testing.T) {
res := rec.Result()
defer res.Body.Close()

data, err := ioutil.ReadAll(res.Body)
data, err := io.ReadAll(res.Body)
if err != nil {
t.Errorf("unexpected error. Error: %s", err)
}
Expand All @@ -104,7 +104,7 @@ func TestUsersHandler_Get(t *testing.T) {
res := rec.Result()
defer res.Body.Close()

data, err := ioutil.ReadAll(res.Body)
data, err := io.ReadAll(res.Body)
if err != nil {
t.Errorf("unextected error. Error: %s", err)
}
Expand All @@ -128,7 +128,7 @@ func TestUsersHandler_Get(t *testing.T) {
res := rec.Result()
defer res.Body.Close()

data, err := ioutil.ReadAll(res.Body)
data, err := io.ReadAll(res.Body)
if err != nil {
t.Errorf("unextected error. Error: %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions routes/home_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/muety/wakapi/mocks"
"github.com/muety/wakapi/models"
"github.com/stretchr/testify/assert"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestHomeHandler_Get_NotLoggedIn(t *testing.T) {

assert.Equal(t, http.StatusOK, res.StatusCode)

data, err := ioutil.ReadAll(res.Body)
data, err := io.ReadAll(res.Body)
if err != nil {
t.Errorf("unextected error. Error: %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions routes/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
stripePrice "github.com/stripe/stripe-go/v74/price"
stripeSubscription "github.com/stripe/stripe-go/v74/subscription"
"github.com/stripe/stripe-go/v74/webhook"
"io/ioutil"
"io"
"net/http"
"strings"
"time"
Expand Down Expand Up @@ -197,7 +197,7 @@ func (h *SubscriptionHandler) PostPortal(w http.ResponseWriter, r *http.Request)

func (h *SubscriptionHandler) PostWebhook(w http.ResponseWriter, r *http.Request) {
bodyReader := http.MaxBytesReader(w, r.Body, int64(65536))
payload, err := ioutil.ReadAll(bodyReader)
payload, err := io.ReadAll(bodyReader)
if err != nil {
conf.Log().Request(r).Error("error in stripe webhook request: %v", err)
w.WriteHeader(http.StatusServiceUnavailable)
Expand Down
14 changes: 7 additions & 7 deletions routes/utils/heartbeat_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"encoding/json"
"github.com/muety/wakapi/models"
"io/ioutil"
"io"
"net/http"
)

Expand All @@ -25,11 +25,11 @@ func ParseHeartbeats(r *http.Request) ([]*models.Heartbeat, error) {
func tryParseBulk(r *http.Request) ([]*models.Heartbeat, error) {
var heartbeats []*models.Heartbeat

body, _ := ioutil.ReadAll(r.Body)
body, _ := io.ReadAll(r.Body)
r.Body.Close()
r.Body = ioutil.NopCloser(bytes.NewBuffer(body))
r.Body = io.NopCloser(bytes.NewBuffer(body))

dec := json.NewDecoder(ioutil.NopCloser(bytes.NewBuffer(body)))
dec := json.NewDecoder(io.NopCloser(bytes.NewBuffer(body)))
if err := dec.Decode(&heartbeats); err != nil {
return nil, err
}
Expand All @@ -40,11 +40,11 @@ func tryParseBulk(r *http.Request) ([]*models.Heartbeat, error) {
func tryParseSingle(r *http.Request) ([]*models.Heartbeat, error) {
var heartbeat models.Heartbeat

body, _ := ioutil.ReadAll(r.Body)
body, _ := io.ReadAll(r.Body)
r.Body.Close()
r.Body = ioutil.NopCloser(bytes.NewBuffer(body))
r.Body = io.NopCloser(bytes.NewBuffer(body))

dec := json.NewDecoder(ioutil.NopCloser(bytes.NewBuffer(body)))
dec := json.NewDecoder(io.NopCloser(bytes.NewBuffer(body)))
if err := dec.Decode(&heartbeat); err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions utils/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package utils
import (
"encoding/json"
"html/template"
"io"
"io/fs"
"io/ioutil"
"path"
)

Expand Down Expand Up @@ -44,7 +44,7 @@ func LoadTemplates(templateFs fs.FS, funcs template.FuncMap) (TemplateMap, error
if err != nil {
return nil, err
}
templateData, err := ioutil.ReadAll(templateFile)
templateData, err := io.ReadAll(templateFile)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit fd5bdf9

Please sign in to comment.