io/ioutil deprecated (#3217)

master
guangwu 2 years ago committed by GitHub
parent 4f13fe8188
commit 63368d8b0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2,7 +2,6 @@ package dartgen
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
) )
@ -83,7 +82,7 @@ func genVars(dir string, isLegacy bool, scheme string, hostname string) error {
} }
if !fileExists(dir + "vars.dart") { if !fileExists(dir + "vars.dart") {
err = ioutil.WriteFile(dir+"vars.dart", []byte(fmt.Sprintf(`const serverHost='%s://%s';`, scheme, hostname)), 0o644) err = os.WriteFile(dir+"vars.dart", []byte(fmt.Sprintf(`const serverHost='%s://%s';`, scheme, hostname)), 0o644)
if err != nil { if err != nil {
return err return err
} }
@ -94,7 +93,7 @@ func genVars(dir string, isLegacy bool, scheme string, hostname string) error {
if isLegacy { if isLegacy {
tpl = varTemplate tpl = varTemplate
} }
err = ioutil.WriteFile(dir+"kv.dart", []byte(tpl), 0o644) err = os.WriteFile(dir+"kv.dart", []byte(tpl), 0o644)
if err != nil { if err != nil {
return err return err
} }

@ -3,7 +3,6 @@ package format
import ( import (
"fmt" "fmt"
"io/fs" "io/fs"
"io/ioutil"
"os" "os"
"path" "path"
"testing" "testing"
@ -71,11 +70,11 @@ func Test_apiFormatReader_issue1721(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
importedFilename := path.Join(dir, "foo.api") importedFilename := path.Join(dir, "foo.api")
err = ioutil.WriteFile(importedFilename, []byte{}, fs.ModePerm) err = os.WriteFile(importedFilename, []byte{}, fs.ModePerm)
require.NoError(t, err) require.NoError(t, err)
filename := path.Join(subDir, "bar.api") filename := path.Join(subDir, "bar.api")
err = ioutil.WriteFile(filename, []byte(fmt.Sprintf(`import "%s"`, importedFilename)), 0o644) err = os.WriteFile(filename, []byte(fmt.Sprintf(`import "%s"`, importedFilename)), 0o644)
require.NoError(t, err) require.NoError(t, err)
f, err := os.Open(filename) f, err := os.Open(filename)

@ -1,7 +1,6 @@
package ast package ast
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -25,13 +24,13 @@ func Test_ImportCycle(t *testing.T) {
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
mainPath := filepath.Join(dir, mainFilename) mainPath := filepath.Join(dir, mainFilename)
err = ioutil.WriteFile(mainPath, []byte(mainSrc), 0o777) err = os.WriteFile(mainPath, []byte(mainSrc), 0o777)
require.NoError(t, err) require.NoError(t, err)
subAPath := filepath.Join(dir, subAFilename) subAPath := filepath.Join(dir, subAFilename)
err = ioutil.WriteFile(subAPath, []byte(subASrc), 0o777) err = os.WriteFile(subAPath, []byte(subASrc), 0o777)
require.NoError(t, err) require.NoError(t, err)
subBPath := filepath.Join(dir, subBFilename) subBPath := filepath.Join(dir, subBFilename)
err = ioutil.WriteFile(subBPath, []byte(subBSrc), 0o777) err = os.WriteFile(subBPath, []byte(subBSrc), 0o777)
require.NoError(t, err) require.NoError(t, err)
_, err = NewParser().Parse(mainPath) _, err = NewParser().Parse(mainPath)
@ -55,13 +54,13 @@ func Test_MultiImportedShouldAllowed(t *testing.T) {
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
mainPath := filepath.Join(dir, mainFilename) mainPath := filepath.Join(dir, mainFilename)
err = ioutil.WriteFile(mainPath, []byte(mainSrc), 0o777) err = os.WriteFile(mainPath, []byte(mainSrc), 0o777)
require.NoError(t, err) require.NoError(t, err)
subAPath := filepath.Join(dir, subAFilename) subAPath := filepath.Join(dir, subAFilename)
err = ioutil.WriteFile(subAPath, []byte(subASrc), 0o777) err = os.WriteFile(subAPath, []byte(subASrc), 0o777)
require.NoError(t, err) require.NoError(t, err)
subBPath := filepath.Join(dir, subBFilename) subBPath := filepath.Join(dir, subBFilename)
err = ioutil.WriteFile(subBPath, []byte(subBSrc), 0o777) err = os.WriteFile(subBPath, []byte(subBSrc), 0o777)
require.NoError(t, err) require.NoError(t, err)
_, err = NewParser().Parse(mainPath) _, err = NewParser().Parse(mainPath)
@ -84,13 +83,13 @@ func Test_RedundantDeclarationShouldNotBeAllowed(t *testing.T) {
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
mainPath := filepath.Join(dir, mainFilename) mainPath := filepath.Join(dir, mainFilename)
err = ioutil.WriteFile(mainPath, []byte(mainSrc), 0o777) err = os.WriteFile(mainPath, []byte(mainSrc), 0o777)
require.NoError(t, err) require.NoError(t, err)
subAPath := filepath.Join(dir, subAFilename) subAPath := filepath.Join(dir, subAFilename)
err = ioutil.WriteFile(subAPath, []byte(subASrc), 0o777) err = os.WriteFile(subAPath, []byte(subASrc), 0o777)
require.NoError(t, err) require.NoError(t, err)
subBPath := filepath.Join(dir, subBFilename) subBPath := filepath.Join(dir, subBFilename)
err = ioutil.WriteFile(subBPath, []byte(subBSrc), 0o777) err = os.WriteFile(subBPath, []byte(subBSrc), 0o777)
require.NoError(t, err) require.NoError(t, err)
_, err = NewParser().Parse(mainPath) _, err = NewParser().Parse(mainPath)

@ -2,7 +2,6 @@ package test
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -120,7 +119,7 @@ func TestApiParser(t *testing.T) {
t.Run("nestedImport", func(t *testing.T) { t.Run("nestedImport", func(t *testing.T) {
file := filepath.Join(pathx.MustTempDir(), "foo.api") file := filepath.Join(pathx.MustTempDir(), "foo.api")
err := ioutil.WriteFile(file, []byte(nestedAPIImport), os.ModePerm) err := os.WriteFile(file, []byte(nestedAPIImport), os.ModePerm)
if err != nil { if err != nil {
return return
} }
@ -150,7 +149,7 @@ func TestApiParser(t *testing.T) {
t.Run("ambiguousSyntax", func(t *testing.T) { t.Run("ambiguousSyntax", func(t *testing.T) {
file := filepath.Join(pathx.MustTempDir(), "foo.api") file := filepath.Join(pathx.MustTempDir(), "foo.api")
err := ioutil.WriteFile(file, []byte(ambiguousSyntax), os.ModePerm) err := os.WriteFile(file, []byte(ambiguousSyntax), os.ModePerm)
if err != nil { if err != nil {
return return
} }
@ -164,7 +163,7 @@ func TestApiParser(t *testing.T) {
t.Run("ambiguousSyntax", func(t *testing.T) { t.Run("ambiguousSyntax", func(t *testing.T) {
file := filepath.Join(pathx.MustTempDir(), "foo.api") file := filepath.Join(pathx.MustTempDir(), "foo.api")
err := ioutil.WriteFile(file, []byte(ambiguousSyntax), os.ModePerm) err := os.WriteFile(file, []byte(ambiguousSyntax), os.ModePerm)
if err != nil { if err != nil {
return return
} }
@ -178,7 +177,7 @@ func TestApiParser(t *testing.T) {
t.Run("ambiguousService", func(t *testing.T) { t.Run("ambiguousService", func(t *testing.T) {
file := filepath.Join(pathx.MustTempDir(), "foo.api") file := filepath.Join(pathx.MustTempDir(), "foo.api")
err := ioutil.WriteFile(file, []byte(ambiguousService), os.ModePerm) err := os.WriteFile(file, []byte(ambiguousService), os.ModePerm)
if err != nil { if err != nil {
return return
} }
@ -208,7 +207,7 @@ func TestApiParser(t *testing.T) {
assert.Error(t, err) assert.Error(t, err)
file := filepath.Join(pathx.MustTempDir(), "foo.api") file := filepath.Join(pathx.MustTempDir(), "foo.api")
err = ioutil.WriteFile(file, []byte(duplicateHandler), os.ModePerm) err = os.WriteFile(file, []byte(duplicateHandler), os.ModePerm)
if err != nil { if err != nil {
return return
} }
@ -237,7 +236,7 @@ func TestApiParser(t *testing.T) {
assert.Error(t, err) assert.Error(t, err)
file := filepath.Join(pathx.MustTempDir(), "foo.api") file := filepath.Join(pathx.MustTempDir(), "foo.api")
err = ioutil.WriteFile(file, []byte(duplicateRoute), os.ModePerm) err = os.WriteFile(file, []byte(duplicateRoute), os.ModePerm)
if err != nil { if err != nil {
return return
} }
@ -261,7 +260,7 @@ func TestApiParser(t *testing.T) {
assert.Error(t, err) assert.Error(t, err)
file := filepath.Join(pathx.MustTempDir(), "foo.api") file := filepath.Join(pathx.MustTempDir(), "foo.api")
err = ioutil.WriteFile(file, []byte(duplicateType), os.ModePerm) err = os.WriteFile(file, []byte(duplicateType), os.ModePerm)
if err != nil { if err != nil {
return return
} }

@ -2,7 +2,6 @@ package testdata
import ( import (
"fmt" "fmt"
"io/ioutil"
"log" "log"
"os" "os"
"os/exec" "os/exec"
@ -78,7 +77,7 @@ func mustGetTestData(baseDir string) (Files, Files) {
return data, nil return data, nil
} }
return data, ioutil.WriteFile(fp, []byte(data.Content), os.ModePerm) return data, os.WriteFile(fp, []byte(data.Content), os.ModePerm)
} }
oldDir := filepath.Join(baseDir, "old_fs") oldDir := filepath.Join(baseDir, "old_fs")
newDir := filepath.Join(baseDir, "new_fs") newDir := filepath.Join(baseDir, "new_fs")

@ -8,7 +8,6 @@ import (
"go/parser" "go/parser"
"go/token" "go/token"
"io/fs" "io/fs"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -163,7 +162,7 @@ func writeFile(pkgs []*ast.Package, verbose bool) error {
return fmt.Errorf("[rewriteImport] format file %s error: %w", filename, err) return fmt.Errorf("[rewriteImport] format file %s error: %w", filename, err)
} }
err = ioutil.WriteFile(filename, w.Bytes(), os.ModePerm) err = os.WriteFile(filename, w.Bytes(), os.ModePerm)
if err != nil { if err != nil {
return fmt.Errorf("[rewriteImport] write file %s error: %w", filename, err) return fmt.Errorf("[rewriteImport] write file %s error: %w", filename, err)
} }

@ -2,7 +2,7 @@ package migrate
import ( import (
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"strings" "strings"
"time" "time"
@ -34,7 +34,7 @@ func getLatest(repo string, verbose bool) ([]string, error) {
continue continue
} }
defer resp.Body.Close() defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body) data, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
log(err) log(err)
continue continue

@ -1,7 +1,7 @@
package generate package generate
import ( import (
"io/ioutil" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -22,7 +22,7 @@ func TestDo(t *testing.T) {
tempDir := pathx.MustTempDir() tempDir := pathx.MustTempDir()
typesfile := filepath.Join(tempDir, "types.go") typesfile := filepath.Join(tempDir, "types.go")
err = ioutil.WriteFile(typesfile, []byte(testTypes), 0o666) err = os.WriteFile(typesfile, []byte(testTypes), 0o666)
assert.Nil(t, err) assert.Nil(t, err)
err = Do(&Context{ err = Do(&Context{
@ -38,7 +38,7 @@ func TestDo(t *testing.T) {
t.Run("missing config", func(t *testing.T) { t.Run("missing config", func(t *testing.T) {
tempDir := t.TempDir() tempDir := t.TempDir()
typesfile := filepath.Join(tempDir, "types.go") typesfile := filepath.Join(tempDir, "types.go")
err := ioutil.WriteFile(typesfile, []byte(testTypes), 0o666) err := os.WriteFile(typesfile, []byte(testTypes), 0o666)
assert.Nil(t, err) assert.Nil(t, err)
err = Do(&Context{ err = Do(&Context{
@ -54,7 +54,7 @@ func TestDo(t *testing.T) {
cfg := &config.Config{NamingFormat: "foo"} cfg := &config.Config{NamingFormat: "foo"}
tempDir := t.TempDir() tempDir := t.TempDir()
typesfile := filepath.Join(tempDir, "types.go") typesfile := filepath.Join(tempDir, "types.go")
err := ioutil.WriteFile(typesfile, []byte(testTypes), 0o666) err := os.WriteFile(typesfile, []byte(testTypes), 0o666)
assert.Nil(t, err) assert.Nil(t, err)
err = Do(&Context{ err = Do(&Context{

@ -2,7 +2,6 @@ package command
import ( import (
_ "embed" _ "embed"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"sort" "sort"
@ -76,12 +75,12 @@ func TestFromDDl(t *testing.T) {
user1Sql := filepath.Join(tempDir, "user1.sql") user1Sql := filepath.Join(tempDir, "user1.sql")
user2Sql := filepath.Join(tempDir, "user2.sql") user2Sql := filepath.Join(tempDir, "user2.sql")
err = ioutil.WriteFile(user1Sql, []byte(sql), os.ModePerm) err = os.WriteFile(user1Sql, []byte(sql), os.ModePerm)
if err != nil { if err != nil {
return return
} }
err = ioutil.WriteFile(user2Sql, []byte(sql), os.ModePerm) err = os.WriteFile(user2Sql, []byte(sql), os.ModePerm)
if err != nil { if err != nil {
return return
} }

@ -2,7 +2,7 @@ package migrationnotes
import ( import (
"fmt" "fmt"
"io/ioutil" "os"
"strings" "strings"
) )
@ -24,7 +24,7 @@ func migrateBefore1_3_4(dir, style string) error {
} }
func needShow1_3_4(dir, style string) (bool, error) { func needShow1_3_4(dir, style string) (bool, error) {
files, err := ioutil.ReadDir(dir) files, err := os.ReadDir(dir)
if err != nil { if err != nil {
return false, nil return false, nil
} }

@ -2,7 +2,6 @@ package migrationnotes
import ( import (
"io/fs" "io/fs"
"io/ioutil"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -18,15 +17,15 @@ func Test_needShow1_3_4(t *testing.T) {
dir1 := path.Join(root, "dir1") dir1 := path.Join(root, "dir1")
require.NoError(t, os.Mkdir(dir1, fs.ModePerm)) require.NoError(t, os.Mkdir(dir1, fs.ModePerm))
ioutil.WriteFile(filepath.Join(dir1, "foo_gen.go"), nil, fs.ModePerm) os.WriteFile(filepath.Join(dir1, "foo_gen.go"), nil, fs.ModePerm)
dir2 := path.Join(root, "dir2") dir2 := path.Join(root, "dir2")
require.NoError(t, os.Mkdir(dir2, fs.ModePerm)) require.NoError(t, os.Mkdir(dir2, fs.ModePerm))
ioutil.WriteFile(filepath.Join(dir2, "foomodel.go"), nil, fs.ModePerm) os.WriteFile(filepath.Join(dir2, "foomodel.go"), nil, fs.ModePerm)
dir3 := path.Join(root, "dir3") dir3 := path.Join(root, "dir3")
require.NoError(t, os.Mkdir(dir3, fs.ModePerm)) require.NoError(t, os.Mkdir(dir3, fs.ModePerm))
ioutil.WriteFile(filepath.Join(dir3, "irrelevant.go"), nil, fs.ModePerm) os.WriteFile(filepath.Join(dir3, "irrelevant.go"), nil, fs.ModePerm)
type args struct { type args struct {
dir string dir string

@ -3,7 +3,6 @@ package gen
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -168,7 +167,7 @@ func (g *defaultGenerator) createFile(modelList map[string]*codeTuple) error {
name := util.SafeString(modelFilename) + "_gen.go" name := util.SafeString(modelFilename) + "_gen.go"
filename := filepath.Join(dirAbs, name) filename := filepath.Join(dirAbs, name)
err = ioutil.WriteFile(filename, []byte(codes.modelCode), os.ModePerm) err = os.WriteFile(filename, []byte(codes.modelCode), os.ModePerm)
if err != nil { if err != nil {
return err return err
} }
@ -179,7 +178,7 @@ func (g *defaultGenerator) createFile(modelList map[string]*codeTuple) error {
g.Warning("%s already exists, ignored.", name) g.Warning("%s already exists, ignored.", name)
continue continue
} }
err = ioutil.WriteFile(filename, []byte(codes.modelCustomCode), os.ModePerm) err = os.WriteFile(filename, []byte(codes.modelCustomCode), os.ModePerm)
if err != nil { if err != nil {
return err return err
} }

@ -3,7 +3,6 @@ package gen
import ( import (
"database/sql" "database/sql"
_ "embed" _ "embed"
"io/ioutil"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -30,7 +29,7 @@ func TestCacheModel(t *testing.T) {
_ = Clean() _ = Clean()
sqlFile := filepath.Join(pathx.MustTempDir(), "tmp.sql") sqlFile := filepath.Join(pathx.MustTempDir(), "tmp.sql")
err := ioutil.WriteFile(sqlFile, []byte(source), 0o777) err := os.WriteFile(sqlFile, []byte(source), 0o777)
assert.Nil(t, err) assert.Nil(t, err)
dir := filepath.Join(pathx.MustTempDir(), "./testmodel") dir := filepath.Join(pathx.MustTempDir(), "./testmodel")
@ -65,7 +64,7 @@ func TestNamingModel(t *testing.T) {
_ = Clean() _ = Clean()
sqlFile := filepath.Join(pathx.MustTempDir(), "tmp.sql") sqlFile := filepath.Join(pathx.MustTempDir(), "tmp.sql")
err := ioutil.WriteFile(sqlFile, []byte(source), 0o777) err := os.WriteFile(sqlFile, []byte(source), 0o777)
assert.Nil(t, err) assert.Nil(t, err)
dir, _ := filepath.Abs("./testmodel") dir, _ := filepath.Abs("./testmodel")
@ -103,7 +102,7 @@ func TestFolderName(t *testing.T) {
_ = Clean() _ = Clean()
sqlFile := filepath.Join(pathx.MustTempDir(), "tmp.sql") sqlFile := filepath.Join(pathx.MustTempDir(), "tmp.sql")
err := ioutil.WriteFile(sqlFile, []byte(source), 0o777) err := os.WriteFile(sqlFile, []byte(source), 0o777)
assert.Nil(t, err) assert.Nil(t, err)
dir, _ := filepath.Abs("./testmodel") dir, _ := filepath.Abs("./testmodel")
@ -179,7 +178,7 @@ func Test_genPublicModel(t *testing.T) {
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
modelFilename := filepath.Join(modelDir, "foo.sql") modelFilename := filepath.Join(modelDir, "foo.sql")
err = ioutil.WriteFile(modelFilename, []byte(source), 0o777) err = os.WriteFile(modelFilename, []byte(source), 0o777)
require.NoError(t, err) require.NoError(t, err)
g, err := NewDefaultGenerator(modelDir, &config.Config{ g, err := NewDefaultGenerator(modelDir, &config.Config{

@ -2,7 +2,7 @@ package parser
import ( import (
_ "embed" _ "embed"
"io/ioutil" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -15,7 +15,7 @@ import (
func TestParsePlainText(t *testing.T) { func TestParsePlainText(t *testing.T) {
sqlFile := filepath.Join(pathx.MustTempDir(), "tmp.sql") sqlFile := filepath.Join(pathx.MustTempDir(), "tmp.sql")
err := ioutil.WriteFile(sqlFile, []byte("plain text"), 0o777) err := os.WriteFile(sqlFile, []byte("plain text"), 0o777)
assert.Nil(t, err) assert.Nil(t, err)
_, err = Parse(sqlFile, "go_zero", false) _, err = Parse(sqlFile, "go_zero", false)
@ -24,7 +24,7 @@ func TestParsePlainText(t *testing.T) {
func TestParseSelect(t *testing.T) { func TestParseSelect(t *testing.T) {
sqlFile := filepath.Join(pathx.MustTempDir(), "tmp.sql") sqlFile := filepath.Join(pathx.MustTempDir(), "tmp.sql")
err := ioutil.WriteFile(sqlFile, []byte("select * from user"), 0o777) err := os.WriteFile(sqlFile, []byte("select * from user"), 0o777)
assert.Nil(t, err) assert.Nil(t, err)
tables, err := Parse(sqlFile, "go_zero", false) tables, err := Parse(sqlFile, "go_zero", false)
@ -37,7 +37,7 @@ var user string
func TestParseCreateTable(t *testing.T) { func TestParseCreateTable(t *testing.T) {
sqlFile := filepath.Join(pathx.MustTempDir(), "tmp.sql") sqlFile := filepath.Join(pathx.MustTempDir(), "tmp.sql")
err := ioutil.WriteFile(sqlFile, []byte(user), 0o777) err := os.WriteFile(sqlFile, []byte(user), 0o777)
assert.Nil(t, err) assert.Nil(t, err)
tables, err := Parse(sqlFile, "go_zero", false) tables, err := Parse(sqlFile, "go_zero", false)

@ -1,7 +1,7 @@
package util package util
import ( import (
"io/ioutil" "os"
"path/filepath" "path/filepath"
) )
@ -13,7 +13,7 @@ func MatchFiles(in string) ([]string, error) {
return nil, err return nil, err
} }
files, err := ioutil.ReadDir(abs) files, err := os.ReadDir(abs)
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -3,14 +3,14 @@ package format
import ( import (
"bytes" "bytes"
"io" "io"
"io/ioutil" "os"
"github.com/zeromicro/go-zero/tools/goctl/pkg/parser/api/parser" "github.com/zeromicro/go-zero/tools/goctl/pkg/parser/api/parser"
) )
// File formats the api file. // File formats the api file.
func File(filename string) error { func File(filename string) error {
data, err := ioutil.ReadFile(filename) data, err := os.ReadFile(filename)
if err != nil { if err != nil {
return err return err
} }
@ -18,7 +18,7 @@ func File(filename string) error {
if err := Source(data, buffer); err != nil { if err := Source(data, buffer); err != nil {
return err return err
} }
return ioutil.WriteFile(filename, buffer.Bytes(), 0666) return os.WriteFile(filename, buffer.Bytes(), 0666)
} }
// Source formats the api source. // Source formats the api source.

@ -3,7 +3,7 @@ package parser
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"testing" "testing"
@ -18,7 +18,7 @@ func Test_Parse(t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
}) })
t.Run("invalid", func(t *testing.T) { t.Run("invalid", func(t *testing.T) {
data, err := ioutil.ReadFile("./testdata/invalid.api") data, err := os.ReadFile("./testdata/invalid.api")
assert.NoError(t, err) assert.NoError(t, err)
splits := bytes.Split(data, []byte("-----")) splits := bytes.Split(data, []byte("-----"))
var testFile []string var testFile []string
@ -29,7 +29,7 @@ func Test_Parse(t *testing.T) {
continue continue
} }
filename := filepath.Join(t.TempDir(), fmt.Sprintf("invalid%d.api", idx)) filename := filepath.Join(t.TempDir(), fmt.Sprintf("invalid%d.api", idx))
err := ioutil.WriteFile(filename, split, 0666) err := os.WriteFile(filename, split, 0666)
assert.NoError(t, err) assert.NoError(t, err)
testFile = append(testFile, filename) testFile = append(testFile, filename)
} }

@ -4,8 +4,8 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -647,7 +647,7 @@ func NewScanner(filename string, src interface{}) (*Scanner, error) {
} }
func readData(filename string, src interface{}) ([]byte, error) { func readData(filename string, src interface{}) ([]byte, error) {
data, err := ioutil.ReadFile(filename) data, err := os.ReadFile(filename)
if err == nil { if err == nil {
return data, nil return data, nil
} }

@ -6,7 +6,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
@ -171,7 +170,7 @@ func downloadFile(filepath, url string) error {
// NewPlugin returns contextual resources when written in other languages // NewPlugin returns contextual resources when written in other languages
func NewPlugin() (*Plugin, error) { func NewPlugin() (*Plugin, error) {
var plugin Plugin var plugin Plugin
content, err := ioutil.ReadAll(os.Stdin) content, err := io.ReadAll(os.Stdin)
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -2,7 +2,7 @@ package quickstart
import ( import (
_ "embed" _ "embed"
"io/ioutil" "os"
"path/filepath" "path/filepath"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
@ -38,7 +38,7 @@ func initRPCProto() error {
protoFilename := filepath.Join(zrpcWorkDir, protoName) protoFilename := filepath.Join(zrpcWorkDir, protoName)
rpcBytes := []byte(protocContent) rpcBytes := []byte(protocContent)
return ioutil.WriteFile(protoFilename, rpcBytes, 0o666) return os.WriteFile(protoFilename, rpcBytes, 0o666)
} }
type micro struct{} type micro struct{}
@ -55,7 +55,7 @@ func (m micro) mustStartRPCProject() {
arg := "goctl rpc protoc " + protoName + " --go_out=. --go-grpc_out=. --zrpc_out=. --verbose" arg := "goctl rpc protoc " + protoName + " --go_out=. --go-grpc_out=. --zrpc_out=. --verbose"
execCommand(zrpcWorkDir, arg) execCommand(zrpcWorkDir, arg)
etcFile := filepath.Join(zrpcWorkDir, "etc", "greet.yaml") etcFile := filepath.Join(zrpcWorkDir, "etc", "greet.yaml")
logx.Must(ioutil.WriteFile(etcFile, []byte(rpcEtcContent), 0o666)) logx.Must(os.WriteFile(etcFile, []byte(rpcEtcContent), 0o666))
} }
func (m micro) start() { func (m micro) start() {

@ -2,7 +2,7 @@ package quickstart
import ( import (
_ "embed" _ "embed"
"io/ioutil" "os"
"path/filepath" "path/filepath"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
@ -35,7 +35,7 @@ func initAPIFlags() error {
apiFilename := filepath.Join(apiWorkDir, "greet.api") apiFilename := filepath.Join(apiWorkDir, "greet.api")
apiBytes := []byte(apiContent) apiBytes := []byte(apiContent)
if err := ioutil.WriteFile(apiFilename, apiBytes, 0o666); err != nil { if err := os.WriteFile(apiFilename, apiBytes, 0o666); err != nil {
return err return err
} }
@ -59,7 +59,7 @@ func (m mono) createAPIProject() {
log.Debug(">> Generating quickstart api project...") log.Debug(">> Generating quickstart api project...")
logx.Must(gogen.GoCommand(nil, nil)) logx.Must(gogen.GoCommand(nil, nil))
etcFile := filepath.Join(apiWorkDir, "etc", "greet.yaml") etcFile := filepath.Join(apiWorkDir, "etc", "greet.yaml")
logx.Must(ioutil.WriteFile(etcFile, []byte(apiEtcContent), 0o666)) logx.Must(os.WriteFile(etcFile, []byte(apiEtcContent), 0o666))
logicFile := filepath.Join(apiWorkDir, "internal", "logic", "pinglogic.go") logicFile := filepath.Join(apiWorkDir, "internal", "logic", "pinglogic.go")
svcFile := filepath.Join(apiWorkDir, "internal", "svc", "servicecontext.go") svcFile := filepath.Join(apiWorkDir, "internal", "svc", "servicecontext.go")
configPath := filepath.Join(apiWorkDir, "internal", "config") configPath := filepath.Join(apiWorkDir, "internal", "config")

@ -2,7 +2,6 @@ package generator
import ( import (
_ "embed" _ "embed"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -36,5 +35,5 @@ func (g *Generator) GenConfig(ctx DirContext, _ parser.Proto, cfg *conf.Config)
return err return err
} }
return ioutil.WriteFile(fileName, []byte(text), os.ModePerm) return os.WriteFile(fileName, []byte(text), os.ModePerm)
} }

@ -1,7 +1,6 @@
package generator package generator
import ( import (
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@ -14,7 +13,7 @@ import (
func Test_findPbFile(t *testing.T) { func Test_findPbFile(t *testing.T) {
dir := t.TempDir() dir := t.TempDir()
protoFile := filepath.Join(dir, "greet.proto") protoFile := filepath.Join(dir, "greet.proto")
err := ioutil.WriteFile(protoFile, []byte(` err := os.WriteFile(protoFile, []byte(`
syntax = "proto3"; syntax = "proto3";
package greet; package greet;

@ -6,7 +6,7 @@ import (
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/fs"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
@ -145,9 +145,20 @@ func GetTemplateDir(category string) (string, error) {
// backward compatible, it will be removed in the feature // backward compatible, it will be removed in the feature
// backward compatible start. // backward compatible start.
beforeTemplateDir := filepath.Join(home, version.GetGoctlVersion(), category) beforeTemplateDir := filepath.Join(home, version.GetGoctlVersion(), category)
fs, _ := ioutil.ReadDir(beforeTemplateDir) entries, err := os.ReadDir(beforeTemplateDir)
if err != nil {
return "", err
}
infos := make([]fs.FileInfo, 0, len(entries))
for _, entry := range entries {
info, err := entry.Info()
if err != nil {
return "", err
}
infos = append(infos, info)
}
var hasContent bool var hasContent bool
for _, e := range fs { for _, e := range infos {
if e.Size() > 0 { if e.Size() > 0 {
hasContent = true hasContent = true
} }
@ -256,7 +267,7 @@ func createTemplate(file, content string, force bool) error {
// MustTempDir creates a temporary directory. // MustTempDir creates a temporary directory.
func MustTempDir() string { func MustTempDir() string {
dir, err := ioutil.TempDir("", "") dir, err := os.MkdirTemp("", "")
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }

@ -1,7 +1,6 @@
package pathx package pathx
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -23,7 +22,7 @@ func TestGetTemplateDir(t *testing.T) {
return return
} }
tempFile := filepath.Join(dir, "bar.txt") tempFile := filepath.Join(dir, "bar.txt")
err = ioutil.WriteFile(tempFile, []byte("foo"), os.ModePerm) err = os.WriteFile(tempFile, []byte("foo"), os.ModePerm)
if err != nil { if err != nil {
return return
} }
@ -79,7 +78,7 @@ func TestGetGoctlHome(t *testing.T) {
t.Run("goctl_is_file", func(t *testing.T) { t.Run("goctl_is_file", func(t *testing.T) {
tmpFile := filepath.Join(t.TempDir(), "a.tmp") tmpFile := filepath.Join(t.TempDir(), "a.tmp")
backupTempFile := tmpFile + ".old" backupTempFile := tmpFile + ".old"
err := ioutil.WriteFile(tmpFile, nil, 0o666) err := os.WriteFile(tmpFile, nil, 0o666)
if err != nil { if err != nil {
return return
} }

@ -1,7 +1,6 @@
package pathx package pathx
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -10,9 +9,9 @@ import (
) )
func TestReadLink(t *testing.T) { func TestReadLink(t *testing.T) {
dir, err := ioutil.TempDir("", "go-zero") dir, err := os.CreateTemp("", "go-zero")
assert.Nil(t, err) assert.Nil(t, err)
symLink := filepath.Join(dir, "test") symLink := filepath.Join(dir.Name(), "test")
pwd, err := os.Getwd() pwd, err := os.Getwd()
assertError(err, t) assertError(err, t)

@ -3,7 +3,7 @@ package util
import ( import (
"bytes" "bytes"
goformat "go/format" goformat "go/format"
"io/ioutil" "os"
"regexp" "regexp"
"text/template" "text/template"
@ -50,7 +50,7 @@ func (t *DefaultTemplate) SaveTo(data any, path string, forceUpdate bool) error
return err return err
} }
return ioutil.WriteFile(path, output.Bytes(), regularPerm) return os.WriteFile(path, output.Bytes(), regularPerm)
} }
// Execute returns the codes after the template executed // Execute returns the codes after the template executed

Loading…
Cancel
Save