From 2e1988847cd49d021e76ea6f74e3f3953999a8df Mon Sep 17 00:00:00 2001 From: xinqshang Date: Tue, 9 Aug 2022 14:31:56 +0800 Subject: [PATCH 1/2] refactor(LSC): replace deprecated functions under pkg io/ioutil Signed-off-by: xinqshang --- Makefile | 2 ++ internal/pkg/aws/s3/s3file.go | 4 ++-- internal/pkg/aws/s3/s3file_test.go | 5 ++--- internal/pkg/backend/local/local.go | 5 ++--- internal/pkg/backend/local/local_test.go | 5 ++--- internal/pkg/configloader/config.go | 17 +++++++++-------- internal/pkg/configloader/toolconfig.go | 4 ++-- internal/pkg/plugin/argocdapp/options.go | 2 +- internal/pkg/plugininstaller/helm/installer.go | 4 ++-- pkg/util/file/file.go | 5 ++--- pkg/util/file/file_test.go | 5 ++--- pkg/util/file/local_test.go | 3 +-- pkg/util/file/processer_test.go | 5 ++--- pkg/util/file/remote_test.go | 4 ++-- pkg/util/file/render_test.go | 17 ++++++++--------- pkg/util/github/file.go | 4 ++-- pkg/util/github/github_test.go | 4 ++-- pkg/util/gitlab/file.go | 4 ++-- pkg/util/k8s/state.go | 2 +- pkg/util/k8s/static.go | 8 ++++---- pkg/util/md5/md5.go | 4 ++-- pkg/util/template/render.go | 6 +++--- 22 files changed, 57 insertions(+), 62 deletions(-) diff --git a/Makefile b/Makefile index 9965dabe3..961cf0580 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,7 @@ SELF_DIR=$(dir $(lastword $(MAKEFILE_LIST))) +echo "SELF_DIR:$SELF_DIR" + GOOS=$(shell go env GOOS) GOPATH=$(shell go env GOPATH) GOARCH=$(shell go env GOARCH) diff --git a/internal/pkg/aws/s3/s3file.go b/internal/pkg/aws/s3/s3file.go index 8e6860ca2..13cf19c47 100644 --- a/internal/pkg/aws/s3/s3file.go +++ b/internal/pkg/aws/s3/s3file.go @@ -4,7 +4,7 @@ import ( "bytes" "context" "fmt" - "io/ioutil" + "io" "strings" "github.com/aws/aws-sdk-go-v2/aws" @@ -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 } diff --git a/internal/pkg/aws/s3/s3file_test.go b/internal/pkg/aws/s3/s3file_test.go index 039fed2bc..c5c6e42e5 100644 --- a/internal/pkg/aws/s3/s3file_test.go +++ b/internal/pkg/aws/s3/s3file_test.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "io" - "io/ioutil" "strconv" "testing" @@ -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 } @@ -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) } diff --git a/internal/pkg/backend/local/local.go b/internal/pkg/backend/local/local.go index 2a0f565fa..bd7f2a751 100644 --- a/internal/pkg/backend/local/local.go +++ b/internal/pkg/backend/local/local.go @@ -2,7 +2,6 @@ package local import ( "errors" - "io/ioutil" "os" "path/filepath" "sync" @@ -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 } @@ -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 diff --git a/internal/pkg/backend/local/local_test.go b/internal/pkg/backend/local/local_test.go index b3b70c865..bd52d3187 100644 --- a/internal/pkg/backend/local/local_test.go +++ b/internal/pkg/backend/local/local_test.go @@ -1,7 +1,6 @@ package local_test import ( - "io/ioutil" "os" "path/filepath" @@ -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()) }) @@ -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)) }) diff --git a/internal/pkg/configloader/config.go b/internal/pkg/configloader/config.go index a34e4f27d..b5b63eb18 100644 --- a/internal/pkg/configloader/config.go +++ b/internal/pkg/configloader/config.go @@ -3,7 +3,7 @@ package configloader import ( "bytes" "fmt" - "io/ioutil" + "os" "runtime" "strings" @@ -98,7 +98,7 @@ func renderToolsFromCoreConfigAndConfigBytes(coreConfig *CoreConfig, toolsConfig } func loadOriginalConfigFile(configFile string) ([]byte, error) { - originalConfigFileBytes, err := ioutil.ReadFile(configFile) + originalConfigFileBytes, err := os.ReadFile(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?") @@ -165,9 +165,10 @@ func (c *Config) ValidateDependency() []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 @@ -176,9 +177,9 @@ func (c *Config) ValidateDependency() []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. diff --git a/internal/pkg/configloader/toolconfig.go b/internal/pkg/configloader/toolconfig.go index 249d859ef..fe27a0084 100644 --- a/internal/pkg/configloader/toolconfig.go +++ b/internal/pkg/configloader/toolconfig.go @@ -2,7 +2,7 @@ package configloader import ( "fmt" - "io/ioutil" + "os" "gopkg.in/yaml.v3" @@ -106,7 +106,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 diff --git a/internal/pkg/plugin/argocdapp/options.go b/internal/pkg/plugin/argocdapp/options.go index 9381e6b62..ad2c5170a 100644 --- a/internal/pkg/plugin/argocdapp/options.go +++ b/internal/pkg/plugin/argocdapp/options.go @@ -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 { diff --git a/internal/pkg/plugininstaller/helm/installer.go b/internal/pkg/plugininstaller/helm/installer.go index f36d28d37..136db0684 100644 --- a/internal/pkg/plugininstaller/helm/installer.go +++ b/internal/pkg/plugininstaller/helm/installer.go @@ -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 { @@ -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 { diff --git a/pkg/util/file/file.go b/pkg/util/file/file.go index 9230deb1e..6d6073f17 100644 --- a/pkg/util/file/file.go +++ b/pkg/util/file/file.go @@ -2,7 +2,6 @@ package file import ( "fmt" - "io/ioutil" "os" "path/filepath" ) @@ -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) } diff --git a/pkg/util/file/file_test.go b/pkg/util/file/file_test.go index f9358daa2..4ef133461 100644 --- a/pkg/util/file/file_test.go +++ b/pkg/util/file/file_test.go @@ -2,7 +2,6 @@ package file import ( "errors" - "io/ioutil" "os" "path/filepath" @@ -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)) }) diff --git a/pkg/util/file/local_test.go b/pkg/util/file/local_test.go index 528463ae0..bc5d4ba61 100644 --- a/pkg/util/file/local_test.go +++ b/pkg/util/file/local_test.go @@ -1,7 +1,6 @@ package file import ( - "io/ioutil" "os" "path/filepath" @@ -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)) }) diff --git a/pkg/util/file/processer_test.go b/pkg/util/file/processer_test.go index 5bdcc7798..a82548628 100644 --- a/pkg/util/file/processer_test.go +++ b/pkg/util/file/processer_test.go @@ -4,7 +4,6 @@ import ( "archive/zip" "fmt" "io" - "io/ioutil" "os" "path/filepath" @@ -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)) diff --git a/pkg/util/file/remote_test.go b/pkg/util/file/remote_test.go index 09e1c6fbe..cdd5fa199 100644 --- a/pkg/util/file/remote_test.go +++ b/pkg/util/file/remote_test.go @@ -2,8 +2,8 @@ package file import ( "fmt" - "io/ioutil" "net/http" + "os" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" @@ -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)) }) diff --git a/pkg/util/file/render_test.go b/pkg/util/file/render_test.go index 8a1c62a0f..858a1de0a 100644 --- a/pkg/util/file/render_test.go +++ b/pkg/util/file/render_test.go @@ -2,7 +2,6 @@ package file import ( "fmt" - "io/ioutil" "os" "path/filepath" @@ -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()) }) @@ -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)) }) @@ -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) { @@ -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)) }) diff --git a/pkg/util/github/file.go b/pkg/util/github/file.go index 205064da5..acb56a88a 100644 --- a/pkg/util/github/file.go +++ b/pkg/util/github/file.go @@ -2,7 +2,7 @@ package github import ( "io/fs" - "io/ioutil" + "os" "path/filepath" "github.com/google/go-github/v42/github" @@ -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 } diff --git a/pkg/util/github/github_test.go b/pkg/util/github/github_test.go index 9f297d927..2ce9d985a 100644 --- a/pkg/util/github/github_test.go +++ b/pkg/util/github/github_test.go @@ -1,7 +1,7 @@ package github import ( - "io/ioutil" + "io" "net/http" "net/http/httptest" "net/url" @@ -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) } diff --git a/pkg/util/gitlab/file.go b/pkg/util/gitlab/file.go index e52cb88c9..0a859b5f6 100644 --- a/pkg/util/gitlab/file.go +++ b/pkg/util/gitlab/file.go @@ -2,7 +2,7 @@ package gitlab import ( "io/fs" - "io/ioutil" + "os" "path/filepath" "github.com/devstream-io/devstream/pkg/util/log" @@ -25,7 +25,7 @@ func (c *Client) PushLocalPathToBranch(repoPath, branch, pathWithNamespace, comm log.Debugf("Found file: %s.", path) - content, err := ioutil.ReadFile(path) + content, err := os.ReadFile(path) if err != nil { return err } diff --git a/pkg/util/k8s/state.go b/pkg/util/k8s/state.go index bfe1000fb..f775053b8 100644 --- a/pkg/util/k8s/state.go +++ b/pkg/util/k8s/state.go @@ -15,7 +15,7 @@ type AllResourceStatus struct { DaemonSet []ResourceStatus } -//GetResourceStatus get all resource state by input nameSpace and filtermap +// GetResourceStatus get all resource state by input nameSpace and filtermap func (c *Client) GetResourceStatus(nameSpace string, anFilter, labelFilter map[string]string) (AllResourceStatus, error) { stateMap := AllResourceStatus{} // 1. list deploy resource diff --git a/pkg/util/k8s/static.go b/pkg/util/k8s/static.go index 2a7b3679d..846039138 100644 --- a/pkg/util/k8s/static.go +++ b/pkg/util/k8s/static.go @@ -31,10 +31,10 @@ func (c *Client) ListDevstreamNamespace() (*corev1.NamespaceList, error) { // Check whether the given namespace is created by dtm // If the given namespace has label "created_by=DevStream", we'll control it. -// 1. The specified namespace is created by dtm, then it should be deleted -// when errors are encountered during creation or `dtm delete`. -// 2. The specified namespace is controlled by user, maybe they want to deploy plugins in -// an existing namespace or other situations, then we should not delete this namespace. +// 1. The specified namespace is created by dtm, then it should be deleted +// when errors are encountered during creation or `dtm delete`. +// 2. The specified namespace is controlled by user, maybe they want to deploy plugins in +// an existing namespace or other situations, then we should not delete this namespace. func (c *Client) IsDevstreamNS(namespace string) (bool, error) { nsList, err := c.ListDevstreamNamespace() if err != nil { diff --git a/pkg/util/md5/md5.go b/pkg/util/md5/md5.go index 602b4b572..9b4447d48 100644 --- a/pkg/util/md5/md5.go +++ b/pkg/util/md5/md5.go @@ -1,7 +1,7 @@ package md5 import ( - "io/ioutil" + "os" "k8s.io/utils/strings" ) @@ -15,7 +15,7 @@ func FileMatchesMD5(fileName, md5FileName string) (bool, error) { return false, err } - md5ContentBytes, err := ioutil.ReadFile(md5FileName) + md5ContentBytes, err := os.ReadFile(md5FileName) if err != nil { return false, err } diff --git a/pkg/util/template/render.go b/pkg/util/template/render.go index 71aacc93a..87c0b4d87 100644 --- a/pkg/util/template/render.go +++ b/pkg/util/template/render.go @@ -2,7 +2,7 @@ package template import ( "bytes" - "io/ioutil" + "os" "text/template" "github.com/devstream-io/devstream/pkg/util/log" @@ -28,7 +28,7 @@ func RenderForFile(name, tplFileName, dstFileName string, variable any) error { log.Debugf("Render config: %v.", variable) log.Debugf("Render output: %s.", dstFileName) - textBytes, err := ioutil.ReadFile(tplFileName) + textBytes, err := os.ReadFile(tplFileName) if err != nil { return err } @@ -38,5 +38,5 @@ func RenderForFile(name, tplFileName, dstFileName string, variable any) error { log.Debugf("render %s failed: %s", name, err) return err } - return ioutil.WriteFile(dstFileName, []byte(renderedStr), 0644) + return os.WriteFile(dstFileName, []byte(renderedStr), 0644) } From 227092e33471c1464c6c182927d25fa3c38161aa Mon Sep 17 00:00:00 2001 From: xinqshang Date: Tue, 9 Aug 2022 16:11:47 +0800 Subject: [PATCH 2/2] refactor: remove debug log Signed-off-by: xinqshang --- Makefile | 2 -- 1 file changed, 2 deletions(-) diff --git a/Makefile b/Makefile index 6e5c08970..db4fe7950 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,5 @@ SELF_DIR=$(dir $(lastword $(MAKEFILE_LIST))) -echo "SELF_DIR:$SELF_DIR" - GOOS=$(shell go env GOOS) GOPATH=$(shell go env GOPATH) GOARCH=$(shell go env GOARCH)