Browse Source

refactor: migrate from io/ioutil to io and os

- Replaced deprecated io/ioutil functions with their equivalents in io and os packages
- This change aligns with Go 1.16+ recommendations to avoid using io/ioutil
- Updated affected code and tests to ensure compatibility
pull/546/head
yipinhe 1 year ago
parent
commit
ef9bec0dd0
  1. 6
      client/workflow/utils.go
  2. 4
      dtmsvr/config/config.go
  3. 6
      dtmutil/utils.go
  4. 4
      test/busi/base_http.go

6
client/workflow/utils.go

@ -3,7 +3,7 @@ package workflow
import ( import (
"bytes" "bytes"
"errors" "errors"
"io/ioutil" "io"
"net/http" "net/http"
"strconv" "strconv"
@ -71,8 +71,8 @@ func newRoundTripper(old http.RoundTripper, wf *Workflow) http.RoundTripper {
// HTTPResp2DtmError check for dtm error and return it // HTTPResp2DtmError check for dtm error and return it
func HTTPResp2DtmError(resp *http.Response) ([]byte, error) { func HTTPResp2DtmError(resp *http.Response) ([]byte, error) {
code := resp.StatusCode code := resp.StatusCode
data, err := ioutil.ReadAll(resp.Body) data, err := io.ReadAll(resp.Body)
resp.Body = ioutil.NopCloser(bytes.NewBuffer(data)) resp.Body = io.NopCloser(bytes.NewBuffer(data))
if code == http.StatusTooEarly { if code == http.StatusTooEarly {
return data, dtmcli.ErrorMessage2Error(string(data), dtmcli.ErrOngoing) return data, dtmcli.ErrorMessage2Error(string(data), dtmcli.ErrOngoing)
} else if code == http.StatusConflict { } else if code == http.StatusConflict {

4
dtmsvr/config/config.go

@ -2,7 +2,7 @@ package config
import ( import (
"encoding/json" "encoding/json"
"io/ioutil" "os"
"github.com/dtm-labs/dtm/client/dtmcli" "github.com/dtm-labs/dtm/client/dtmcli"
"github.com/dtm-labs/logger" "github.com/dtm-labs/logger"
@ -113,7 +113,7 @@ var Config = Type{}
func MustLoadConfig(confFile string) { func MustLoadConfig(confFile string) {
loadFromEnv("", &Config) loadFromEnv("", &Config)
if confFile != "" { if confFile != "" {
cont, err := ioutil.ReadFile(confFile) cont, err := os.ReadFile(confFile)
logger.FatalIfError(err) logger.FatalIfError(err)
err = yaml.Unmarshal(cont, &Config) err = yaml.Unmarshal(cont, &Config)
logger.FatalIfError(err) logger.FatalIfError(err)

6
dtmutil/utils.go

@ -10,7 +10,7 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"io/ioutil" "io"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
@ -37,7 +37,7 @@ func GetGinApp() *gin.Engine {
dtmimp.E2P(err) dtmimp.E2P(err)
if len(rb) > 0 { if len(rb) > 0 {
body = string(rb) body = string(rb)
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(rb)) c.Request.Body = io.NopCloser(bytes.NewBuffer(rb))
} }
} }
logger.Debugf("begin %s %s body: %s", c.Request.Method, c.Request.URL, body) logger.Debugf("begin %s %s body: %s", c.Request.Method, c.Request.URL, body)
@ -160,7 +160,7 @@ func RunSQLScript(conf dtmcli.DBConf, script string, skipDrop bool) {
con, err := dtmimp.StandaloneDB(conf) con, err := dtmimp.StandaloneDB(conf)
logger.FatalIfError(err) logger.FatalIfError(err)
defer func() { _ = con.Close() }() defer func() { _ = con.Close() }()
content, err := ioutil.ReadFile(script) content, err := os.ReadFile(script)
logger.FatalIfError(err) logger.FatalIfError(err)
sqls := strings.Split(string(content), ";") sqls := strings.Split(string(content), ";")
for _, sql := range sqls { for _, sql := range sqls {

4
test/busi/base_http.go

@ -10,7 +10,7 @@ import (
"database/sql" "database/sql"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io"
"strings" "strings"
"github.com/dtm-labs/dtm/client/dtmcli" "github.com/dtm-labs/dtm/client/dtmcli"
@ -86,7 +86,7 @@ func RunHTTP(app *gin.Engine) {
// BaseAddRoute add base route handler // BaseAddRoute add base route handler
func BaseAddRoute(app *gin.Engine) { func BaseAddRoute(app *gin.Engine) {
app.POST(BusiAPI+"/workflow/resume", dtmutil.WrapHandler(func(ctx *gin.Context) interface{} { app.POST(BusiAPI+"/workflow/resume", dtmutil.WrapHandler(func(ctx *gin.Context) interface{} {
data, err := ioutil.ReadAll(ctx.Request.Body) data, err := io.ReadAll(ctx.Request.Body)
logger.FatalIfError(err) logger.FatalIfError(err)
return workflow.ExecuteByQS(ctx.Request.URL.Query(), data) return workflow.ExecuteByQS(ctx.Request.URL.Query(), data)
})) }))

Loading…
Cancel
Save