diff --git a/.github/workflows/e2e-test.yaml b/.github/workflows/e2e-test.yaml index 303b9ea3..66344bdf 100644 --- a/.github/workflows/e2e-test.yaml +++ b/.github/workflows/e2e-test.yaml @@ -16,7 +16,7 @@ on: env: # Common versions - GO_VERSION: '1.18' + GO_VERSION: '1.19' jobs: kind: diff --git a/.github/workflows/go.yaml b/.github/workflows/go.yaml index 017348fa..deecb614 100644 --- a/.github/workflows/go.yaml +++ b/.github/workflows/go.yaml @@ -13,16 +13,19 @@ on: - '**.go' - '**.yaml' - '**.mod' - +permissions: + contents: read + # Optional: allow read access to pull request. Use with `only-new-issues` option. + # pull-requests: read jobs: test: name: test runs-on: ubuntu-latest steps: - - name: Set up Go 1.18 + - name: Set up Go 1.19 uses: actions/setup-go@v4 with: - go-version: 1.18 + go-version: 1.19 id: go - name: Check out code into the Go module directory @@ -31,6 +34,11 @@ jobs: - name: Run test run: make test + - name: golangci-lint + uses: golangci/golangci-lint-action@v3 + with: + version: v1.52.2 + - name: Upload coverage report uses: codecov/codecov-action@v3.1.2 with: diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index c8af5bd0..ee7973ad 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -24,7 +24,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v4 with: - go-version: 1.18 + go-version: 1.19 - name: Run GoReleaser uses: goreleaser/goreleaser-action@v4 with: diff --git a/Makefile b/Makefile index 2610326c..6924f8e7 100644 --- a/Makefile +++ b/Makefile @@ -69,7 +69,7 @@ vet: lint: golangci $(GOLANGCILINT) run ./... -test: fmt vet lint +test: fmt vet go test -race -coverprofile=coverage.txt -covermode=atomic ./cmd/... doc-gen: @@ -81,7 +81,7 @@ endif doc-run: docsify serve docs -GOLANGCILINT_VERSION ?= v1.46.2 +GOLANGCILINT_VERSION ?= v1.52.2 HOSTOS := $(shell uname -s | tr '[:upper:]' '[:lower:]') HOSTARCH := $(shell uname -m) ifeq ($(HOSTARCH),x86_64) diff --git a/cmd/add.go b/cmd/add.go index 6097f8ca..749e5bd6 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -3,7 +3,7 @@ package cmd import ( "errors" "fmt" - "io/ioutil" + "io" "os" "strconv" @@ -49,7 +49,7 @@ func (ac *AddCommand) runAdd(cmd *cobra.Command, args []string) error { if file == "-" { // from stdin - contents, err := ioutil.ReadAll(os.Stdin) + contents, err := io.ReadAll(os.Stdin) if err != nil { return err } diff --git a/cmd/alias.go b/cmd/alias.go index af404598..05a796bf 100644 --- a/cmd/alias.go +++ b/cmd/alias.go @@ -3,7 +3,6 @@ package cmd import ( "bufio" "fmt" - "io/ioutil" "os" "path/filepath" "strings" @@ -34,6 +33,7 @@ func (al *AliasCommand) Init() { _ = al.command.MarkFlagRequired("out") } +// SourceCmd source command const SourceCmd = "[[ ! -f ~/.kubecm ]] || source ~/.kubecm" func (al *AliasCommand) runAlias(command *cobra.Command, args []string) error { @@ -79,7 +79,7 @@ alias %s='kubectl --context %s'` } func updateFile(cxt, path string) error { - err := ioutil.WriteFile(path, []byte(cxt), 0644) + err := os.WriteFile(path, []byte(cxt), 0644) if err != nil { return err } diff --git a/cmd/alias_test.go b/cmd/alias_test.go index 37401ac4..08764802 100644 --- a/cmd/alias_test.go +++ b/cmd/alias_test.go @@ -1,14 +1,16 @@ package cmd import ( - "io/ioutil" "os" "testing" ) func Test_updateFile(t *testing.T) { - testFile, _ := ioutil.TempFile("", "") - defer os.Remove(testFile.Name()) + temp, err := os.CreateTemp("", "") + if err != nil { + return + } + defer os.Remove(temp.Name()) type args struct { cxt string path string @@ -23,7 +25,7 @@ func Test_updateFile(t *testing.T) { name: "test", args: args{ cxt: "test", - path: testFile.Name(), + path: temp.Name(), }, }, } @@ -44,7 +46,7 @@ func Test_updateFile(t *testing.T) { } func Test_writeAppend(t *testing.T) { - bashFile, _ := ioutil.TempFile("", ".bash") + bashFile, _ := os.CreateTemp("", ".bash") defer os.Remove(bashFile.Name()) type args struct { context string diff --git a/cmd/clear_test.go b/cmd/clear_test.go index 05196ea4..571d0eb3 100644 --- a/cmd/clear_test.go +++ b/cmd/clear_test.go @@ -1,7 +1,6 @@ package cmd import ( - "io/ioutil" "os" "testing" @@ -9,8 +8,8 @@ import ( ) func Test_clearContext(t *testing.T) { - trueFile, _ := ioutil.TempFile("", "") - falseFile, _ := ioutil.TempFile("", "") + trueFile, _ := os.CreateTemp("", "") + falseFile, _ := os.CreateTemp("", "") defer os.Remove(trueFile.Name()) defer os.Remove(falseFile.Name()) _ = clientcmd.WriteToFile(appendMergeConfig, trueFile.Name()) diff --git a/cmd/merge.go b/cmd/merge.go index c20fd8d9..900d9a3a 100644 --- a/cmd/merge.go +++ b/cmd/merge.go @@ -2,7 +2,6 @@ package cmd import ( "fmt" - "io/ioutil" "os" "strconv" @@ -89,7 +88,7 @@ func loadKubeConfig(yaml string) (*clientcmdapi.Config, error) { } func listFile(folder string) []string { - files, _ := ioutil.ReadDir(folder) + files, _ := os.ReadDir(folder) var fileList []string for _, file := range files { if file.Name() == ".DS_Store" { diff --git a/cmd/merge_test.go b/cmd/merge_test.go index 9c1ca35f..cc3628bf 100644 --- a/cmd/merge_test.go +++ b/cmd/merge_test.go @@ -1,7 +1,6 @@ package cmd import ( - "io/ioutil" "os" "path/filepath" "reflect" @@ -29,23 +28,23 @@ var ( ) func Test_listFile(t *testing.T) { - tempDir, err := ioutil.TempDir("", t.Name()) + temp, err := os.MkdirTemp("", t.Name()) if err != nil { t.Fatalf("TempDir %s: %v", t.Name(), err) } - defer os.RemoveAll(tempDir) - filename1 := filepath.Join(tempDir, "config1") - filename2 := filepath.Join(tempDir, "config2") - dsStore := filepath.Join(tempDir, ".DS_Store") - err = ioutil.WriteFile(filename1, []byte("shmorp"), 0444) + defer os.RemoveAll(temp) + filename1 := filepath.Join(temp, "config1") + filename2 := filepath.Join(temp, "config2") + dsStore := filepath.Join(temp, ".DS_Store") + err = os.WriteFile(filename1, []byte("shmorp"), 0444) if err != nil { t.Fatalf("WriteFile %s: %v", filename1, err) } - err = ioutil.WriteFile(filename2, []byte("florp"), 0444) + err = os.WriteFile(filename2, []byte("florp"), 0444) if err != nil { t.Fatalf("WriteFile %s: %v", filename2, err) } - err = ioutil.WriteFile(dsStore, []byte("xxxx"), 0444) + err = os.WriteFile(dsStore, []byte("xxxx"), 0444) if err != nil { t.Fatalf("WriteFile %s: %v", filename2, err) } @@ -59,7 +58,7 @@ func Test_listFile(t *testing.T) { want []string }{ // TODO: Add test cases. - {"testDir", args{folder: tempDir}, []string{filename1, filename2}}, + {"testDir", args{folder: temp}, []string{filename1, filename2}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -71,19 +70,22 @@ func Test_listFile(t *testing.T) { } func Test_loadKubeConfig(t *testing.T) { - tempDir, err := ioutil.TempDir("", t.Name()) + temp, err := os.MkdirTemp("", t.Name()) + if err != nil { + return + } if err != nil { t.Fatalf("TempDir %s: %v", t.Name(), err) } - defer os.RemoveAll(tempDir) + defer os.RemoveAll(temp) - merge1 := filepath.Join(tempDir, "merge1") + merge1 := filepath.Join(temp, "merge1") err = clientcmd.WriteToFile(mergeTestConfig, merge1) if err != nil { t.Fatalf("WriteFile %s: %v", merge1, err) } - mergeFail := filepath.Join(tempDir, "config2") - err = ioutil.WriteFile(mergeFail, []byte("florp"), 0444) + mergeFail := filepath.Join(temp, "config2") + err = os.WriteFile(mergeFail, []byte("florp"), 0444) if err != nil { t.Fatalf("WriteFile %s: %v", mergeFail, err) } diff --git a/cmd/root.go b/cmd/root.go index 29062c09..8bec0aa3 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -43,7 +43,7 @@ type Cli struct { rootCmd *cobra.Command } -//NewCli returns the cli instance used to register and execute command +// NewCli returns the cli instance used to register and execute command func NewCli() *Cli { cli := &Cli{ rootCmd: &cobra.Command{ diff --git a/cmd/utils_test.go b/cmd/utils_test.go index e93ac51b..a5d2ccf3 100644 --- a/cmd/utils_test.go +++ b/cmd/utils_test.go @@ -4,7 +4,6 @@ import ( "bytes" "errors" "fmt" - "io/ioutil" "os" "os/user" "reflect" @@ -257,10 +256,10 @@ func checkConfig(want, got *clientcmdapi.Config, t *testing.T) { } func Test_getFileName(t *testing.T) { - tempDir, _ := ioutil.TempDir("", "kubecm-get-file-") - defer os.RemoveAll(tempDir) - tempFilePath := fmt.Sprintf("%s/%s", tempDir, "testPath") - _ = ioutil.WriteFile(tempFilePath, []byte{}, 0666) + temp, _ := os.CreateTemp("", "kubecm-get-file-") + defer os.RemoveAll(temp.Name()) + tempFilePath := fmt.Sprintf("%s/%s", temp.Name(), "testPath") + _ = os.WriteFile(tempFilePath, []byte{}, 0666) type args struct { path string diff --git a/go.mod b/go.mod index 83367147..5c9eb979 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/sunny0826/kubecm -go 1.18 +go 1.19 require ( github.com/alibabacloud-go/cs-20151215/v2 v2.4.5 diff --git a/main.go b/main.go index e798c501..120993cc 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 + http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, diff --git a/pkg/update/update.go b/pkg/update/update.go index 5602bbdf..13bc44de 100644 --- a/pkg/update/update.go +++ b/pkg/update/update.go @@ -3,7 +3,7 @@ package update import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "strings" "time" @@ -42,7 +42,7 @@ func getLatestReleaseInfo(repo string) (*ReleaseInfo, error) { } defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return nil, err }