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 <guoguangwu@magic-shield.com>
  • Loading branch information
testwill authored and rclsilver committed Oct 18, 2023
1 parent 6e2c062 commit 9507ca4
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 29 deletions.
3 changes: 1 addition & 2 deletions api/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"reflect"

Expand All @@ -30,7 +29,7 @@ type yamlBinding struct{}

func (yamlBinding) Name() string { return "yamlBinding" }
func (yamlBinding) Bind(req *http.Request, obj interface{}) error {
bodyBytes, err := ioutil.ReadAll(req.Body)
bodyBytes, err := io.ReadAll(req.Body)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sync"
Expand Down Expand Up @@ -99,14 +98,15 @@ func TestMain(m *testing.M) {

func loadTemplates() map[string][]byte {
templateList := map[string][]byte{}
files, err := ioutil.ReadDir(testDirTemplates)
files, err := os.ReadDir(testDirTemplates)
if err != nil {
panic(err)
}

for _, file := range files {
if file.Mode().IsRegular() {
bytes, err := ioutil.ReadFile(filepath.Join(testDirTemplates, file.Name()))
info, _ := file.Info()
if info.Mode().IsRegular() {
bytes, err := os.ReadFile(filepath.Join(testDirTemplates, file.Name()))
if err != nil {
panic(err)
}
Expand Down
6 changes: 3 additions & 3 deletions engine/functions/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"reflect"
"regexp"
Expand Down Expand Up @@ -149,7 +149,7 @@ func (f *Function) MetadataSchema() json.RawMessage {

// LoadFromDir loads recursively all the function from a given directory.
func LoadFromDir(directory string) error {
files, err := ioutil.ReadDir(directory)
files, err := os.ReadDir(directory)
if err != nil {
logrus.Warnf("Ignoring functions directory %s: %s", directory, err)
return nil
Expand All @@ -167,7 +167,7 @@ func LoadFromDir(directory string) error {
continue
}

content, err := ioutil.ReadFile(path.Join(directory, file.Name()))
content, err := os.ReadFile(path.Join(directory, file.Name()))
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions models/tasktemplate/fromdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package tasktemplate

import (
"fmt"
"io/ioutil"
"os"
"path"
"strings"

Expand All @@ -20,15 +20,15 @@ var (
// LoadFromDir reads yaml-formatted task templates
// from a folder and upserts them in database
func LoadFromDir(dbp zesty.DBProvider, dir string) error {
files, err := ioutil.ReadDir(dir)
files, err := os.ReadDir(dir)
if err != nil {
return fmt.Errorf("failed to open template directory %s: %s", dir, err)
}
for _, file := range files {
if file.IsDir() || !strings.HasSuffix(file.Name(), ".yaml") {
continue
}
tmpl, err := ioutil.ReadFile(path.Join(dir, file.Name()))
tmpl, err := os.ReadFile(path.Join(dir, file.Name()))
if err != nil {
return fmt.Errorf("failed to read template '%s': %s", file.Name(), err)
}
Expand Down
7 changes: 3 additions & 4 deletions models/tasktemplate/fromdir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package tasktemplate_test

import (
"fmt"
"io/ioutil"
"os"
"path"
"testing"
Expand Down Expand Up @@ -69,7 +68,7 @@ func TestLoadFromDir(t *testing.T) {
assert.Len(t, taskTemplatesFromDatabase, 2, "wrong size of imported templates")

tt := tasktemplate.TaskTemplate{}
tmpl, err := ioutil.ReadFile(path.Join("templates_tests", "hello-world-now.yaml"))
tmpl, err := os.ReadFile(path.Join("templates_tests", "hello-world-now.yaml"))
assert.Nil(t, err, "unable to read file hello-world-now.yaml")
err = yaml.Unmarshal(tmpl, &tt)
assert.Nil(t, err, "unable to unmarshal tasktemplate")
Expand Down Expand Up @@ -118,7 +117,7 @@ func TestLoadFromDir(t *testing.T) {

func TestInvalidVariablesTemplates(t *testing.T) {
tt := tasktemplate.TaskTemplate{}
tmpl, err := ioutil.ReadFile(path.Join("templates_errors_tests", "error-variables.yaml"))
tmpl, err := os.ReadFile(path.Join("templates_errors_tests", "error-variables.yaml"))
assert.Nil(t, err, "unable to read file error-variables.yaml")
err = yaml.Unmarshal(tmpl, &tt)
assert.Nil(t, err, "unable to unmarshal tasktemplate")
Expand Down Expand Up @@ -158,7 +157,7 @@ func TestInvalidVariablesTemplates(t *testing.T) {

func TestDependenciesValidation(t *testing.T) {
tt := tasktemplate.TaskTemplate{}
tmpl, err := ioutil.ReadFile(path.Join("templates_errors_tests", "error-variables.yaml"))
tmpl, err := os.ReadFile(path.Join("templates_errors_tests", "error-variables.yaml"))
assert.Nil(t, err, "unable to read file error-variables.yaml")
err = yaml.Unmarshal(tmpl, &tt)
assert.Nil(t, err, "unable to unmarshal tasktemplate")
Expand Down
4 changes: 2 additions & 2 deletions pkg/notify/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"time"

Expand Down Expand Up @@ -79,7 +79,7 @@ func (w *NotificationSender) Send(m *notify.Message, name string) {
if res.StatusCode >= 400 {
resErr := fmt.Errorf("failed to send notification using %q: backend returned with status code %d", name, res.StatusCode)

resBody, err := ioutil.ReadAll(res.Body)
resBody, err := io.ReadAll(res.Body)
if err == nil {
notify.WrappedSendErrorWithBody(resErr, m, Type, name, string(resBody))
} else {
Expand Down
6 changes: 3 additions & 3 deletions pkg/plugins/builtin/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"encoding/xml"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"strconv"
Expand Down Expand Up @@ -301,13 +301,13 @@ func exec(stepName string, config interface{}, ctx interface{}) (interface{}, in
// remove response magic prefix
if cfg.TrimPrefix != "" {
trimPrefixBytes := []byte(cfg.TrimPrefix)
respBody, err := ioutil.ReadAll(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, nil, fmt.Errorf("HTTP cannot read response: %s", err.Error())
}
resp.Body.Close()
respBody = bytes.TrimPrefix(respBody, trimPrefixBytes)
resp.Body = ioutil.NopCloser(bytes.NewReader(respBody))
resp.Body = io.NopCloser(bytes.NewReader(respBody))
}

return httputil.UnmarshalResponse(resp)
Expand Down
4 changes: 2 additions & 2 deletions pkg/plugins/builtin/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package pluginhttp
import (
"bytes"
"encoding/json"
"io/ioutil"
"io"
"net/http"
"net/http/httputil"
"testing"
Expand Down Expand Up @@ -158,7 +158,7 @@ func Test_exec(t *testing.T) {
var httpResponse = new(http.Response)
httpResponse.Header = http.Header{"Set-Cookie": {"Cookie-1=foo"}, "Content-Type": {"application/json"}}
var bodyResponse = []byte(`{"foo": "bar"}`)
httpResponse.Body = ioutil.NopCloser(bytes.NewBuffer(bodyResponse))
httpResponse.Body = io.NopCloser(bytes.NewBuffer(bodyResponse))
httpResponse.StatusCode = 200
return httpResponse, nil
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/plugins/builtin/httputil/httputil.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"io"
"net/http"
"strings"
"time"
Expand Down Expand Up @@ -36,7 +36,7 @@ func UnmarshalResponse(resp *http.Response) (interface{}, interface{}, error) {

defer resp.Body.Close()

bodyBytes, err := ioutil.ReadAll(resp.Body)
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, nil, fmt.Errorf("can't read body: %s", err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/plugins/builtin/httputil/httputil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package httputil

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

Expand All @@ -14,7 +14,7 @@ func TestUnmarshalResponse(t *testing.T) {
var httpResponse = new(http.Response)
httpResponse.Header = http.Header{"Set-Cookie": {"Cookie-1=foo"}, "Content-Type": {"application/json"}}
var bodyResponse = []byte(`{"foo": "bar"}`)
httpResponse.Body = ioutil.NopCloser(bytes.NewBuffer(bodyResponse))
httpResponse.Body = io.NopCloser(bytes.NewBuffer(bodyResponse))
httpResponse.StatusCode = 200

output, metadata, err := UnmarshalResponse(httpResponse)
Expand Down
4 changes: 2 additions & 2 deletions pkg/plugins/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package plugins

import (
"fmt"
"io/ioutil"
"os"
"plugin"
"reflect"
"strings"
Expand Down Expand Up @@ -71,7 +71,7 @@ func InitializersFromFolder(path string, service *Service) error {
}

func loadPlugins(path string, load func(string, plugin.Symbol) error) error {
files, err := ioutil.ReadDir(path)
files, err := os.ReadDir(path)
if err != nil {
logrus.Warnf("Ignoring plugin directory %s: %s", path, err)
} else {
Expand Down

0 comments on commit 9507ca4

Please sign in to comment.