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

style: Cancel the io/outil libraries used in the project and switch to using native methods of os and io libraries #1203

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions cmd/hz/generator/custom_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package generator
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
Expand Down Expand Up @@ -315,7 +315,7 @@ func (pkgGen *HttpPackageGenerator) genLoopService(tplInfo *Template, filePathRe
return err
}
case Append: // todo: append logic need to be optimized for method
fileContent, err := ioutil.ReadFile(filePath)
fileContent, err := os.ReadFile(filePath)
if err != nil {
return err
}
Expand Down Expand Up @@ -492,7 +492,7 @@ func (pkgGen *HttpPackageGenerator) genSingleCustomizedFile(tplInfo *Template, f
return err
}
case Append: // todo: append logic need to be optimized for single file
fileContent, err := ioutil.ReadFile(filePath)
fileContent, err := os.ReadFile(filePath)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/hz/generator/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package generator
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -196,7 +196,7 @@ func (pkgGen *HttpPackageGenerator) updateHandler(handler interface{}, handlerTp
return nil
}

file, err := ioutil.ReadFile(filePath)
file, err := os.ReadFile(filePath)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/hz/generator/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strings"
Expand Down Expand Up @@ -63,7 +63,7 @@ func (lg *LayoutGenerator) Init() error {
config := layoutConfig
// unmarshal from user-defined config file if it exists
if lg.ConfigPath != "" {
cdata, err := ioutil.ReadFile(lg.ConfigPath)
cdata, err := os.ReadFile(lg.ConfigPath)
if err != nil {
return fmt.Errorf("read layout config from %s failed, err: %v", lg.ConfigPath, err.Error())
}
Expand Down Expand Up @@ -216,7 +216,7 @@ func (lg *LayoutGenerator) GenerateByConfig(configPath string) error {
if err := lg.checkInited(); err != nil {
return err
}
buf, err := ioutil.ReadFile(configPath)
buf, err := os.ReadFile(configPath)
if err != nil {
return fmt.Errorf("read data file '%s' failed, err: %v", configPath, err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/hz/generator/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package generator
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"text/template"
Expand Down Expand Up @@ -84,7 +84,7 @@ func (pkgGen *HttpPackageGenerator) Init() error {
customConfig := TemplateConfig{}
// unmarshal from user-defined config file if it exists
if pkgGen.ConfigPath != "" {
cdata, err := ioutil.ReadFile(pkgGen.ConfigPath)
cdata, err := os.ReadFile(pkgGen.ConfigPath)
if err != nil {
return fmt.Errorf("read layout config from %s failed, err: %v", pkgGen.ConfigPath, err.Error())
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/hz/generator/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ package generator
import (
"bytes"
"fmt"
"io/ioutil"
"math"
"os"
"path/filepath"
"regexp"
"sort"
Expand Down Expand Up @@ -345,7 +345,7 @@ func (pkgGen *HttpPackageGenerator) updateRegister(pkg, rDir, pkgName string) er
return pkgGen.TemplateGenerator.Generate(register, registerTplName, registerPath, false)
}

file, err := ioutil.ReadFile(registerPath)
file, err := os.ReadFile(registerPath)
if err != nil {
return fmt.Errorf("read register '%s' failed, err: %v", registerPath, err.Error())
}
Expand Down Expand Up @@ -493,7 +493,7 @@ func (pkgGen *HttpPackageGenerator) updateMiddlewareReg(router interface{}, midd
return nil
})

file, err := ioutil.ReadFile(filePath)
file, err := os.ReadFile(filePath)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/hz/protobuf/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ package protobuf

import (
"fmt"
"io/ioutil"
"io"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -112,7 +112,7 @@ func (plugin *Plugin) Run() int {
}
}()
// read protoc request
in, err := ioutil.ReadAll(os.Stdin)
in, err := io.ReadAll(os.Stdin)
if err != nil {
logs.Errorf("read request failed: %s\n", err.Error())
return meta.PluginError
Expand Down
4 changes: 2 additions & 2 deletions cmd/hz/protobuf/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package protobuf

import (
"io/ioutil"
"os"
"strings"
"testing"

Expand All @@ -27,7 +27,7 @@ import (
)

func TestPlugin_Handle(t *testing.T) {
in, err := ioutil.ReadFile("../testdata/request_protoc.out")
in, err := os.ReadFile("../testdata/request_protoc.out")
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/hz/protobuf/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package protobuf

import (
"io/ioutil"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -104,7 +104,7 @@ func TestTagGenerate(t *testing.T) {
},
}

in, err := ioutil.ReadFile("./test_data/protobuf_tag_test.out")
in, err := os.ReadFile("./test_data/protobuf_tag_test.out")
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/hz/thrift/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package thrift
import (
"errors"
"fmt"
"io/ioutil"
"io"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -221,7 +221,7 @@ func (plugin *Plugin) recvVerboseLogger() string {
}

func (plugin *Plugin) handleRequest() error {
data, err := ioutil.ReadAll(os.Stdin)
data, err := io.ReadAll(os.Stdin)
if err != nil {
return fmt.Errorf("read request failed: %s", err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/hz/thrift/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package thrift

import (
"io/ioutil"
"os"
"testing"

"github.com/cloudwego/hertz/cmd/hz/generator"
Expand All @@ -26,7 +26,7 @@ import (
)

func TestRun(t *testing.T) {
data, err := ioutil.ReadFile("../testdata/request_thrift.out")
data, err := os.ReadFile("../testdata/request_thrift.out")
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/hz/thrift/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package thrift

import (
"io/ioutil"
"os"
"strings"
"testing"

Expand All @@ -26,7 +26,7 @@ import (
)

func TestInsertTag(t *testing.T) {
data, err := ioutil.ReadFile("./test_data/thrift_tag_test.out")
data, err := os.ReadFile("./test_data/thrift_tag_test.out")
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/hz/util/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"bytes"
"fmt"
"go/build"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -85,7 +84,7 @@ var goModReg = regexp.MustCompile(`^\s*module\s+(\S+)\s*`)
func SearchGoMod(cwd string, recurse bool) (moduleName, path string, found bool) {
for {
path = filepath.Join(cwd, "go.mod")
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err == nil {
for _, line := range strings.Split(string(data), "\n") {
m := goModReg.FindStringSubmatch(line)
Expand Down
4 changes: 2 additions & 2 deletions internal/bytesconv/bytesconv_table_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
)

const (
Expand Down Expand Up @@ -251,7 +251,7 @@ func main() {
fmt.Fprintf(w, "\tValidHeaderFieldNameTable = %q\n", validHeaderFieldNameTable)
fmt.Fprintf(w, ")\n")

if err := ioutil.WriteFile("bytesconv_table.go", w.Bytes(), 0o660); err != nil {
if err := os.WriteFile("bytesconv_table.go", w.Bytes(), 0o660); err != nil {
log.Fatal(err)
}
}
Expand Down
7 changes: 3 additions & 4 deletions pkg/app/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -1625,7 +1624,7 @@ func TestClientReadResponseBodyStream(t *testing.T) {
if string(p) != part1 {
t.Errorf("read len=%v, read content=%v; want len=%v, want content=%v", r, string(p), len(part1), part1)
}
left, _ := ioutil.ReadAll(bodyStream)
left, _ := io.ReadAll(bodyStream)
if string(left) != part2 {
t.Errorf("left len=%v, left content=%v; want len=%v, want content=%v", len(left), string(left), len(part2), part2)
}
Expand Down Expand Up @@ -2067,13 +2066,13 @@ func TestClientReadResponseBodyStreamWithDoubleRequest(t *testing.T) {
if bodyStream1 == nil {
t.Errorf("bodystream1 is nil")
}
data, _ := ioutil.ReadAll(bodyStream1)
data, _ := io.ReadAll(bodyStream1)
if string(data) != part1+part2 {
t.Errorf("read len=%v, read content=%v; want len=%v, want content=%v", len(data), data, len(part1+part2), part1+part2)
}

// read left bodystream
left, _ := ioutil.ReadAll(bodyStream)
left, _ := io.ReadAll(bodyStream)
if string(left) != part2 {
t.Errorf("left len=%v, left content=%v; want len=%v, want content=%v", len(left), string(left), len(part2), part2)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/app/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"errors"
"fmt"
"html/template"
"io/ioutil"
"io"
"net"
"os"
"reflect"
Expand Down Expand Up @@ -462,7 +462,7 @@ tailfoobar`
if err := req.Read(&ctx.Request, mr); err != nil {
t.Fatalf("unexpected error: %s", err)
}
tail, err := ioutil.ReadAll(mr)
tail, err := io.ReadAll(mr)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
Expand Down Expand Up @@ -988,7 +988,7 @@ func TestRequestBodyStream(t *testing.T) {
s := "testRequestBodyStream"
mr := bytes.NewBufferString(s)
c.Request.SetBodyStream(mr, -1)
data, err := ioutil.ReadAll(c.RequestBodyStream())
data, err := io.ReadAll(c.RequestBodyStream())
assert.Nil(t, err)
assert.DeepEqual(t, "testRequestBodyStream", string(data))
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/app/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import (
"fmt"
"html"
"io"
"io/ioutil"
"mime"
"net/http"
"os"
Expand Down Expand Up @@ -1075,7 +1074,7 @@ func readFileHeader(f *os.File, compressed bool) ([]byte, error) {
R: r,
N: 512,
}
data, err := ioutil.ReadAll(lr)
data, err := io.ReadAll(lr)
if _, err := f.Seek(0, 0); err != nil {
return nil, err
}
Expand Down
7 changes: 3 additions & 4 deletions pkg/app/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"path"
Expand Down Expand Up @@ -186,13 +185,13 @@ func TestServeFileSmallNoReadFrom(t *testing.T) {

teststr := "hello, world!"

tempdir, err := ioutil.TempDir("", "httpexpect")
tempdir, err := os.MkdirTemp("", "httpexpect")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempdir)

if err := ioutil.WriteFile(
if err := os.WriteFile(
path.Join(tempdir, "hello"), []byte(teststr), 0o666); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -426,7 +425,7 @@ func getFileContents(path string) ([]byte, error) {
return nil, err
}
defer f.Close()
return ioutil.ReadAll(f)
return io.ReadAll(f)
}

func TestParseByteRangeSuccess(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/app/middlewares/server/recovery/recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"runtime"

"github.com/cloudwego/hertz/pkg/app"
Expand Down Expand Up @@ -66,7 +66,7 @@ func stack(skip int) []byte {
// Print this much at least. If we can't find the source, it won't show.
fmt.Fprintf(buf, "%s:%d (0x%x)\n", file, line, pc)
if file != lastFile {
data, err := ioutil.ReadFile(file)
data, err := os.ReadFile(file)
if err != nil {
continue
}
Expand Down
Loading
Loading