Skip to content

Commit

Permalink
chore: remove refs to deprecated io/ioutil
Browse files Browse the repository at this point in the history
  • Loading branch information
testwill committed Jul 18, 2023
1 parent f9dea19 commit 0a71fad
Show file tree
Hide file tree
Showing 35 changed files with 87 additions and 104 deletions.
6 changes: 3 additions & 3 deletions conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package conf
import (
"bytes"
"encoding/json"
"io/ioutil"
"io"
httpClient "net/http"
netUrl "net/url"
"os"
Expand Down Expand Up @@ -221,7 +221,7 @@ func getYamlFileFromHTTP(url string) ([]byte, error) {
return nil, err
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
return io.ReadAll(resp.Body)
}

func getYamlFileFromFile(path string) ([]byte, error) {
Expand All @@ -232,7 +232,7 @@ func getYamlFileFromFile(path string) ([]byte, error) {
if f.IsDir() {
return mergeYamlFiles(path)
}
return ioutil.ReadFile(path)
return os.ReadFile(path)
}

func getYamlFile(path string) ([]byte, error) {
Expand Down
3 changes: 1 addition & 2 deletions conf/conf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
httpClient "net/http"
"os"
Expand Down Expand Up @@ -103,7 +102,7 @@ func TestGetYamlFileFromFile(t *testing.T) {
t.Errorf("getYamlFileFromFile(\"/tmp/nonexistent\") = nil, expected error")
}

tmpfile, err := ioutil.TempFile("", "invalid*.yaml")
tmpfile, err := os.CreateTemp("", "invalid*.yaml")
if err != nil {
t.Errorf("TempFile(\"invalid*.yaml\") %v", err)
}
Expand Down
3 changes: 1 addition & 2 deletions daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package daemon

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -80,7 +79,7 @@ func TestPIDFileSymLink(t *testing.T) {
t.Fatalf("Could not found the pid file, %v", err)
}

buf, err := ioutil.ReadFile(filepath.Join(path, "test.txt"))
buf, err := os.ReadFile(filepath.Join(path, "test.txt"))
if err != nil {
t.Fatalf("Could not read the pid file, %v", err)
}
Expand Down
3 changes: 1 addition & 2 deletions global/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"crypto/x509"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -200,7 +199,7 @@ func (t *TLS) Config() (*tls.Config, error) {
return nil, nil
}

cert, err := ioutil.ReadFile(t.CA)
cert, err := os.ReadFile(t.CA)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions notify/dingtalk/dingtalk.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"time"
Expand Down Expand Up @@ -85,7 +85,7 @@ func (c *NotifyConfig) SendDingtalkNotification(title, msg string) error {
defer resp.Body.Close()
}

buf, err := ioutil.ReadAll(resp.Body)
buf, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
Expand Down
11 changes: 5 additions & 6 deletions notify/dingtalk/dingtalk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package dingtalk
import (
"errors"
"io"
"io/ioutil"
"net/http"
"reflect"
"strings"
Expand Down Expand Up @@ -52,7 +51,7 @@ func TestDingTalk(t *testing.T) {

var client *http.Client
monkey.PatchInstanceMethod(reflect.TypeOf(client), "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) {
r := ioutil.NopCloser(strings.NewReader(`{"errmsg": "ok", "errcode": 0}`))
r := io.NopCloser(strings.NewReader(`{"errmsg": "ok", "errcode": 0}`))
return &http.Response{
StatusCode: 200,
Body: r,
Expand All @@ -63,7 +62,7 @@ func TestDingTalk(t *testing.T) {

// bad response
monkey.PatchInstanceMethod(reflect.TypeOf(client), "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) {
r := ioutil.NopCloser(strings.NewReader(`{"errmsg": "error", "errcode": 1}`))
r := io.NopCloser(strings.NewReader(`{"errmsg": "error", "errcode": 1}`))
return &http.Response{
StatusCode: 200,
Body: r,
Expand All @@ -74,7 +73,7 @@ func TestDingTalk(t *testing.T) {

// bad json
monkey.PatchInstanceMethod(reflect.TypeOf(client), "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) {
r := ioutil.NopCloser(strings.NewReader(`{"errmsg": "error", "errcode = 1}`))
r := io.NopCloser(strings.NewReader(`{"errmsg": "error", "errcode = 1}`))
return &http.Response{
StatusCode: 200,
Body: r,
Expand All @@ -83,8 +82,8 @@ func TestDingTalk(t *testing.T) {
err = conf.SendDingtalkNotification("title", "message")
assertError(t, err, "Error response from Dingtalk [200]", true)

// bad ioutil.ReadAll
monkey.Patch(ioutil.ReadAll, func(r io.Reader) ([]byte, error) {
// bad io.ReadAll
monkey.Patch(io.ReadAll, func(r io.Reader) ([]byte, error) {
return nil, errors.New("read error")
})
err = conf.SendDingtalkNotification("title", "message")
Expand Down
4 changes: 2 additions & 2 deletions notify/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"strings"
"time"
Expand Down Expand Up @@ -343,7 +343,7 @@ func (c *NotifyConfig) SendDiscordNotification(discord Discord, tag string) erro
}
defer resp.Body.Close()

buf, err := ioutil.ReadAll(resp.Body)
buf, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
Expand Down
9 changes: 4 additions & 5 deletions notify/discord/discord_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
"reflect"
Expand Down Expand Up @@ -174,7 +173,7 @@ func TestDiscordNotify(t *testing.T) {

var client *http.Client
monkey.PatchInstanceMethod(reflect.TypeOf(client), "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) {
r := ioutil.NopCloser(strings.NewReader(``))
r := io.NopCloser(strings.NewReader(``))
return &http.Response{
StatusCode: 204,
Body: r,
Expand All @@ -191,7 +190,7 @@ func TestDiscordNotify(t *testing.T) {

// error response
monkey.PatchInstanceMethod(reflect.TypeOf(client), "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) {
r := ioutil.NopCloser(strings.NewReader(`no permission`))
r := io.NopCloser(strings.NewReader(`no permission`))
return &http.Response{
StatusCode: 403,
Body: r,
Expand All @@ -202,8 +201,8 @@ func TestDiscordNotify(t *testing.T) {
conf.Notify(r)
assert.Contains(t, buf.String(), "no permission")

// ioutil.ReadAll Error
monkey.Patch(ioutil.ReadAll, func(r io.Reader) ([]byte, error) {
// io.ReadAll Error
monkey.Patch(io.ReadAll, func(r io.Reader) ([]byte, error) {
return nil, errors.New("read error")
})
buf.Reset()
Expand Down
4 changes: 2 additions & 2 deletions notify/lark/lark.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"

"github.com/megaease/easeprobe/global"
Expand Down Expand Up @@ -72,7 +72,7 @@ func (c *NotifyConfig) SendLarkNotification(msg string) error {
defer resp.Body.Close()
}

buf, err := ioutil.ReadAll(resp.Body)
buf, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
Expand Down
11 changes: 5 additions & 6 deletions notify/lark/lark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package lark
import (
"errors"
"io"
"io/ioutil"
"net/http"
"reflect"
"strings"
Expand All @@ -45,7 +44,7 @@ func TestLark(t *testing.T) {

var client *http.Client
monkey.PatchInstanceMethod(reflect.TypeOf(client), "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) {
r := ioutil.NopCloser(strings.NewReader(`{"StatusCode": 0}`))
r := io.NopCloser(strings.NewReader(`{"StatusCode": 0}`))
return &http.Response{
StatusCode: 200,
Body: r,
Expand All @@ -56,7 +55,7 @@ func TestLark(t *testing.T) {

// bad response
monkey.PatchInstanceMethod(reflect.TypeOf(client), "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) {
r := ioutil.NopCloser(strings.NewReader(`{"StatusCode": "100"}`))
r := io.NopCloser(strings.NewReader(`{"StatusCode": "100"}`))
return &http.Response{
StatusCode: 200,
Body: r,
Expand All @@ -66,7 +65,7 @@ func TestLark(t *testing.T) {
assertError(t, err, "Error response from Lark - code [0] - msg []")

monkey.PatchInstanceMethod(reflect.TypeOf(client), "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) {
r := ioutil.NopCloser(strings.NewReader(`{"StatusCode": "100", "code": 10, "msg": "lark error"}`))
r := io.NopCloser(strings.NewReader(`{"StatusCode": "100", "code": 10, "msg": "lark error"}`))
return &http.Response{
StatusCode: 200,
Body: r,
Expand All @@ -76,7 +75,7 @@ func TestLark(t *testing.T) {
assertError(t, err, "Error response from Lark - code [10] - msg [lark error]")

monkey.PatchInstanceMethod(reflect.TypeOf(client), "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) {
r := ioutil.NopCloser(strings.NewReader(`bad : json format`))
r := io.NopCloser(strings.NewReader(`bad : json format`))
return &http.Response{
StatusCode: 200,
Body: r,
Expand All @@ -85,7 +84,7 @@ func TestLark(t *testing.T) {
err = conf.SendLark("title", "message")
assertError(t, err, "Error response from Lark [200] - [bad : json format]")

monkey.Patch(ioutil.ReadAll, func(_ io.Reader) ([]byte, error) {
monkey.Patch(io.ReadAll, func(_ io.Reader) ([]byte, error) {
return nil, errors.New("read error")
})
err = conf.SendLark("title", "message")
Expand Down
4 changes: 2 additions & 2 deletions notify/ringcentral/ringcentral.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"

"github.com/megaease/easeprobe/global"
Expand Down Expand Up @@ -92,7 +92,7 @@ func (c *NotifyConfig) SendRingCentral(title, msg string) error {

defer resp.Body.Close()

buf, err := ioutil.ReadAll(resp.Body)
buf, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
Expand Down
7 changes: 3 additions & 4 deletions notify/ringcentral/ringcentral_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package ringcentral
import (
"errors"
"io"
"io/ioutil"
"net/http"
"reflect"
"strings"
Expand All @@ -48,7 +47,7 @@ func TestRingCentral(t *testing.T) {

var client http.Client
monkey.PatchInstanceMethod(reflect.TypeOf(&client), "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) {
r := ioutil.NopCloser(strings.NewReader(`{"status":"OK"}`))
r := io.NopCloser(strings.NewReader(`{"status":"OK"}`))
return &http.Response{
StatusCode: 200,
Body: r,
Expand All @@ -58,7 +57,7 @@ func TestRingCentral(t *testing.T) {
assert.NoError(t, err)

monkey.PatchInstanceMethod(reflect.TypeOf(&client), "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) {
r := ioutil.NopCloser(strings.NewReader(`{"status": "error","message": "Your request was accepted, however a post was not generated","error": "Webhook not found!"}`))
r := io.NopCloser(strings.NewReader(`{"status": "error","message": "Your request was accepted, however a post was not generated","error": "Webhook not found!"}`))
return &http.Response{
StatusCode: 200,
Body: r,
Expand All @@ -67,7 +66,7 @@ func TestRingCentral(t *testing.T) {
err = conf.SendRingCentral("title", "message")
assertError(t, err, "Non-ok response returned from RingCentral {\"status\": \"error\",\"message\": \"Your request was accepted, however a post was not generated\",\"error\": \"Webhook not found!\"}")

monkey.Patch(ioutil.ReadAll, func(_ io.Reader) ([]byte, error) {
monkey.Patch(io.ReadAll, func(_ io.Reader) ([]byte, error) {
return nil, errors.New("read error")
})
err = conf.SendRingCentral("title", "message")
Expand Down
4 changes: 2 additions & 2 deletions notify/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package slack
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"net/http"

"github.com/megaease/easeprobe/global"
Expand Down Expand Up @@ -70,7 +70,7 @@ func (c *NotifyConfig) SendSlackNotification(msg string) error {

defer resp.Body.Close()

buf, err := ioutil.ReadAll(resp.Body)
buf, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
Expand Down
7 changes: 3 additions & 4 deletions notify/slack/slack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package slack
import (
"errors"
"io"
"io/ioutil"
"net/http"
"reflect"
"strings"
Expand All @@ -47,7 +46,7 @@ func TestSlack(t *testing.T) {

var client http.Client
monkey.PatchInstanceMethod(reflect.TypeOf(&client), "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) {
r := ioutil.NopCloser(strings.NewReader(`ok`))
r := io.NopCloser(strings.NewReader(`ok`))
return &http.Response{
StatusCode: 200,
Body: r,
Expand All @@ -57,7 +56,7 @@ func TestSlack(t *testing.T) {
assert.NoError(t, err)

monkey.PatchInstanceMethod(reflect.TypeOf(&client), "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) {
r := ioutil.NopCloser(strings.NewReader(`not found`))
r := io.NopCloser(strings.NewReader(`not found`))
return &http.Response{
StatusCode: 404,
Body: r,
Expand All @@ -66,7 +65,7 @@ func TestSlack(t *testing.T) {
err = conf.SendSlack("title", "message")
assertError(t, err, "Error response from Slack - code [404] - msg [not found]")

monkey.Patch(ioutil.ReadAll, func(_ io.Reader) ([]byte, error) {
monkey.Patch(io.ReadAll, func(_ io.Reader) ([]byte, error) {
return nil, errors.New("read error")
})
err = conf.SendSlack("title", "message")
Expand Down
4 changes: 2 additions & 2 deletions notify/sms/nexmo/nexmo.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package nexmo

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -67,7 +67,7 @@ func (c Nexmo) Notify(title, text string) error {
}
defer resp.Body.Close()

buf, err := ioutil.ReadAll(resp.Body)
buf, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 0a71fad

Please sign in to comment.