From 9507ca429a9169ee076e3c4218b4859945e0cda2 Mon Sep 17 00:00:00 2001 From: guoguangwu Date: Mon, 24 Jul 2023 14:29:49 +0800 Subject: [PATCH] chore: remove refs to deprecated io/ioutil Signed-off-by: guoguangwu --- api/bind.go | 3 +-- engine/engine_test.go | 8 ++++---- engine/functions/functions.go | 6 +++--- models/tasktemplate/fromdir.go | 6 +++--- models/tasktemplate/fromdir_test.go | 7 +++---- pkg/notify/webhook/webhook.go | 4 ++-- pkg/plugins/builtin/http/http.go | 6 +++--- pkg/plugins/builtin/http/http_test.go | 4 ++-- pkg/plugins/builtin/httputil/httputil.go | 4 ++-- pkg/plugins/builtin/httputil/httputil_test.go | 4 ++-- pkg/plugins/plugins.go | 4 ++-- 11 files changed, 27 insertions(+), 29 deletions(-) diff --git a/api/bind.go b/api/bind.go index fba91169..63c29b9f 100644 --- a/api/bind.go +++ b/api/bind.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net/http" "reflect" @@ -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 } diff --git a/engine/engine_test.go b/engine/engine_test.go index 45d137f7..4f8eeb7c 100644 --- a/engine/engine_test.go +++ b/engine/engine_test.go @@ -5,7 +5,6 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" "os" "path/filepath" "sync" @@ -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) } diff --git a/engine/functions/functions.go b/engine/functions/functions.go index 95779ad8..da761882 100644 --- a/engine/functions/functions.go +++ b/engine/functions/functions.go @@ -4,7 +4,7 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "os" "path" "reflect" "regexp" @@ -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 @@ -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 } diff --git a/models/tasktemplate/fromdir.go b/models/tasktemplate/fromdir.go index b9fa7a72..0fbe8a74 100644 --- a/models/tasktemplate/fromdir.go +++ b/models/tasktemplate/fromdir.go @@ -2,7 +2,7 @@ package tasktemplate import ( "fmt" - "io/ioutil" + "os" "path" "strings" @@ -20,7 +20,7 @@ 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) } @@ -28,7 +28,7 @@ func LoadFromDir(dbp zesty.DBProvider, dir string) error { 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) } diff --git a/models/tasktemplate/fromdir_test.go b/models/tasktemplate/fromdir_test.go index 09fac0d2..b5d42cc0 100644 --- a/models/tasktemplate/fromdir_test.go +++ b/models/tasktemplate/fromdir_test.go @@ -2,7 +2,6 @@ package tasktemplate_test import ( "fmt" - "io/ioutil" "os" "path" "testing" @@ -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") @@ -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") @@ -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") diff --git a/pkg/notify/webhook/webhook.go b/pkg/notify/webhook/webhook.go index 2dba1723..84c73de5 100644 --- a/pkg/notify/webhook/webhook.go +++ b/pkg/notify/webhook/webhook.go @@ -4,7 +4,7 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "time" @@ -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 { diff --git a/pkg/plugins/builtin/http/http.go b/pkg/plugins/builtin/http/http.go index f8d5c596..005263fc 100644 --- a/pkg/plugins/builtin/http/http.go +++ b/pkg/plugins/builtin/http/http.go @@ -6,7 +6,7 @@ import ( "encoding/xml" "errors" "fmt" - "io/ioutil" + "io" "net/http" "net/url" "strconv" @@ -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) diff --git a/pkg/plugins/builtin/http/http_test.go b/pkg/plugins/builtin/http/http_test.go index 0990fb5b..eafbeb96 100644 --- a/pkg/plugins/builtin/http/http_test.go +++ b/pkg/plugins/builtin/http/http_test.go @@ -3,7 +3,7 @@ package pluginhttp import ( "bytes" "encoding/json" - "io/ioutil" + "io" "net/http" "net/http/httputil" "testing" @@ -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 }, diff --git a/pkg/plugins/builtin/httputil/httputil.go b/pkg/plugins/builtin/httputil/httputil.go index 8d623bcd..3ff2b920 100644 --- a/pkg/plugins/builtin/httputil/httputil.go +++ b/pkg/plugins/builtin/httputil/httputil.go @@ -5,7 +5,7 @@ import ( "crypto/tls" "crypto/x509" "fmt" - "io/ioutil" + "io" "net/http" "strings" "time" @@ -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()) } diff --git a/pkg/plugins/builtin/httputil/httputil_test.go b/pkg/plugins/builtin/httputil/httputil_test.go index db995345..6a61acfc 100644 --- a/pkg/plugins/builtin/httputil/httputil_test.go +++ b/pkg/plugins/builtin/httputil/httputil_test.go @@ -2,7 +2,7 @@ package httputil import ( "bytes" - "io/ioutil" + "io" "net/http" "testing" @@ -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) diff --git a/pkg/plugins/plugins.go b/pkg/plugins/plugins.go index 16e72e06..c6ca33ad 100644 --- a/pkg/plugins/plugins.go +++ b/pkg/plugins/plugins.go @@ -2,7 +2,7 @@ package plugins import ( "fmt" - "io/ioutil" + "os" "plugin" "reflect" "strings" @@ -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 {