Skip to content

Commit e207d61

Browse files
magifd2claude
andcommitted
fix: resolve golangci-lint errcheck and staticcheck warnings
- Use _, _ = fmt.Fprintf(...) for diagnostic writes to stderr - Use defer func() { _ = resp.Body.Close() }() for HTTP response bodies - Use _ = json.NewEncoder/r.ParseForm in test handlers - Remove unused newTestClient helper from client_test.go - Lowercase error string in requireHost() (staticcheck ST1005) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 4c43bda commit e207d61

5 files changed

Lines changed: 18 additions & 38 deletions

File tree

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func promptForCredentials() error {
139139
// requireHost returns an error if no host is configured.
140140
func requireHost() error {
141141
if cfg.Host == "" {
142-
return fmt.Errorf("Splunk host is required (--host, SPLUNK_HOST env var, or host in config file)")
142+
return fmt.Errorf("splunk host is required (--host, SPLUNK_HOST env var, or host in config file)")
143143
}
144144
return nil
145145
}

cmd/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func readFromTTY() string {
129129
if runtime.GOOS != "windows" {
130130
tty, err := os.Open("/dev/tty")
131131
if err == nil {
132-
defer tty.Close()
132+
defer func() { _ = tty.Close() }()
133133
r = tty
134134
}
135135
}

internal/client/client.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ func New(cfg *config.Config, silent bool) (*Client, error) {
7676
// Logf writes to stderr unless silent.
7777
func (c *Client) Logf(format string, a ...any) {
7878
if !c.silent {
79-
fmt.Fprintf(c.stderr, format, a...)
79+
_, _ = fmt.Fprintf(c.stderr, format, a...)
8080
}
8181
}
8282

8383
// debugf writes to stderr only when debug is enabled.
8484
func (c *Client) debugf(format string, a ...any) {
8585
if c.cfg.Debug {
86-
fmt.Fprintf(c.stderr, "DEBUG: "+format, a...)
86+
_, _ = fmt.Fprintf(c.stderr, "DEBUG: "+format, a...)
8787
}
8888
}
8989

@@ -177,7 +177,7 @@ func (c *Client) StartSearch(ctx context.Context, spl, earliest, latest string)
177177
if err != nil {
178178
return "", err
179179
}
180-
defer resp.Body.Close()
180+
defer func() { _ = resp.Body.Close() }()
181181

182182
if err := checkStatus(resp, http.StatusCreated); err != nil {
183183
return "", err
@@ -212,7 +212,7 @@ func (c *Client) GetJobStatus(ctx context.Context, sid string) (JobStatus, error
212212
if err != nil {
213213
return JobStatus{}, err
214214
}
215-
defer resp.Body.Close()
215+
defer func() { _ = resp.Body.Close() }()
216216

217217
if err := checkStatus(resp, http.StatusOK); err != nil {
218218
return JobStatus{}, err
@@ -337,7 +337,7 @@ func (c *Client) fetchResultsPage(ctx context.Context, sid string, offset, count
337337
if err != nil {
338338
return nil, err
339339
}
340-
defer resp.Body.Close()
340+
defer func() { _ = resp.Body.Close() }()
341341

342342
if err := checkStatus(resp, http.StatusOK); err != nil {
343343
return nil, err
@@ -370,7 +370,7 @@ func (c *Client) CancelSearch(ctx context.Context, sid string) error {
370370
if err != nil {
371371
return err
372372
}
373-
defer resp.Body.Close()
373+
defer func() { _ = resp.Body.Close() }()
374374

375375
if resp.StatusCode != http.StatusOK {
376376
body, _ := io.ReadAll(resp.Body)

internal/client/client_test.go

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,6 @@ import (
1111
"github.com/nlink-jp/splunk-cli/internal/config"
1212
)
1313

14-
func newTestClient(t *testing.T, handler http.Handler) *Client {
15-
t.Helper()
16-
srv := httptest.NewTLSServer(handler)
17-
t.Cleanup(srv.Close)
18-
19-
cfg := &config.Config{
20-
Host: srv.URL,
21-
Token: "testtoken",
22-
Insecure: true,
23-
}
24-
c, err := New(cfg, true)
25-
if err != nil {
26-
t.Fatalf("New: %v", err)
27-
}
28-
// Use the test server's TLS client to handle self-signed cert.
29-
c.http = srv.Client()
30-
// Re-add token setup by wrapping the transport.
31-
return c
32-
}
33-
3414
func TestStartSearch(t *testing.T) {
3515
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
3616
if r.Method != http.MethodPost {
@@ -40,7 +20,7 @@ func TestStartSearch(t *testing.T) {
4020
t.Errorf("unexpected path: %s", r.URL.Path)
4121
}
4222
w.WriteHeader(http.StatusCreated)
43-
json.NewEncoder(w).Encode(map[string]string{"sid": "test_sid_123"})
23+
_ = json.NewEncoder(w).Encode(map[string]string{"sid": "test_sid_123"})
4424
})
4525

4626
srv := httptest.NewServer(handler)
@@ -64,10 +44,10 @@ func TestStartSearch(t *testing.T) {
6444
func TestStartSearch_PipePrefix(t *testing.T) {
6545
var gotSearch string
6646
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
67-
r.ParseForm()
47+
_ = r.ParseForm()
6848
gotSearch = r.FormValue("search")
6949
w.WriteHeader(http.StatusCreated)
70-
json.NewEncoder(w).Encode(map[string]string{"sid": "sid1"})
50+
_ = json.NewEncoder(w).Encode(map[string]string{"sid": "sid1"})
7151
})
7252

7353
srv := httptest.NewServer(handler)
@@ -77,13 +57,13 @@ func TestStartSearch_PipePrefix(t *testing.T) {
7757
c, _ := New(cfg, true)
7858

7959
// SPL starting with | should NOT get "search " prefix.
80-
c.StartSearch(context.Background(), "| stats count", "", "")
60+
_, _ = c.StartSearch(context.Background(), "| stats count", "", "")
8161
if gotSearch != "| stats count" {
8262
t.Errorf("pipe SPL should not get prefix, got: %q", gotSearch)
8363
}
8464

8565
// Normal SPL should get "search " prefix.
86-
c.StartSearch(context.Background(), "index=main", "", "")
66+
_, _ = c.StartSearch(context.Background(), "index=main", "", "")
8767
if gotSearch != "search index=main" {
8868
t.Errorf("normal SPL should get prefix, got: %q", gotSearch)
8969
}
@@ -92,7 +72,7 @@ func TestStartSearch_PipePrefix(t *testing.T) {
9272
func TestGetJobStatus(t *testing.T) {
9373
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
9474
w.Header().Set("Content-Type", "application/json")
95-
json.NewEncoder(w).Encode(map[string]any{
75+
_ = json.NewEncoder(w).Encode(map[string]any{
9676
"entry": []map[string]any{
9777
{"content": map[string]any{
9878
"isDone": true,
@@ -127,7 +107,7 @@ func TestGetJobStatus(t *testing.T) {
127107

128108
func TestGetJobStatus_NotFound(t *testing.T) {
129109
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
130-
json.NewEncoder(w).Encode(map[string]any{"entry": []any{}})
110+
_ = json.NewEncoder(w).Encode(map[string]any{"entry": []any{}})
131111
})
132112

133113
srv := httptest.NewServer(handler)
@@ -145,7 +125,7 @@ func TestGetJobStatus_NotFound(t *testing.T) {
145125
func TestGetJobStatus_APIError(t *testing.T) {
146126
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
147127
w.WriteHeader(http.StatusUnauthorized)
148-
w.Write([]byte("Unauthorized"))
128+
_, _ = w.Write([]byte("Unauthorized"))
149129
})
150130

151131
srv := httptest.NewServer(handler)
@@ -168,7 +148,7 @@ func TestCancelSearch(t *testing.T) {
168148
if !strings.Contains(r.URL.Path, "control") {
169149
t.Errorf("unexpected path: %s", r.URL.Path)
170150
}
171-
r.ParseForm()
151+
_ = r.ParseForm()
172152
if r.FormValue("action") != "cancel" {
173153
t.Errorf("expected action=cancel, got %q", r.FormValue("action"))
174154
}

internal/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func ApplyEnvVars(cfg *Config) {
121121

122122
func checkPermissions(path string, info os.FileInfo) {
123123
if info.Mode().Perm()&0077 != 0 {
124-
fmt.Fprintf(Stderr,
124+
_, _ = fmt.Fprintf(Stderr,
125125
"Warning: config file %s has permissions %#o; expected 0600.\n"+
126126
" The file may contain credentials. Run: chmod 600 %s\n",
127127
path, info.Mode().Perm(), path,

0 commit comments

Comments
 (0)