From 0aa0c602cfe7749e972aba4357f66235d8662a7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20R=C3=BCger?= Date: Mon, 31 Mar 2025 18:51:24 +0200 Subject: [PATCH 1/3] chore: Bump golangci-lint to v2 --- .github/workflows/lint.yml | 19 +------------------ .golangci.yaml | 20 +++++++++++++++++++- command_test.go | 18 +++++++++--------- fish.go | 28 +++++++++++++++------------- help.go | 4 ++-- 5 files changed, 46 insertions(+), 43 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 880792927c..8cf5bb8e48 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -34,23 +34,6 @@ jobs: echo "$non_formatted_files" test -z "$non_formatted_files" - staticcheck: - runs-on: ubuntu-latest - steps: - - name: Set up Go - uses: actions/setup-go@v5 - with: - go-version: stable - - - name: Clone repository - uses: actions/checkout@v4 - - - name: Set up staticcheck - run: go install honnef.co/go/tools/cmd/staticcheck@latest - - - name: Run staticcheck - run: staticcheck ./... - golangci-lint: runs-on: ubuntu-latest @@ -64,6 +47,6 @@ jobs: uses: actions/checkout@v4 - name: Run golangci-lint - uses: golangci/golangci-lint-action@v6 + uses: golangci/golangci-lint-action@v7 with: version: latest diff --git a/.golangci.yaml b/.golangci.yaml index a6e8c7e9d5..846c6fa371 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -1,5 +1,23 @@ -# https://golangci-lint.run/usage/configuration/ +version: "2" linters: enable: - makezero - misspell + exclusions: + generated: lax + presets: + - comments + - common-false-positives + - legacy + - std-error-handling + paths: + - third_party$ + - builtin$ + - examples$ +formatters: + exclusions: + generated: lax + paths: + - third_party$ + - builtin$ + - examples$ diff --git a/command_test.go b/command_test.go index fe7c69e552..de1b06aa5c 100644 --- a/command_test.go +++ b/command_test.go @@ -2760,7 +2760,7 @@ func TestFlagAction(t *testing.T) { if v[0] == "err" { return fmt.Errorf("error string slice") } - _, err := cmd.Root().Writer.Write([]byte(fmt.Sprintf("%v ", v))) + _, err := fmt.Fprintf(cmd.Root().Writer, "%v ", v) return err }, }, @@ -2771,7 +2771,7 @@ func TestFlagAction(t *testing.T) { if !v { return fmt.Errorf("value is false") } - _, err := cmd.Root().Writer.Write([]byte(fmt.Sprintf("%t ", v))) + _, err := fmt.Fprintf(cmd.Root().Writer, "%t ", v) return err }, }, @@ -2782,7 +2782,7 @@ func TestFlagAction(t *testing.T) { if v == 0 { return fmt.Errorf("empty duration") } - _, err := cmd.Root().Writer.Write([]byte(v.String() + " ")) + _, err := fmt.Fprintf(cmd.Root().Writer, v.String()+" ") return err }, }, @@ -2793,7 +2793,7 @@ func TestFlagAction(t *testing.T) { if v < 0 { return fmt.Errorf("negative float64") } - _, err := cmd.Root().Writer.Write([]byte(strconv.FormatFloat(v, 'f', -1, 64) + " ")) + _, err := fmt.Fprintf(cmd.Root().Writer, strconv.FormatFloat(v, 'f', -1, 64)+" ") return err }, }, @@ -2804,7 +2804,7 @@ func TestFlagAction(t *testing.T) { if len(v) > 0 && v[0] < 0 { return fmt.Errorf("invalid float64 slice") } - _, err := cmd.Root().Writer.Write([]byte(fmt.Sprintf("%v ", v))) + _, err := fmt.Fprintf(cmd.Root().Writer, "%v ", v) return err }, }, @@ -2815,7 +2815,7 @@ func TestFlagAction(t *testing.T) { if v < 0 { return fmt.Errorf("negative int") } - _, err := cmd.Root().Writer.Write([]byte(fmt.Sprintf("%v ", v))) + _, err := fmt.Fprintf(cmd.Root().Writer, "%v ", v) return err }, }, @@ -2826,7 +2826,7 @@ func TestFlagAction(t *testing.T) { if len(v) > 0 && v[0] < 0 { return fmt.Errorf("invalid int slice") } - _, err := cmd.Root().Writer.Write([]byte(fmt.Sprintf("%v ", v))) + _, err := fmt.Fprintf(cmd.Root().Writer, "%v ", v) return err }, }, @@ -2853,7 +2853,7 @@ func TestFlagAction(t *testing.T) { if v == 0 { return fmt.Errorf("zero uint64") } - _, err := cmd.Root().Writer.Write([]byte(fmt.Sprintf("%v ", v))) + _, err := fmt.Fprintf(cmd.Root().Writer, "%v ", v) return err }, }, @@ -2864,7 +2864,7 @@ func TestFlagAction(t *testing.T) { if _, ok := v["err"]; ok { return fmt.Errorf("error string map") } - _, err := cmd.Root().Writer.Write([]byte(fmt.Sprintf("%v", v))) + _, err := fmt.Fprintf(cmd.Root().Writer, "%v", v) return err }, }, diff --git a/fish.go b/fish.go index c807dcc7e5..5a952671e9 100644 --- a/fish.go +++ b/fish.go @@ -68,16 +68,17 @@ func (cmd *Command) prepareFishCommands(commands []*Command, allCommands *[]stri completions := []string{} for _, command := range commands { var completion strings.Builder - completion.WriteString(fmt.Sprintf( + fmt.Fprintf(&completion, "complete -r -c %s -n '%s' -a '%s'", cmd.Name, cmd.fishSubcommandHelper(previousCommands), strings.Join(command.Names(), " "), - )) + ) if command.Usage != "" { - completion.WriteString(fmt.Sprintf(" -d '%s'", - escapeSingleQuotes(command.Usage))) + fmt.Fprintf(&completion, + " -d '%s'", + escapeSingleQuotes(command.Usage)) } if !command.HideHelp { @@ -112,23 +113,23 @@ func (cmd *Command) prepareFishFlags(flags []Flag, previousCommands []string) [] completions := []string{} for _, f := range flags { completion := &strings.Builder{} - completion.WriteString(fmt.Sprintf( + fmt.Fprintf(completion, "complete -c %s -n '%s'", cmd.Name, cmd.fishSubcommandHelper(previousCommands), - )) + ) fishAddFileFlag(f, completion) for idx, opt := range f.Names() { if idx == 0 { - completion.WriteString(fmt.Sprintf( + fmt.Fprintf(completion, " -l %s", strings.TrimSpace(opt), - )) + ) } else { - completion.WriteString(fmt.Sprintf( + fmt.Fprintf(completion, " -s %s", strings.TrimSpace(opt), - )) + ) } } @@ -138,8 +139,9 @@ func (cmd *Command) prepareFishFlags(flags []Flag, previousCommands []string) [] } if flag.GetUsage() != "" { - completion.WriteString(fmt.Sprintf(" -d '%s'", - escapeSingleQuotes(flag.GetUsage()))) + fmt.Fprintf(completion, + " -d '%s'", + escapeSingleQuotes(flag.GetUsage())) } } @@ -175,5 +177,5 @@ func (cmd *Command) fishSubcommandHelper(allCommands []string) string { } func escapeSingleQuotes(input string) string { - return strings.Replace(input, `'`, `\'`, -1) + return strings.ReplaceAll(input, `'`, `\'`) } diff --git a/help.go b/help.go index c935e41993..c039a5d02b 100644 --- a/help.go +++ b/help.go @@ -425,7 +425,7 @@ func printHelpCustom(out io.Writer, templ string, data interface{}, customFuncs handleTemplateError(err) } - if _, err := t.New("visibleGlobalFlagCategoryTemplate").Parse(strings.Replace(visibleFlagCategoryTemplate, "OPTIONS", "GLOBAL OPTIONS", -1)); err != nil { + if _, err := t.New("visibleGlobalFlagCategoryTemplate").Parse(strings.ReplaceAll(visibleFlagCategoryTemplate, "OPTIONS", "GLOBAL OPTIONS")); err != nil { handleTemplateError(err) } @@ -513,7 +513,7 @@ func subtract(a, b int) int { func indent(spaces int, v string) string { pad := strings.Repeat(" ", spaces) - return pad + strings.Replace(v, "\n", "\n"+pad, -1) + return pad + strings.ReplaceAll(v, "\n", "\n"+pad) } func nindent(spaces int, v string) string { From a617630955defc3860da00fa2609a571fdfc1b42 Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Wed, 2 Apr 2025 09:55:33 +0800 Subject: [PATCH 2/3] Stricter `.golangci.yaml` configuration Signed-off-by: Eng Zer Jun --- .golangci.yaml | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/.golangci.yaml b/.golangci.yaml index 846c6fa371..18b70c8b0e 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -4,20 +4,5 @@ linters: - makezero - misspell exclusions: - generated: lax presets: - - comments - - common-false-positives - - legacy - std-error-handling - paths: - - third_party$ - - builtin$ - - examples$ -formatters: - exclusions: - generated: lax - paths: - - third_party$ - - builtin$ - - examples$ From 18dbf80251bbce1dbccd76743b7596f35a977c0d Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Wed, 2 Apr 2025 10:02:14 +0800 Subject: [PATCH 3/3] ci: Run `actions/setup-go` after `actions/checkout` `actions/setup-go` uses `go.sum` file as the cache key. The CI needs to clone the repository first in order to restore the cache properly. Signed-off-by: Eng Zer Jun --- .github/workflows/lint.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 8cf5bb8e48..65a9683c30 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -17,14 +17,14 @@ jobs: format: runs-on: ubuntu-latest steps: + - name: Clone repository + uses: actions/checkout@v4 + - name: Set up Go uses: actions/setup-go@v5 with: go-version: stable - - name: Clone repository - uses: actions/checkout@v4 - - name: Set up gofumpt run: go install mvdan.cc/gofumpt@latest @@ -38,14 +38,14 @@ jobs: runs-on: ubuntu-latest steps: + - name: Clone repository + uses: actions/checkout@v4 + - name: Set up Go uses: actions/setup-go@v5 with: go-version: stable - - name: Clone repository - uses: actions/checkout@v4 - - name: Run golangci-lint uses: golangci/golangci-lint-action@v7 with: