Skip to content

Commit

Permalink
Merge pull request #970 from opendhc/cl-devstream
Browse files Browse the repository at this point in the history
refactor(LSC): replace deprecated functions under pkg io/ioutil
  • Loading branch information
daniel-hutao authored Aug 10, 2022
2 parents 7516663 + 946165c commit b669039
Show file tree
Hide file tree
Showing 21 changed files with 55 additions and 62 deletions.
4 changes: 2 additions & 2 deletions internal/pkg/aws/s3/s3file.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"io"
"strings"

"github.com/aws/aws-sdk-go-v2/aws"
Expand Down Expand Up @@ -100,7 +100,7 @@ func (f *S3File) Get() ([]byte, 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 internal/pkg/aws/s3/s3file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"io"
"io/ioutil"
"strconv"
"testing"

Expand Down Expand Up @@ -43,7 +42,7 @@ func (mock *MockS3Client) GetObject(ctx context.Context, params *s3.GetObjectInp
checkStringParam(mock.t, "key", mock.key, params.Key)

return &s3.GetObjectOutput{
Body: ioutil.NopCloser(bytes.NewReader([]byte(TestContent))),
Body: io.NopCloser(bytes.NewReader([]byte(TestContent))),
}, nil
}

Expand Down Expand Up @@ -71,7 +70,7 @@ func checkStringParam(t *testing.T, paramName, expected string, actual *string)
func checkBodyParam(t *testing.T, expected []byte, body io.Reader) {
t.Helper()

actual, err := ioutil.ReadAll(body)
actual, err := io.ReadAll(body)
if err != nil {
t.Fatalf("failed to get data from body: %s", err)
}
Expand Down
5 changes: 2 additions & 3 deletions internal/pkg/backend/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package local

import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"sync"
Expand Down Expand Up @@ -44,7 +43,7 @@ func (l *Local) Read() ([]byte, error) {
l.mu.Lock()
defer l.mu.Unlock()

data, err := ioutil.ReadFile(l.filename)
data, err := os.ReadFile(l.filename)
if err != nil {
return nil, err
}
Expand All @@ -61,7 +60,7 @@ func (l *Local) Write(data []byte) error {
return err
}

if err := ioutil.WriteFile(l.filename, data, 0644); err != nil {
if err := os.WriteFile(l.filename, data, 0644); err != nil {
return err
}
return nil
Expand Down
5 changes: 2 additions & 3 deletions internal/pkg/backend/local/local_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package local_test

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

Expand Down Expand Up @@ -47,7 +46,7 @@ var _ = Describe("Local struct", func() {

BeforeEach(func() {
testData = []byte("this is test data")
err := ioutil.WriteFile(tFileLoc, testData, 0644)
err := os.WriteFile(tFileLoc, testData, 0644)
Expect(err).Error().ShouldNot(HaveOccurred())
})

Expand All @@ -68,7 +67,7 @@ var _ = Describe("Local struct", func() {
It("should write data to file", func() {
err := tLocal.Write(writeData)
Expect(err).Error().ShouldNot(HaveOccurred())
fileData, err := ioutil.ReadFile(tFileLoc)
fileData, err := os.ReadFile(tFileLoc)
Expect(err).Error().ShouldNot(HaveOccurred())
Expect(fileData).Should(Equal(writeData))
})
Expand Down
17 changes: 9 additions & 8 deletions internal/pkg/configmanager/configmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package configmanager
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"runtime"
"strings"

Expand Down Expand Up @@ -153,7 +153,7 @@ func (m *Manager) renderToolsFromCoreConfigAndConfigBytes(coreConfig *CoreConfig
}

func (m *Manager) loadOriginalConfigFile() ([]byte, error) {
originalConfigFileBytes, err := ioutil.ReadFile(m.ConfigFile)
originalConfigFileBytes, err := os.ReadFile(m.ConfigFile)
if err != nil {
log.Errorf("Failed to read the config file. Error: %s", err)
log.Info("Maybe the default file (config.yaml) doesn't exist or you forgot to pass your config file to the \"-f\" option?")
Expand All @@ -174,9 +174,10 @@ func (m *Manager) loadOriginalConfigFile() ([]byte, error) {
// varFile: "" # If not empty, use the specified external variables config file
// toolFile: "" # If not empty, use the specified external tools config file
// state:
// backend: local
// options:
// stateFile: devstream.state
//
// backend: local
// options:
// stateFile: devstream.state
//
// ---
// # variables config
Expand All @@ -185,9 +186,9 @@ func (m *Manager) loadOriginalConfigFile() ([]byte, error) {
// ---
// # plugins config
// tools:
// - name: A-PLUGIN-NAME
// instanceID: default
// options:
// - name: A-PLUGIN-NAME
// instanceID: default
// options:
// foo: bar
//
// See https://github.com/devstream-io/devstream/issues/596 for more details.
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/configmanager/toolconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package configmanager

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

"gopkg.in/yaml.v3"

Expand Down Expand Up @@ -108,7 +108,7 @@ func loadVarsIntoMap(varConfigBytes []byte) (map[string]interface{}, error) {
}

func readFile(filePath string) ([]byte, error) {
fileBytes, err := ioutil.ReadFile(filePath)
fileBytes, err := os.ReadFile(filePath)
if err != nil {
log.Errorf("Failed to read the %s: %s", filePath, err)
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/plugin/argocdapp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Source struct {
RepoURL string `validate:"required"`
}

/// NewOptions create options by raw options
// / NewOptions create options by raw options
func NewOptions(options plugininstaller.RawOptions) (Options, error) {
var opts Options
if err := mapstructure.Decode(options, &opts); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/plugininstaller/helm/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func InstallOrUpdate(options plugininstaller.RawOptions) error {
return err
}

//DealWithNsWhenInstall will create namespace by input options
// DealWithNsWhenInstall will create namespace by input options
func DealWithNsWhenInstall(options plugininstaller.RawOptions) error {
opts, err := NewOptions(options)
if err != nil {
Expand Down Expand Up @@ -72,7 +72,7 @@ func DealWithNsWhenInstall(options plugininstaller.RawOptions) error {
return nil
}

//DealWithNsWhenInterruption will Delete namespace by input options
// DealWithNsWhenInterruption will Delete namespace by input options
func DealWithNsWhenInterruption(options plugininstaller.RawOptions) error {
opts, err := NewOptions(options)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions pkg/util/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package file

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
Expand Down Expand Up @@ -112,9 +111,9 @@ func (c *TemplateConfig) Run() (string, error) {

// CopyFile will copy file content from src to dst
func CopyFile(src, dest string) error {
bytesRead, err := ioutil.ReadFile(src)
bytesRead, err := os.ReadFile(src)
if err != nil {
return err
}
return ioutil.WriteFile(dest, bytesRead, 0644)
return os.WriteFile(dest, bytesRead, 0644)
}
5 changes: 2 additions & 3 deletions pkg/util/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"

Expand Down Expand Up @@ -146,11 +145,11 @@ var _ = Describe("CopyFile func", func() {
})

It("should copy content form src to dst", func() {
err := ioutil.WriteFile(srcPath, testContent, 0666)
err := os.WriteFile(srcPath, testContent, 0666)
Expect(err).Error().ShouldNot(HaveOccurred())
err = CopyFile(srcPath, dstPath)
Expect(err).Error().ShouldNot(HaveOccurred())
data, err := ioutil.ReadFile(dstPath)
data, err := os.ReadFile(dstPath)
Expect(err).Error().ShouldNot(HaveOccurred())
Expect(data).Should(Equal(testContent))
})
Expand Down
3 changes: 1 addition & 2 deletions pkg/util/file/local_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package file

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

Expand Down Expand Up @@ -52,7 +51,7 @@ var _ = Describe("getFileFromContent func", func() {
It("should return a file for content", func() {
fileName, _ := getFileFromContent(testContent)
// check file exist
content, err := ioutil.ReadFile(fileName)
content, err := os.ReadFile(fileName)
Expect(err).ShouldNot(HaveOccurred())
Expect(string(content)).Should(Equal(testContent))
})
Expand Down
5 changes: 2 additions & 3 deletions pkg/util/file/processer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"archive/zip"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -38,11 +37,11 @@ var _ = Describe("unZipFileProcesser func", func() {
It("should work", func() {
dstPath, err := unZipFileProcesser(zipFileName)
Expect(err).Error().ShouldNot(HaveOccurred())
dirFiles, err := ioutil.ReadDir(dstPath)
dirFiles, err := os.ReadDir(dstPath)
Expect(err).Error().ShouldNot(HaveOccurred())
Expect(len(dirFiles)).Should(Equal(1))
Expect(dirFiles[0].Name()).Should(Equal(zipLocation))
zipDirFiles, err := ioutil.ReadDir(filepath.Join(dstPath, zipLocation))
zipDirFiles, err := os.ReadDir(filepath.Join(dstPath, zipLocation))
Expect(err).Error().ShouldNot(HaveOccurred())
Expect(len(dirFiles)).Should(Equal(1))
Expect(zipDirFiles[0].Name()).Should(Equal(tempFile))
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/file/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package file

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

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -53,7 +53,7 @@ var _ = Describe("getFileFromURL", func() {
reqURL := fmt.Sprintf("%s%s", server.URL(), testPath)
fileName, err := getFileFromURL(reqURL)
Expect(err).Error().ShouldNot(HaveOccurred())
fileContent, err := ioutil.ReadFile(fileName)
fileContent, err := os.ReadFile(fileName)
Expect(err).Error().ShouldNot(HaveOccurred())
Expect(string(fileContent)).Should(Equal(remoteContent))
})
Expand Down
17 changes: 8 additions & 9 deletions pkg/util/file/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package file

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

Expand All @@ -23,7 +22,7 @@ var _ = Describe("renderFile func", func() {
Expect(err).Error().ShouldNot(HaveOccurred())
defer tempFile.Close()
tempFilePath = tempFile.Name()
err = ioutil.WriteFile(tempFilePath, []byte(templateContent), 0666)
err = os.WriteFile(tempFilePath, []byte(templateContent), 0666)
Expect(err).Error().ShouldNot(HaveOccurred())
})

Expand Down Expand Up @@ -58,7 +57,7 @@ var _ = Describe("renderFile func", func() {
},
})
Expect(err).Error().ShouldNot(HaveOccurred())
content, err := ioutil.ReadFile(dstPath)
content, err := os.ReadFile(dstPath)
Expect(err).Error().ShouldNot(HaveOccurred())
Expect(string(content)).Should(Equal(rightContent))
})
Expand Down Expand Up @@ -107,7 +106,7 @@ var _ = Describe("renderGitRepoDir func", func() {
f, err := os.Create(filePath)
Expect(err).Error().ShouldNot(HaveOccurred())
defer f.Close()
err = ioutil.WriteFile(filePath, []byte(content), 0755)
err = os.WriteFile(filePath, []byte(content), 0755)
Expect(err).Error().ShouldNot(HaveOccurred())
}
createDir := func(dirPath string) {
Expand Down Expand Up @@ -158,24 +157,24 @@ var _ = Describe("renderGitRepoDir func", func() {
It("should render all dir", func() {
dstPath, err := renderGitRepoDir("test", srcPath, vars)
Expect(err).Error().ShouldNot(HaveOccurred())
files, err := ioutil.ReadDir(dstPath)
files, err := os.ReadDir(dstPath)
Expect(err).Error().ShouldNot(HaveOccurred())
// test README.md dir is not copied
Expect(len(files)).Should(Equal(2))
// test git dir files should not copied
gitDirFiles, err := ioutil.ReadDir(filepath.Join(dstPath, ".git"))
gitDirFiles, err := os.ReadDir(filepath.Join(dstPath, ".git"))
Expect(err).Error().ShouldNot(HaveOccurred())
Expect(len(gitDirFiles)).Should(Equal(0))
// test content dir files is copied
contentDirLoc := filepath.Join(dstPath, contentDir)
contentFiles, err := ioutil.ReadDir(contentDirLoc)
contentFiles, err := os.ReadDir(contentDirLoc)
Expect(err).Error().ShouldNot(HaveOccurred())
Expect(len(contentFiles)).Should(Equal(2))
// test file content
tplFileContent, err := ioutil.ReadFile(filepath.Join(contentDirLoc, "test.yaml"))
tplFileContent, err := os.ReadFile(filepath.Join(contentDirLoc, "test.yaml"))
Expect(err).Error().ShouldNot(HaveOccurred())
Expect(string(tplFileContent)).Should(Equal(renderdContent))
rawFileContent, err := ioutil.ReadFile(filepath.Join(contentDirLoc, "raw.txt"))
rawFileContent, err := os.ReadFile(filepath.Join(contentDirLoc, "raw.txt"))
Expect(err).Error().ShouldNot(HaveOccurred())
Expect(string(rawFileContent)).Should(Equal(rawContent))
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/github/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package github

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

"github.com/google/go-github/v42/github"
Expand Down Expand Up @@ -42,7 +42,7 @@ func (c *Client) PushLocalPath(repoPath, branch string) error {

log.Debugf("Found file: %s.", path)

content, err := ioutil.ReadFile(path)
content, err := os.ReadFile(path)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/github/github_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package github

import (
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -49,7 +49,7 @@ func DoTestMethod(t *testing.T, r *http.Request, want string) {
}

func DoTestBody(t *testing.T, r *http.Request, want string) {
b, err := ioutil.ReadAll(r.Body)
b, err := io.ReadAll(r.Body)
if err != nil {
t.Errorf("Error reading request body: %v", err)
}
Expand Down
Loading

0 comments on commit b669039

Please sign in to comment.