Skip to content

Commit 5aa0e69

Browse files
appleboyclaude
andauthored
fix(lint): resolve all 49 golangci-lint issues (#262)
* fix(lint): resolve all 49 golangci-lint issues - Upgrade golangci-lint config to v2 with stricter linters - Replace interface{} with any and use maps.Copy, strings.SplitSeq - Use errors.New for static error strings instead of fmt.Errorf - Use http.StatusOK and http.MethodGet stdlib constants in tests - Replace os.Chdir/os.Setenv with t.Chdir/t.Setenv in tests - Add required explanations to all nolint directives - Replace fmt.Printf with proper error returns in anthropic provider Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix(lint): address PR review feedback - Wrap original error with %w in anthropic APIError branches - Add trailing newline to color.White version output - Quote file path in test shell command with fmt.Sprintf %q Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix(lint): resolve windows-specific lint issues - Use errors.New for static error strings in api_key_helper_windows.go - Modernize for loops to range over int in windows test Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 7e32b99 commit 5aa0e69

21 files changed

Lines changed: 180 additions & 115 deletions

.golangci.yml

Lines changed: 85 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,114 @@
11
version: "2"
2+
output:
3+
sort-order:
4+
- file
25
linters:
36
default: none
47
enable:
8+
- bidichk
59
- bodyclose
6-
- dogsled
7-
- dupl
10+
- depguard
811
- errcheck
9-
- exhaustive
10-
- goconst
12+
- forbidigo
13+
- gocheckcompilerdirectives
1114
- gocritic
12-
- gocyclo
13-
- goprintffuncname
14-
- gosec
1515
- govet
1616
- ineffassign
17-
- misspell
17+
- mirror
18+
- modernize
1819
- nakedret
19-
- noctx
20+
- nilnil
2021
- nolintlint
21-
- rowserrcheck
22+
- perfsprint
23+
- revive
2224
- staticcheck
25+
- testifylint
2326
- unconvert
2427
- unparam
2528
- unused
26-
- whitespace
29+
- usestdlibvars
30+
- usetesting
31+
- wastedassign
32+
settings:
33+
depguard:
34+
rules:
35+
main:
36+
deny:
37+
- pkg: io/ioutil
38+
desc: use os or io instead
39+
- pkg: golang.org/x/exp
40+
desc: it's experimental and unreliable
41+
- pkg: github.com/pkg/errors
42+
desc: use builtin errors package instead
43+
nolintlint:
44+
allow-unused: false
45+
require-explanation: true
46+
require-specific: true
47+
gocritic:
48+
enabled-checks:
49+
- equalFold
50+
disabled-checks: []
51+
revive:
52+
severity: error
53+
rules:
54+
- name: blank-imports
55+
- name: constant-logical-expr
56+
- name: context-as-argument
57+
- name: context-keys-type
58+
- name: dot-imports
59+
- name: empty-lines
60+
- name: error-return
61+
- name: error-strings
62+
- name: exported
63+
- name: identical-branches
64+
- name: if-return
65+
- name: increment-decrement
66+
- name: modifies-value-receiver
67+
- name: package-comments
68+
- name: redefines-builtin-id
69+
- name: superfluous-else
70+
- name: time-naming
71+
- name: unexported-return
72+
- name: var-declaration
73+
- name: var-naming
74+
disabled: true
75+
staticcheck:
76+
checks:
77+
- all
78+
testifylint: {}
79+
usetesting:
80+
os-temp-dir: true
81+
perfsprint:
82+
concat-loop: false
83+
govet:
84+
enable:
85+
- nilness
86+
- unusedwrite
2787
exclusions:
2888
generated: lax
2989
presets:
3090
- comments
3191
- common-false-positives
3292
- legacy
3393
- std-error-handling
34-
paths:
35-
- third_party$
36-
- builtin$
37-
- examples$
94+
rules:
95+
- linters:
96+
- errcheck
97+
- staticcheck
98+
- unparam
99+
path: _test\.go
100+
issues:
101+
max-issues-per-linter: 0
102+
max-same-issues: 0
38103
formatters:
39104
enable:
40105
- gofmt
41106
- gofumpt
42-
- goimports
43107
- golines
108+
settings:
109+
gofumpt:
110+
extra-rules: true
44111
exclusions:
45112
generated: lax
46-
paths:
47-
- third_party$
48-
- builtin$
49-
- examples$
113+
run:
114+
timeout: 10m

cmd/commit.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"html"
77
"io"
8+
"maps"
89
"os"
910
"path"
1011
"strings"
@@ -136,9 +137,7 @@ var commitCmd = &cobra.Command{
136137
data := util.Data{}
137138
// Add template variables
138139
if vars := util.ConvertToMap(templateVars); len(vars) > 0 {
139-
for k, v := range vars {
140-
data[k] = v
141-
}
140+
maps.Copy(data, vars)
142141
}
143142

144143
// Add template variables from file

cmd/textarea.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func initialPrompt(value string) model {
3232
ti.InsertString(value)
3333

3434
maxWidth := 0
35-
for _, line := range strings.Split(value, "\n") {
35+
for line := range strings.SplitSeq(value, "\n") {
3636
if len(line) > maxWidth {
3737
maxWidth = len(line)
3838
}
@@ -57,7 +57,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
5757

5858
switch msg := msg.(type) {
5959
case tea.KeyMsg:
60-
switch msg.Type { //nolint:exhaustive
60+
switch msg.Type { //nolint:exhaustive // only handling specific key types
6161
case tea.KeyEsc:
6262
if m.textarea.Focused() {
6363
m.textarea.Blur()

cmd/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func printVersion(format string, v VersionInfo) error {
8989
// Print each row with colored label and default value color
9090
for _, row := range rows {
9191
blue.Print(row[0])
92-
fmt.Printf(" %s\n", row[1])
92+
color.White(" %s\n", row[1])
9393
}
9494
return nil
9595
}

core/transport/transport_test.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (m *mockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)
2020
if m.resp != nil || m.err != nil {
2121
return m.resp, m.err
2222
}
23-
return &http.Response{StatusCode: 200, Body: http.NoBody, Request: req}, nil
23+
return &http.Response{StatusCode: http.StatusOK, Body: http.NoBody, Request: req}, nil
2424
}
2525

2626
func TestDefaultHeaderTransport_CustomHeaders(t *testing.T) {
@@ -34,7 +34,12 @@ func TestDefaultHeaderTransport_CustomHeaders(t *testing.T) {
3434
AppName: "myapp",
3535
AppVersion: "1.2.3",
3636
}
37-
req, _ := http.NewRequestWithContext(context.Background(), "GET", "http://example.com", nil)
37+
req, _ := http.NewRequestWithContext(
38+
context.Background(),
39+
http.MethodGet,
40+
"http://example.com",
41+
nil,
42+
)
3843
resp, err := tr.RoundTrip(req)
3944
if err != nil {
4045
t.Fatalf("RoundTrip error: %v", err)
@@ -65,7 +70,12 @@ func TestDefaultHeaderTransport_EmptyHeadersAndAppInfo(t *testing.T) {
6570
AppName: "",
6671
AppVersion: "",
6772
}
68-
req, _ := http.NewRequestWithContext(context.Background(), "GET", "http://example.com", nil)
73+
req, _ := http.NewRequestWithContext(
74+
context.Background(),
75+
http.MethodGet,
76+
"http://example.com",
77+
nil,
78+
)
6979
resp, err := tr.RoundTrip(req)
7080
if err != nil {
7181
t.Fatalf("RoundTrip error: %v", err)
@@ -91,7 +101,12 @@ func TestDefaultHeaderTransport_OriginErrorPropagation(t *testing.T) {
91101
AppName: "",
92102
AppVersion: "",
93103
}
94-
req, _ := http.NewRequestWithContext(context.Background(), "GET", "http://example.com", nil)
104+
req, _ := http.NewRequestWithContext(
105+
context.Background(),
106+
http.MethodGet,
107+
"http://example.com",
108+
nil,
109+
)
95110
resp, err := tr.RoundTrip(req)
96111
if resp != nil && resp.Body != nil {
97112
resp.Body.Close()
@@ -111,7 +126,12 @@ func TestDefaultHeaderTransport_MultipleHeaderValues(t *testing.T) {
111126
AppName: "app",
112127
AppVersion: "v",
113128
}
114-
req, _ := http.NewRequestWithContext(context.Background(), "GET", "http://example.com", nil)
129+
req, _ := http.NewRequestWithContext(
130+
context.Background(),
131+
http.MethodGet,
132+
"http://example.com",
133+
nil,
134+
)
115135
resp, err := tr.RoundTrip(req)
116136
if err != nil {
117137
t.Fatalf("RoundTrip error: %v", err)

git/git.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package git
33
import (
44
"context"
55
"errors"
6-
"fmt"
76
"os"
87
"os/exec"
98
"path"
@@ -138,7 +137,7 @@ func (c *Command) commit(ctx context.Context, val string) *exec.Cmd {
138137
"commit",
139138
"--no-verify",
140139
"--signoff",
141-
fmt.Sprintf("--message=%s", val),
140+
"--message=" + val,
142141
}
143142

144143
if c.isAmend {
@@ -234,7 +233,11 @@ func (c *Command) InstallHook(ctx context.Context) error {
234233
}
235234

236235
// Write the hook file with executable permissions (0o755)
237-
return os.WriteFile(target, content, 0o755) //nolint:gosec
236+
return os.WriteFile(
237+
target,
238+
content,
239+
0o755,
240+
) //nolint:gosec // hook file needs executable permissions
238241
}
239242

240243
// UninstallHook removes the prepare-commit-msg hook if it exists.

git/git_test.go

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,12 @@ func TestCanExecuteGitDiff(t *testing.T) {
2525
tmpDir := t.TempDir()
2626

2727
// Change to the temporary directory
28-
originalDir, err := os.Getwd()
29-
if err != nil {
30-
t.Fatalf("Failed to get current directory: %v", err)
31-
}
32-
defer func() {
33-
if err := os.Chdir(originalDir); err != nil {
34-
t.Logf("Failed to restore directory: %v", err)
35-
}
36-
}()
37-
38-
if err := os.Chdir(tmpDir); err != nil {
39-
t.Fatalf("Failed to change directory: %v", err)
40-
}
28+
t.Chdir(tmpDir)
4129

4230
cmd := New()
4331
ctx := context.Background()
4432

45-
err = cmd.CanExecuteGitDiff(ctx)
33+
err := cmd.CanExecuteGitDiff(ctx)
4634
if err == nil {
4735
t.Error("CanExecuteGitDiff() should fail in a non-git directory")
4836
}
@@ -63,24 +51,12 @@ func TestCanExecuteGitDiff(t *testing.T) {
6351
defer os.RemoveAll(tmpDir)
6452

6553
// Change to the subdirectory
66-
originalDir, err := os.Getwd()
67-
if err != nil {
68-
t.Fatalf("Failed to get current directory: %v", err)
69-
}
70-
defer func() {
71-
if err := os.Chdir(originalDir); err != nil {
72-
t.Logf("Failed to restore directory: %v", err)
73-
}
74-
}()
75-
76-
if err := os.Chdir(tmpDir); err != nil {
77-
t.Fatalf("Failed to change directory: %v", err)
78-
}
54+
t.Chdir(tmpDir)
7955

8056
cmd := New()
8157
ctx := context.Background()
8258

83-
err = cmd.CanExecuteGitDiff(ctx)
59+
err := cmd.CanExecuteGitDiff(ctx)
8460
if err != nil {
8561
t.Errorf(
8662
"CanExecuteGitDiff() should succeed in a git repository subdirectory, got error: %v",

git/hook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const (
2626

2727
// init initializes the Git hook templates by loading them from embedded files.
2828
// If there's an error loading the templates, the function logs a fatal error and terminates the program.
29-
func init() { //nolint:gochecknoinits
29+
func init() { //nolint:gochecknoinits // required to load embedded templates at startup
3030
if err := util.LoadTemplates(files); err != nil {
3131
log.Fatal(err)
3232
}

prompt/prompt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const (
2323
)
2424

2525
// Initializes the prompt package by loading the templates from the embedded file system.
26-
func init() { //nolint:gochecknoinits
26+
func init() { //nolint:gochecknoinits // required to load embedded templates at startup
2727
if err := util.LoadTemplates(templatesFS); err != nil {
2828
log.Fatal(err)
2929
}

provider/anthropic/anthropic.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,14 @@ func (c *Client) Completion(ctx context.Context, content string) (*core.Response
4242
if err != nil {
4343
var e *anthropic.APIError
4444
if errors.As(err, &e) {
45-
fmt.Printf("Messages error, type: %s, message: %s", e.Type, e.Message)
46-
} else {
47-
fmt.Printf("Messages error: %v\n", err)
45+
return nil, fmt.Errorf(
46+
"messages error, type: %s, message: %s: %w",
47+
e.Type,
48+
e.Message,
49+
err,
50+
)
4851
}
49-
return nil, err
52+
return nil, fmt.Errorf("messages error: %w", err)
5053
}
5154

5255
usage := core.Usage{
@@ -97,11 +100,14 @@ func (c *Client) CompletionStream(
97100
if err != nil {
98101
var e *anthropic.APIError
99102
if errors.As(err, &e) {
100-
fmt.Printf("Messages error, type: %s, message: %s", e.Type, e.Message)
101-
} else {
102-
fmt.Printf("Messages error: %v\n", err)
103+
return nil, fmt.Errorf(
104+
"messages error, type: %s, message: %s: %w",
105+
e.Type,
106+
e.Message,
107+
err,
108+
)
103109
}
104-
return nil, err
110+
return nil, fmt.Errorf("messages error: %w", err)
105111
}
106112

107113
if writeErr != nil {

0 commit comments

Comments
 (0)