diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 880792927c..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 @@ -34,36 +34,19 @@ jobs: echo "$non_formatted_files" test -z "$non_formatted_files" - staticcheck: + golangci-lint: runs-on: ubuntu-latest - steps: - - name: Set up Go - uses: actions/setup-go@v5 - with: - go-version: stable + steps: - 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 - - steps: - 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@v6 + uses: golangci/golangci-lint-action@v7 with: version: latest diff --git a/.golangci.yaml b/.golangci.yaml index a6e8c7e9d5..18b70c8b0e 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -1,5 +1,8 @@ -# https://golangci-lint.run/usage/configuration/ +version: "2" linters: enable: - makezero - misspell + exclusions: + presets: + - std-error-handling 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 {