Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move from io/ioutil to io and os packages #346

Merged
merged 1 commit into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions cmd/client/command/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"

Expand Down Expand Up @@ -128,7 +127,7 @@ func handleRequest(httpMethod string, url string, reqBody []byte, cmd *cobra.Com
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
ExitWithErrorf("%s failed: %v", cmd.Short, err)
}
Expand Down
6 changes: 3 additions & 3 deletions doc/developer-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (c *StatusInLocalController) syncStatus() {
return
}

ioutil.WriteFile(c.spec.Path, buff, 0644)
os.WriteFile(c.spec.Path, buff, 0644)
}
```

Expand All @@ -137,7 +137,7 @@ All objects must satisfy the interface `Object` in [`pkg/object/supervisor/regis
package statusinlocalcontroller

import (
"io/ioutil"
"os"
"runtime/debug"
"time"

Expand Down Expand Up @@ -209,7 +209,7 @@ func (c *StatusInLocalController) syncStatus() {
return
}

ioutil.WriteFile(c.spec.Path, buff, 0644)
os.WriteFile(c.spec.Path, buff, 0644)
}

// Category returns the category of StatusInLocalController.
Expand Down
4 changes: 2 additions & 2 deletions example/backend-service/remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"io"
"net/http"
)

Expand Down Expand Up @@ -44,7 +44,7 @@ func main() {

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package api

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"sort"

Expand Down Expand Up @@ -86,7 +86,7 @@ func (s *Server) objectAPIEntries() []*Entry {
}

func (s *Server) readObjectSpec(w http.ResponseWriter, r *http.Request) (*supervisor.Spec, error) {
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
return nil, fmt.Errorf("read body failed: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ package api

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"time"

Expand Down Expand Up @@ -117,7 +117,7 @@ func (s *Server) wasmApplyData(w http.ResponseWriter, r *http.Request) {
return
}

body, e := ioutil.ReadAll(r.Body)
body, e := io.ReadAll(r.Body)
if e != nil {
HandleAPIError(w, r, http.StatusBadRequest, e)
return
Expand Down
5 changes: 2 additions & 3 deletions pkg/cluster/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package cluster
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -110,7 +109,7 @@ func (m *members) load() error {
return nil
}

buff, err := ioutil.ReadFile(m.file)
buff, err := os.ReadFile(m.file)
if err != nil {
return err
}
Expand Down Expand Up @@ -146,7 +145,7 @@ func (m *members) store() {
}
}

err = ioutil.WriteFile(m.file, buff, 0o644)
err = os.WriteFile(m.file, buff, 0o644)
if err != nil {
logger.Errorf("write file %s failed: %v", m.file, err)
} else {
Expand Down
2 changes: 1 addition & 1 deletion pkg/context/httprequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (r *httpRequest) Size() uint64 {

func (r *httpRequest) finish() {
// NOTE: We don't use this line in case of large flow attack.
// io.Copy(ioutil.Discard, r.std.Body)
// io.Copy(io.Discard, r.std.Body)

// NOTE: The server will do it for us.
// r.std.Body.Close()
Expand Down
3 changes: 1 addition & 2 deletions pkg/filter/proxy/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package proxy
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"strconv"

Expand Down Expand Up @@ -198,7 +197,7 @@ func (p *pool) handle(ctx context.HTTPContext, reqBody io.Reader, client *http.C
// And we do NOT do statistics of duration and respSize
// for it, because we can't wait for it to finish.
defer resp.Body.Close()
io.Copy(ioutil.Discard, resp.Body)
io.Copy(io.Discard, resp.Body)
}()

return ""
Expand Down
4 changes: 2 additions & 2 deletions pkg/filter/retryer/retryer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package retryer
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"math/rand"
"strings"
"time"
Expand Down Expand Up @@ -176,7 +176,7 @@ func (r *Retryer) handle(ctx context.HTTPContext, u *URLRule) string {
attempt := 0
base := float64(u.policy.waitDuration)

data, _ := ioutil.ReadAll(ctx.Request().Body())
data, _ := io.ReadAll(ctx.Request().Body())
for {
attempt++
ctx.Request().SetBody(bytes.NewReader(data))
Expand Down
4 changes: 2 additions & 2 deletions pkg/object/function/worker/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package worker

import (
"fmt"
"io/ioutil"
"io"
"net/http"

"github.com/go-chi/chi/v5"
Expand Down Expand Up @@ -120,7 +120,7 @@ func (worker *Worker) Create(w http.ResponseWriter, r *http.Request) {
}

func (worker *Worker) readAPISpec(w http.ResponseWriter, r *http.Request, spec interface{}) error {
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
return fmt.Errorf("read body failed: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/object/meshcontroller/worker/api_consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package worker
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"

"github.com/go-chi/chi/v5"
Expand Down Expand Up @@ -73,7 +73,7 @@ func (worker *Worker) consulAPIs() []*apiEntry {
}

func (worker *Worker) consulRegister(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
api.HandleAPIError(w, r, http.StatusBadRequest,
fmt.Errorf("read body failed: %v", err))
Expand Down
4 changes: 2 additions & 2 deletions pkg/object/meshcontroller/worker/api_eureka.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"encoding/json"
"encoding/xml"
"fmt"
"io/ioutil"
"io"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -139,7 +139,7 @@ func (worker *Worker) detectedAccept(accept string) string {
}

func (worker *Worker) eurekaRegister(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
api.HandleAPIError(w, r, http.StatusBadRequest,
fmt.Errorf("read body failed: %v", err))
Expand Down
5 changes: 2 additions & 3 deletions pkg/pidfile/pidfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package pidfile

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
Expand All @@ -38,7 +37,7 @@ var pidfilePath string
func Write(opt *option.Options) error {
pidfilePath = filepath.Join(opt.AbsHomeDir, pidfileName)

err := ioutil.WriteFile(pidfilePath, []byte(fmt.Sprintf("%d", os.Getpid())), 0o644)
err := os.WriteFile(pidfilePath, []byte(fmt.Sprintf("%d", os.Getpid())), 0o644)
if err != nil {
logger.Errorf("write %s failed: %s", pidfilePath, err)
return err
Expand All @@ -51,7 +50,7 @@ func Write(opt *option.Options) error {
func Read(opt *option.Options) (int, error) {
pidfilePath = filepath.Join(opt.AbsHomeDir, pidfileName)

data, err := ioutil.ReadFile(pidfilePath)
data, err := os.ReadFile(pidfilePath)
if err != nil {
logger.Errorf("read %s failed: %s", pidfilePath, err)
return 0, err
Expand Down
4 changes: 2 additions & 2 deletions pkg/supervisor/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package supervisor
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"runtime/debug"
Expand Down Expand Up @@ -300,7 +300,7 @@ func (or *ObjectRegistry) storeConfigInLocal(config map[string]string) {
}
buff.Write(configBuff)

err = ioutil.WriteFile(or.configLocalPath, buff.Bytes(), 0o644)
err = os.WriteFile(or.configLocalPath, buff.Bytes(), 0o644)
if err != nil {
logger.Errorf("write %s failed: %v", or.configLocalPath, err)
return
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/jmxtool/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -107,7 +107,7 @@ func handleRequest(httpMethod string, url string, reqBody []byte) ([]byte, error
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/util/signer/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/textproto"
"net/url"
Expand Down Expand Up @@ -507,12 +507,12 @@ func (ctx *SigningContext) hashBody(req *http.Request, verify bool) error {
// sha256 of empty string
ctx.BodyHash = sha256Empty
} else {
body, e := ioutil.ReadAll(req.Body)
body, e := io.ReadAll(req.Body)
if e != nil {
return e
}
req.Body.Close()
req.Body = ioutil.NopCloser(bytes.NewReader(body))
req.Body = io.NopCloser(bytes.NewReader(body))
ctx.BodyHash = sha256DegistAndEncodeToHexString(body)
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/util/signer/signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package signer

import (
"io"
"io/ioutil"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -315,7 +314,7 @@ func TestPresignVerify(t *testing.T) {
}

signer.SetTTL(0)
req.Body = ioutil.NopCloser(strings.NewReader("aaaaa"))
req.Body = io.NopCloser(strings.NewReader("aaaaa"))
if e := signer.Verify(req); e == nil {
t.Errorf("verification should failed, but didn't")
}
Expand Down