Skip to content

Commit

Permalink
Merge pull request #1 from go-kratos/main
Browse files Browse the repository at this point in the history
refactor: move from io/ioutil to io and os packages (go-kratos#1633)
  • Loading branch information
kiripeng214 authored Nov 16, 2021
2 parents 7a52468 + 77b1628 commit 546c922
Show file tree
Hide file tree
Showing 23 changed files with 55 additions and 65 deletions.
4 changes: 2 additions & 2 deletions api/metadata/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"compress/gzip"
"context"
"fmt"
"io/ioutil"
"io"
"sync"

"google.golang.org/grpc"
Expand Down Expand Up @@ -180,7 +180,7 @@ func decompress(b []byte) ([]byte, error) {
if err != nil {
return nil, fmt.Errorf("bad gzipped descriptor: %v", err)
}
out, err := ioutil.ReadAll(r)
out, err := io.ReadAll(r)
if err != nil {
return nil, fmt.Errorf("bad gzipped descriptor: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/kratos/internal/base/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package base
import (
"bufio"
"bytes"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
Expand All @@ -13,7 +13,7 @@ import (

// ModulePath returns go module path.
func ModulePath(filename string) (string, error) {
modBytes, err := ioutil.ReadFile(filename)
modBytes, err := os.ReadFile(filename)
if err != nil {
return "", err
}
Expand Down
9 changes: 4 additions & 5 deletions cmd/kratos/internal/base/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package base
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"path"
Expand Down Expand Up @@ -43,7 +42,7 @@ func copyFile(src, dst string, replaces []string) error {
if err != nil {
return err
}
buf, err := ioutil.ReadFile(src)
buf, err := os.ReadFile(src)
if err != nil {
return err
}
Expand All @@ -55,12 +54,12 @@ func copyFile(src, dst string, replaces []string) error {
}
buf = bytes.ReplaceAll(buf, []byte(old), []byte(next))
}
return ioutil.WriteFile(dst, buf, srcinfo.Mode())
return os.WriteFile(dst, buf, srcinfo.Mode())
}

func copyDir(src, dst string, replaces, ignores []string) error {
var err error
var fds []os.FileInfo
var fds []os.DirEntry
var srcinfo os.FileInfo

if srcinfo, err = os.Stat(src); err != nil {
Expand All @@ -71,7 +70,7 @@ func copyDir(src, dst string, replaces, ignores []string) error {
return err
}

if fds, err = ioutil.ReadDir(src); err != nil {
if fds, err = os.ReadDir(src); err != nil {
return err
}
for _, fd := range fds {
Expand Down
3 changes: 1 addition & 2 deletions cmd/kratos/internal/change/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"regexp"
Expand Down Expand Up @@ -104,7 +103,7 @@ func requestGithubAPI(url string, method string, body io.Reader, token string) (
fatal(err)
}
defer resp.Body.Close()
resBody, err := ioutil.ReadAll(resp.Body)
resBody, err := io.ReadAll(resp.Body)
if err != nil {
fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/kratos/internal/proto/add/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package add

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

"github.com/spf13/cobra"
Expand Down Expand Up @@ -44,9 +44,9 @@ func run(cmd *cobra.Command, args []string) {
}

func modName() string {
modBytes, err := ioutil.ReadFile("go.mod")
modBytes, err := os.ReadFile("go.mod")
if err != nil {
if modBytes, err = ioutil.ReadFile("../go.mod"); err != nil {
if modBytes, err = os.ReadFile("../go.mod"); err != nil {
return ""
}
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/kratos/internal/proto/add/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package add

import (
"fmt"
"io/ioutil"
"os"
"path"
)
Expand Down Expand Up @@ -37,5 +36,5 @@ func (p *Proto) Generate() error {
if _, err := os.Stat(name); !os.IsNotExist(err) {
return fmt.Errorf("%s already exists", p.Name)
}
return ioutil.WriteFile(name, body, 0o644)
return os.WriteFile(name, body, 0o644)
}
3 changes: 1 addition & 2 deletions cmd/kratos/internal/proto/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package client

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -98,7 +97,7 @@ func generate(proto string, args []string) error {
"--go-errors_out=paths=source_relative:.",
}
input = append(input, inputExt...)
protoBytes, err := ioutil.ReadFile(proto)
protoBytes, err := os.ReadFile(proto)
if err == nil && len(protoBytes) > 0 {
if ok, _ := regexp.Match(`\n[^/]*(import)\s+"validate/validate.proto"`, protoBytes); ok {
input = append(input, "--validate_out=lang=go,paths=source_relative:.")
Expand Down
3 changes: 1 addition & 2 deletions cmd/kratos/internal/proto/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package server

import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
Expand Down Expand Up @@ -83,7 +82,7 @@ func run(cmd *cobra.Command, args []string) {
if err != nil {
log.Fatal(err)
}
if err := ioutil.WriteFile(to, b, 0o644); err != nil {
if err := os.WriteFile(to, b, 0o644); err != nil {
log.Fatal(err)
}
fmt.Println(to)
Expand Down
3 changes: 1 addition & 2 deletions cmd/kratos/internal/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package run

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
Expand Down Expand Up @@ -79,7 +78,7 @@ func findCMD(base string) (map[string]string, error) {
err := filepath.Walk(dir, func(walkPath string, info os.FileInfo, err error) error {
// multi level directory is not allowed under the cmdPath directory, so it is judged that the path ends with cmdPath.
if strings.HasSuffix(walkPath, "cmd") {
paths, err := ioutil.ReadDir(walkPath)
paths, err := os.ReadDir(walkPath)
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions config/env/env_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package env

import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -39,7 +38,7 @@ func TestEnvWithPrefix(t *testing.T) {
if err := os.MkdirAll(path, 0o700); err != nil {
t.Error(err)
}
if err := ioutil.WriteFile(filename, data, 0o666); err != nil {
if err := os.WriteFile(filename, data, 0o666); err != nil {
t.Error(err)
}

Expand Down Expand Up @@ -149,7 +148,7 @@ func TestEnvWithoutPrefix(t *testing.T) {
if err := os.MkdirAll(path, 0o700); err != nil {
t.Error(err)
}
if err := ioutil.WriteFile(filename, data, 0o666); err != nil {
if err := os.WriteFile(filename, data, 0o666); err != nil {
t.Error(err)
}

Expand Down
6 changes: 3 additions & 3 deletions config/file/file.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package file

import (
"io/ioutil"
"io"
"os"
"path/filepath"
"strings"
Expand All @@ -26,7 +26,7 @@ func (f *file) loadFile(path string) (*config.KeyValue, error) {
return nil, err
}
defer file.Close()
data, err := ioutil.ReadAll(file)
data, err := io.ReadAll(file)
if err != nil {
return nil, err
}
Expand All @@ -42,7 +42,7 @@ func (f *file) loadFile(path string) (*config.KeyValue, error) {
}

func (f *file) loadDir(path string) (kvs []*config.KeyValue, err error) {
files, err := ioutil.ReadDir(f.path)
files, err := os.ReadDir(f.path)
if err != nil {
return nil, err
}
Expand Down
7 changes: 3 additions & 4 deletions config/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package file

import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"sync"
Expand Down Expand Up @@ -93,7 +92,7 @@ func TestFile(t *testing.T) {
if err := os.MkdirAll(path, 0o700); err != nil {
t.Error(err)
}
if err := ioutil.WriteFile(file, data, 0o666); err != nil {
if err := os.WriteFile(file, data, 0o666); err != nil {
t.Error(err)
}
testSource(t, file, data)
Expand Down Expand Up @@ -181,7 +180,7 @@ func testSource(t *testing.T, path string, data []byte) {
func TestConfig(t *testing.T) {
path := filepath.Join(t.TempDir(), "test_config.json")
defer os.Remove(path)
if err := ioutil.WriteFile(path, []byte(_testJSON), 0o666); err != nil {
if err := os.WriteFile(path, []byte(_testJSON), 0o666); err != nil {
t.Error(err)
}
c := config.New(config.WithSource(
Expand Down Expand Up @@ -293,7 +292,7 @@ func testScan(t *testing.T, c config.Config) {
func TestMergeDataRace(t *testing.T) {
path := filepath.Join(t.TempDir(), "test_config.json")
defer os.Remove(path)
if err := ioutil.WriteFile(path, []byte(_testJSON), 0o666); err != nil {
if err := os.WriteFile(path, []byte(_testJSON), 0o666); err != nil {
t.Error(err)
}
c := config.New(config.WithSource(
Expand Down
4 changes: 2 additions & 2 deletions contrib/log/fluent/fluent_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fluent

import (
"io/ioutil"
"io"
"net"
"os"
"testing"
Expand All @@ -19,7 +19,7 @@ func TestMain(m *testing.M) {
return
}
defer conn.Close()
if _, err = ioutil.ReadAll(conn); err != nil {
if _, err = io.ReadAll(conn); err != nil {
continue
}
}
Expand Down
3 changes: 1 addition & 2 deletions contrib/registry/kubernetes/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"net/url"
"os"
"strconv"
Expand Down Expand Up @@ -249,7 +248,7 @@ var currentNamespace = LoadNamespace()

// LoadNamespace is used to get the current namespace from the file
func LoadNamespace() string {
data, err := ioutil.ReadFile(ServiceAccountNamespacePath)
data, err := os.ReadFile(ServiceAccountNamespacePath)
if err != nil {
return ""
}
Expand Down
4 changes: 2 additions & 2 deletions examples/tls/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"crypto/tls"
"crypto/x509"
"io/ioutil"
"log"
"os"

pb "github.com/go-kratos/kratos/examples/helloworld/helloworld"
"github.com/go-kratos/kratos/v2/transport/grpc"
Expand All @@ -14,7 +14,7 @@ import (

func main() {
// Load CA certificate pem file.
b, err := ioutil.ReadFile("../cert/ca.crt")
b, err := os.ReadFile("../cert/ca.crt")
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions examples/tls/tls_test.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"
"os"
"testing"
"time"

Expand Down Expand Up @@ -100,7 +100,7 @@ func TestETCD(t *testing.T) {
if err != nil {
t.Fatal(err)
}
b, err := ioutil.ReadFile("./cert/server.crt")
b, err := os.ReadFile("./cert/server.crt")
if err != nil {
t.Fatal(err)
}
Expand Down
8 changes: 4 additions & 4 deletions log/filter_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package log

import (
"io/ioutil"
"io"
"testing"
)

Expand Down Expand Up @@ -58,21 +58,21 @@ func TestFilterFunc(t *testing.T) {
}

func BenchmarkFilterKey(b *testing.B) {
log := NewHelper(NewFilter(NewStdLogger(ioutil.Discard), FilterKey("password")))
log := NewHelper(NewFilter(NewStdLogger(io.Discard), FilterKey("password")))
for i := 0; i < b.N; i++ {
log.Infow("password", "123456")
}
}

func BenchmarkFilterValue(b *testing.B) {
log := NewHelper(NewFilter(NewStdLogger(ioutil.Discard), FilterValue("password")))
log := NewHelper(NewFilter(NewStdLogger(io.Discard), FilterValue("password")))
for i := 0; i < b.N; i++ {
log.Infow("password")
}
}

func BenchmarkFilterFunc(b *testing.B) {
log := NewHelper(NewFilter(NewStdLogger(ioutil.Discard), FilterFunc(testFilterFunc)))
log := NewHelper(NewFilter(NewStdLogger(io.Discard), FilterFunc(testFilterFunc)))
for i := 0; i < b.N; i++ {
log.Info("password", "123456")
}
Expand Down
Loading

0 comments on commit 546c922

Please sign in to comment.