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: "io/ioutil" has been deprecated since Go 1.16 #217

Merged
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
5 changes: 2 additions & 3 deletions pkg/compile/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package compile

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"testing"
Expand All @@ -18,11 +17,11 @@ func TestGetKclPath(t *testing.T) {
_ = os.MkdirAll("./kclvm/bin", 0o777)

kclData := fmt.Sprintf("# kcl-test shell, %d", time.Now().Unix())
_ = ioutil.WriteFile("./kclvm/bin/kcl", []byte(kclData), 0o777)
_ = os.WriteFile("./kclvm/bin/kcl", []byte(kclData), 0o777)
defer os.RemoveAll("./kclvm")

kcl := getKclPath()
kclDataGot, _ := ioutil.ReadFile(kcl)
kclDataGot, _ := os.ReadFile(kcl)
if len(kclDataGot) > 50 {
kclDataGot = kclDataGot[:50]
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/engine/states/local/filesystem_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package local
import (
"encoding/json"
"io/fs"
"io/ioutil"
"os"
"time"

Expand Down Expand Up @@ -34,7 +33,7 @@ func (f *FileSystemState) GetLatestState(query *states.StateQuery) (*states.Stat
}
defer file.Close()

jsonFile, err := ioutil.ReadFile(f.Path)
jsonFile, err := os.ReadFile(f.Path)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -77,7 +76,7 @@ func (f *FileSystemState) Apply(state *states.State) error {
if err != nil {
return err
}
return ioutil.WriteFile(f.Path, jsonByte, fs.ModePerm)
return os.WriteFile(f.Path, jsonByte, fs.ModePerm)
}

func (f *FileSystemState) Delete(id string) error {
Expand Down
5 changes: 2 additions & 3 deletions pkg/engine/states/remote/http/http_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"strings"

"kusionstack.io/kusion/pkg/engine/states"

"kusionstack.io/kusion/pkg/log"
)

Expand Down Expand Up @@ -64,7 +63,7 @@ func (s *HTTPState) GetLatestState(query *states.StateQuery) (*states.State, err
}

state := &states.State{}
resBody, _ := ioutil.ReadAll(res.Body)
resBody, _ := io.ReadAll(res.Body)
err = json.Unmarshal(resBody, state)
if err != nil {
return nil, err
Expand Down
9 changes: 4 additions & 5 deletions pkg/engine/states/remote/oss/oss_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"

"kusionstack.io/kusion/pkg/engine/states"
"io"

"github.com/aliyun/aliyun-oss-go-sdk/oss"
"gopkg.in/yaml.v3"

"github.com/aliyun/aliyun-oss-go-sdk/oss"
"kusionstack.io/kusion/pkg/engine/states"
)

var ErrOSSNoExist = errors.New("oss: key not exist")
Expand Down Expand Up @@ -75,7 +74,7 @@ func (s *OssState) GetLatestState(query *states.StateQuery) (*states.State, erro
}
defer body.Close()

data, err := ioutil.ReadAll(body)
data, err := io.ReadAll(body)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/engine/states/remote/s3/s3_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"

"kusionstack.io/kusion/pkg/engine/states"
"io"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"

"kusionstack.io/kusion/pkg/engine/states"
)

var ErrS3NoExist = errors.New("s3: key not exist")
Expand Down Expand Up @@ -94,7 +94,7 @@ func (s *S3State) GetLatestState(query *states.StateQuery) (*states.State, error
}
defer out.Body.Close()

data, err := ioutil.ReadAll(out.Body)
data, err := io.ReadAll(out.Body)
if err != nil {
return nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/kusionctl/cmd/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"

"github.com/gonvenience/wrap"
Expand Down Expand Up @@ -85,7 +84,7 @@ func NewCmdDiff() *cobra.Command {
}

func liveDiffWithStdin() error {
data, err := ioutil.ReadAll(os.Stdin)
data, err := io.ReadAll(os.Stdin)
if err != nil {
return fmt.Errorf("unable to load data from os.stdin as %v", err)
}
Expand Down Expand Up @@ -190,7 +189,7 @@ func liveDiffWithFile(fromLocation, toLocation string) error {
}

func loadFile(location string) (*unstructured.Unstructured, error) {
data, err := ioutil.ReadFile(location)
data, err := os.ReadFile(location)
if err != nil {
return nil, wrap.Errorf(err, "failed to load input files")
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/scaffold/internal_templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package scaffold

import (
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand All @@ -19,11 +19,11 @@ func TestGenInternalTemplates(t *testing.T) {

var checkFiles func(src, target string) error
checkFiles = func(src, target string) error {
srcFileInfos, err := ioutil.ReadDir(src)
srcFileInfos, err := os.ReadDir(src)
if err != nil {
return err
}
targetFileInfos, err := ioutil.ReadDir(target)
targetFileInfos, err := os.ReadDir(target)
if err != nil {
return err
}
Expand All @@ -43,11 +43,11 @@ func TestGenInternalTemplates(t *testing.T) {
return checkFiles(srcPath, targetPath)
} else if !srcFileInfo.IsDir() && !targetFileInfo.IsDir() {
// read content
srcBytes, err := ioutil.ReadFile(srcPath)
srcBytes, err := os.ReadFile(srcPath)
if err != nil {
return err
}
targetBytes, err := ioutil.ReadFile(targetPath)
targetBytes, err := os.ReadFile(targetPath)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/scaffold/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package scaffold

import (
"errors"
"io/ioutil"
"os"
"sync"

"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -38,7 +38,7 @@ func (singleton *projectTemplateLoader) load(path string) (*ProjectTemplate, err
return v, nil
}

b, err := ioutil.ReadFile(path)
b, err := os.ReadFile(path)
if err != nil {
return nil, err
}
Expand Down
13 changes: 6 additions & 7 deletions pkg/scaffold/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -72,7 +71,7 @@ func (repo TemplateRepository) Templates() ([]Template, error) {

// Otherwise, read all subdirectories to find the ones
// that contain a kusion.yaml.
infos, err := ioutil.ReadDir(path)
infos, err := os.ReadDir(path)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -158,7 +157,7 @@ func retrieveURLTemplates(rawurl string, online bool) (TemplateRepository, error

// Create a temp dir.
var temp string
if temp, err = ioutil.TempDir("", "kusion-template-"); err != nil {
if temp, err = os.MkdirTemp("", "kusion-template-"); err != nil {
return TemplateRepository{}, err
}

Expand Down Expand Up @@ -311,9 +310,9 @@ func newTemplateNotFoundError(templateDir string, templateName string) error {
message := fmt.Sprintf("template '%s' not found", templateName)

// Attempt to read the directory to offer suggestions.
infos, err := ioutil.ReadDir(templateDir)
infos, err := os.ReadDir(templateDir)
if err != nil {
log.Errorf("ioutil.ReadDir(%s) error: %v", templateDir, err)
log.Errorf("os.ReadDir(%s) error: %v", templateDir, err)
return errors.New(message)
}

Expand Down Expand Up @@ -401,7 +400,7 @@ func RenderLocalTemplate(sourceDir, destDir string, force bool, tc *TemplateConf

// Read files' content from local dir into file system
func ReadTemplate(dir string, fs afero.Fs) error {
fileInfos, err := ioutil.ReadDir(dir)
fileInfos, err := os.ReadDir(dir)
if err != nil {
return err
}
Expand All @@ -416,7 +415,7 @@ func ReadTemplate(dir string, fs afero.Fs) error {
}
} else {
// Read source file content
content, err := ioutil.ReadFile(path)
content, err := os.ReadFile(path)
if err != nil {
return err
}
Expand Down
7 changes: 3 additions & 4 deletions pkg/scaffold/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package scaffold

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -87,7 +86,7 @@ var (

func TestTemplateRepository_Delete(t *testing.T) {
t.Run("should delete", func(t *testing.T) {
tmp, err := ioutil.TempDir("", "tmp-dir-for-test")
tmp, err := os.MkdirTemp("", "tmp-dir-for-test")
assert.Nil(t, err)
repo := TemplateRepository{
Root: tmp,
Expand Down Expand Up @@ -188,7 +187,7 @@ func Test_cleanupLegacyTemplateDir(t *testing.T) {
t.Run("repo not exist", func(t *testing.T) {
defer monkey.UnpatchAll()
monkey.Patch(GetTemplateDir, func(subDir string) (string, error) {
return ioutil.TempDir("", "tmp-dir-for-test")
return os.MkdirTemp("", "tmp-dir-for-test")
})

err = cleanupLegacyTemplateDir()
Expand Down Expand Up @@ -247,7 +246,7 @@ func TestValidateProjectName(t *testing.T) {

func TestRenderTemplateFiles(t *testing.T) {
// dest dir
tmp, err := ioutil.TempDir("", "tmp-dir-for-test")
tmp, err := os.MkdirTemp("", "tmp-dir-for-test")
assert.Nil(t, err)
defer func() {
err = os.RemoveAll(tmp)
Expand Down
5 changes: 2 additions & 3 deletions pkg/util/kfile/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package kfile

import (
"encoding/json"
"io/ioutil"
"os"
"os/user"
"path"
Expand Down Expand Up @@ -34,7 +33,7 @@ func Stat(filename string) (fileInfo os.FileInfo, err error) {
dirPath := filepath.Dir(filename)
base := filepath.Base(filename)

files, err := ioutil.ReadDir(dirPath)
files, err := os.ReadDir(dirPath)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -106,7 +105,7 @@ func GetCredentials() (map[string]interface{}, error) {
}
// Get kusion credentials data from credentials.json in kusion data folder
credentialsFilepath := filepath.Join(kusionDataFolder, KusionCredentialsFilename())
data, err := ioutil.ReadFile(credentialsFilepath)
data, err := os.ReadFile(credentialsFilepath)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/util/kfile/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package kfile

import (
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
Expand Down Expand Up @@ -134,7 +133,7 @@ func mockMkdirall() {
}

func mockReadFile() {
monkey.Patch(ioutil.ReadFile, func(filename string) ([]byte, error) {
monkey.Patch(os.ReadFile, func(filename string) ([]byte, error) {
return []byte(fmt.Sprintf(`{"token": "%s"}`, mockToken)), nil
})
}
5 changes: 2 additions & 3 deletions pkg/version/scripts/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"os"

_ "kusionstack.io/kcl-plugin"
Expand All @@ -29,9 +28,9 @@ func main() {

data := makeUpdateVersionGoFile(versionInfo)

err = ioutil.WriteFile(*flagOutFile, []byte(data), 0o666)
err = os.WriteFile(*flagOutFile, []byte(data), 0o666)
if err != nil {
log.Fatalf("ioutil.WriteFile: err = %v", err)
log.Fatalf("os.WriteFile: err = %v", err)
}

fmt.Println(versionInfo.String())
Expand Down
3 changes: 1 addition & 2 deletions third_party/dyff/_core_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
"regexp"
"strconv"
Expand Down Expand Up @@ -90,7 +89,7 @@ func compareAgainstExpected(fromPath string, toPath string, expectedPath string,
from, to, err := ytbx.LoadFiles(fromPath, toPath)
Expect(err).To(BeNil())

rawBytes, err := ioutil.ReadFile(expectedPath)
rawBytes, err := os.ReadFile(expectedPath)
Expect(err).To(BeNil())

report, err := CompareInputFiles(from, to, compareOptions...)
Expand Down
Loading