Skip to content

Commit ef24097

Browse files
agb-cloudclaude
andcommitted
fix: resolve all remaining golangci-lint errors
Comprehensive fix for all linter issues: - errcheck: Add error handling for w.Write(), json.Encoder.Encode(), and io.Copy() in test files - gosimple S1034: Use type switch instead of repeated type assertions in setBody() - staticcheck SA9003: Remove empty branch by using early returns in detectContentType() All test mock servers now properly ignore write errors with _ assignments and comments. 🤖 Generated with [Claude Code] Co-Authored-By: Claude <noreply@anthropic.com>
1 parent f5bafc9 commit ef24097

9 files changed

Lines changed: 45 additions & 45 deletions

internal/client/client.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -245,16 +245,19 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e
245245
bodyBuf = &bytes.Buffer{}
246246
}
247247

248-
if reader, ok := body.(io.Reader); ok {
249-
_, err = bodyBuf.ReadFrom(reader)
250-
} else if b, ok := body.([]byte); ok {
251-
_, err = bodyBuf.Write(b)
252-
} else if s, ok := body.(string); ok {
253-
_, err = bodyBuf.WriteString(s)
254-
} else if s, ok := body.(*string); ok {
255-
_, err = bodyBuf.WriteString(*s)
256-
} else if JsonCheck.MatchString(contentType) {
257-
err = json.NewEncoder(bodyBuf).Encode(body)
248+
switch v := body.(type) {
249+
case io.Reader:
250+
_, err = bodyBuf.ReadFrom(v)
251+
case []byte:
252+
_, err = bodyBuf.Write(v)
253+
case string:
254+
_, err = bodyBuf.WriteString(v)
255+
case *string:
256+
_, err = bodyBuf.WriteString(*v)
257+
default:
258+
if JsonCheck.MatchString(contentType) {
259+
err = json.NewEncoder(bodyBuf).Encode(body)
260+
}
258261
}
259262

260263
if err != nil {
@@ -270,20 +273,17 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e
270273

271274
// detectContentType method is used to figure out `Request.Body` content type for request header
272275
func detectContentType(body interface{}) string {
273-
contentType := "text/plain; charset=utf-8"
274-
switch body.(type) {
276+
switch v := body.(type) {
275277
case string:
276-
// Already set to text/plain by default, no need to reassign
278+
return "text/plain; charset=utf-8"
277279
case []byte:
278-
contentType = http.DetectContentType(body.([]byte))
280+
return http.DetectContentType(v)
279281
case map[string]interface{}, []interface{}:
280-
contentType = "application/json; charset=utf-8"
282+
return "application/json; charset=utf-8"
281283
default:
282284
// For any other type, assume JSON
283-
contentType = "application/json; charset=utf-8"
285+
return "application/json; charset=utf-8"
284286
}
285-
286-
return contentType
287287
}
288288

289289
// GenericOpenAPIError Provides access to the body, error and model on returned errors.

test/integration/login_port_selection_integration_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func TestLoginPortSelectionIntegration(t *testing.T) {
7777
}`, tt.mockAlternativePorts)
7878
w.Header().Set("Content-Type", "application/json")
7979
w.WriteHeader(http.StatusOK)
80-
w.Write([]byte(response))
80+
_, _ = w.Write([]byte(response)) // Ignore errors in test mock server
8181
} else if strings.Contains(r.URL.Path, "login_translate") {
8282
response := `{
8383
"code": "200",
@@ -94,7 +94,7 @@ func TestLoginPortSelectionIntegration(t *testing.T) {
9494
}`
9595
w.Header().Set("Content-Type", "application/json")
9696
w.WriteHeader(http.StatusOK)
97-
w.Write([]byte(response))
97+
_, _ = w.Write([]byte(response)) // Ignore errors in test mock server
9898
}
9999
}))
100100
defer mockServer.Close()

test/unit/image_api_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func TestImageAPIGetUploadCredential(t *testing.T) {
5454
}
5555

5656
w.Header().Set("Content-Type", "application/json")
57-
json.NewEncoder(w).Encode(response)
57+
_ = json.NewEncoder(w).Encode(response) // Ignore errors in test mock server
5858
}))
5959
defer server.Close()
6060

@@ -154,7 +154,7 @@ func TestImageAPICreateImage(t *testing.T) {
154154
}
155155

156156
w.Header().Set("Content-Type", "application/json")
157-
json.NewEncoder(w).Encode(response)
157+
_ = json.NewEncoder(w).Encode(response) // Ignore errors in test mock server
158158
}))
159159
defer server.Close()
160160

@@ -309,7 +309,7 @@ func TestImageAPIGetImageTask(t *testing.T) {
309309
}
310310

311311
w.Header().Set("Content-Type", "application/json")
312-
json.NewEncoder(w).Encode(response)
312+
_ = json.NewEncoder(w).Encode(response) // Ignore errors in test mock server
313313
}))
314314
defer server.Close()
315315

@@ -428,7 +428,7 @@ func TestImageAPIListImages(t *testing.T) {
428428
}
429429

430430
w.Header().Set("Content-Type", "application/json")
431-
json.NewEncoder(w).Encode(response)
431+
_ = json.NewEncoder(w).Encode(response) // Ignore errors in test mock server
432432
}))
433433
defer server.Close()
434434

@@ -599,7 +599,7 @@ func TestImageAPIService_StartImage(t *testing.T) {
599599

600600
w.Header().Set("Content-Type", "application/json")
601601
w.WriteHeader(http.StatusOK)
602-
json.NewEncoder(w).Encode(response)
602+
_ = json.NewEncoder(w).Encode(response) // Ignore errors in test mock server
603603
}))
604604
defer server.Close()
605605

@@ -653,7 +653,7 @@ func TestImageAPIService_StartImage(t *testing.T) {
653653

654654
w.Header().Set("Content-Type", "application/json")
655655
w.WriteHeader(http.StatusOK)
656-
json.NewEncoder(w).Encode(response)
656+
_ = json.NewEncoder(w).Encode(response) // Ignore errors in test mock server
657657
}))
658658
defer server.Close()
659659

@@ -720,7 +720,7 @@ func TestImageAPIService_StartImage(t *testing.T) {
720720

721721
w.Header().Set("Content-Type", "application/json")
722722
w.WriteHeader(http.StatusBadRequest)
723-
json.NewEncoder(w).Encode(response)
723+
_ = json.NewEncoder(w).Encode(response) // Ignore errors in test mock server
724724
}))
725725
defer server.Close()
726726

@@ -795,7 +795,7 @@ func TestImageAPIService_StopImage(t *testing.T) {
795795

796796
w.Header().Set("Content-Type", "application/json")
797797
w.WriteHeader(http.StatusOK)
798-
json.NewEncoder(w).Encode(response)
798+
_ = json.NewEncoder(w).Encode(response) // Ignore errors in test mock server
799799
}))
800800
defer server.Close()
801801

@@ -866,7 +866,7 @@ func TestImageAPIService_StopImage(t *testing.T) {
866866

867867
w.Header().Set("Content-Type", "application/json")
868868
w.WriteHeader(http.StatusBadRequest)
869-
json.NewEncoder(w).Encode(response)
869+
_ = json.NewEncoder(w).Encode(response) // Ignore errors in test mock server
870870
}))
871871
defer server.Close()
872872

test/unit/login_port_selection_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func TestLoginPortSelectionFlow(t *testing.T) {
7474
}`
7575
w.Header().Set("Content-Type", "application/json")
7676
w.WriteHeader(http.StatusOK)
77-
w.Write([]byte(response))
77+
_, _ = w.Write([]byte(response)) // Ignore errors in test mock server
7878
}))
7979
defer server.Close()
8080

@@ -157,7 +157,7 @@ func TestLoginTranslateWithSelectedPort(t *testing.T) {
157157
}`
158158
w.Header().Set("Content-Type", "application/json")
159159
w.WriteHeader(http.StatusOK)
160-
w.Write([]byte(response))
160+
_, _ = w.Write([]byte(response)) // Ignore errors in test mock server
161161
}))
162162
defer server.Close()
163163

test/unit/login_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func TestLoginCommand(t *testing.T) {
6464

6565
w.Header().Set("Content-Type", "application/json")
6666
w.WriteHeader(http.StatusOK)
67-
json.NewEncoder(w).Encode(response)
67+
_ = json.NewEncoder(w).Encode(response) // Ignore errors in test mock server
6868
}))
6969
defer mockServer.Close()
7070

@@ -199,7 +199,7 @@ func TestLoginCommandErrorHandling(t *testing.T) {
199199
// Create a mock server that returns an error
200200
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
201201
w.WriteHeader(http.StatusInternalServerError)
202-
w.Write([]byte("Internal Server Error"))
202+
_, _ = w.Write([]byte("Internal Server Error")) // Ignore errors in test mock server
203203
}))
204204
defer mockServer.Close()
205205

test/unit/logout_cmd_simple_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func TestLogoutCommandSimple(t *testing.T) {
126126
// Restore stdout and get output
127127
w.Close()
128128
os.Stdout = oldStdout
129-
io.Copy(&buf, r)
129+
_, _ = io.Copy(&buf, r) // Ignore errors in test helper
130130
output := buf.String()
131131

132132
if err != nil {
@@ -184,7 +184,7 @@ func TestLogoutCommandNoConfig(t *testing.T) {
184184
// Restore stdout and get output
185185
w.Close()
186186
os.Stdout = oldStdout
187-
io.Copy(&buf, r)
187+
_, _ = io.Copy(&buf, r) // Ignore errors in test helper
188188
output := buf.String()
189189

190190
if err != nil {

test/unit/logout_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func TestLogout(t *testing.T) {
9696
// Return mock response
9797
w.Header().Set("Content-Type", "application/json")
9898
w.WriteHeader(tt.mockStatusCode)
99-
json.NewEncoder(w).Encode(tt.mockResponse)
99+
_ = json.NewEncoder(w).Encode(tt.mockResponse) // Ignore errors in test mock server
100100
}))
101101
defer mockServer.Close()
102102

test/unit/oauth_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func TestOAuthAPI_GetLoginProviderURL_WithLocalhostPort(t *testing.T) {
6363
}`
6464
w.Header().Set("Content-Type", "application/json")
6565
w.WriteHeader(http.StatusOK)
66-
w.Write([]byte(response))
66+
_, _ = w.Write([]byte(response)) // Ignore errors in test mock server
6767
}))
6868
defer server.Close()
6969

@@ -156,7 +156,7 @@ func TestOAuthAPI_LoginTranslate_WithLocalhostPort(t *testing.T) {
156156
}`
157157
w.Header().Set("Content-Type", "application/json")
158158
w.WriteHeader(http.StatusOK)
159-
w.Write([]byte(response))
159+
_, _ = w.Write([]byte(response)) // Ignore errors in test mock server
160160
}))
161161
defer server.Close()
162162

@@ -218,7 +218,7 @@ func TestOAuthLoginProviderResponse_AlternativePorts(t *testing.T) {
218218
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
219219
w.Header().Set("Content-Type", "application/json")
220220
w.WriteHeader(http.StatusOK)
221-
w.Write([]byte(jsonResponse))
221+
_, _ = w.Write([]byte(jsonResponse)) // Ignore errors in test mock server
222222
}))
223223
defer server.Close()
224224

test/unit/retry_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestRetryMechanism(t *testing.T) {
2626
return func(w http.ResponseWriter, r *http.Request) {
2727
*attemptCount++
2828
w.WriteHeader(http.StatusOK)
29-
w.Write([]byte("success"))
29+
_, _ = w.Write([]byte("success")) // Ignore errors in test mock server
3030
}
3131
},
3232
expectedAttempts: 1,
@@ -41,11 +41,11 @@ func TestRetryMechanism(t *testing.T) {
4141
if *attemptCount == 1 {
4242
// Simulate server error on first attempt (retryable)
4343
w.WriteHeader(http.StatusInternalServerError)
44-
w.Write([]byte("server error"))
44+
_, _ = w.Write([]byte("server error")) // Ignore errors in test mock server
4545
return
4646
}
4747
w.WriteHeader(http.StatusOK)
48-
w.Write([]byte("success"))
48+
_, _ = w.Write([]byte("success")) // Ignore errors in test mock server
4949
}
5050
},
5151
expectedAttempts: 2,
@@ -59,11 +59,11 @@ func TestRetryMechanism(t *testing.T) {
5959
*attemptCount++
6060
if *attemptCount <= 2 {
6161
w.WriteHeader(http.StatusInternalServerError)
62-
w.Write([]byte("server error"))
62+
_, _ = w.Write([]byte("server error")) // Ignore errors in test mock server
6363
return
6464
}
6565
w.WriteHeader(http.StatusOK)
66-
w.Write([]byte("success"))
66+
_, _ = w.Write([]byte("success")) // Ignore errors in test mock server
6767
}
6868
},
6969
expectedAttempts: 3,
@@ -76,7 +76,7 @@ func TestRetryMechanism(t *testing.T) {
7676
return func(w http.ResponseWriter, r *http.Request) {
7777
*attemptCount++
7878
w.WriteHeader(http.StatusNotFound)
79-
w.Write([]byte("not found"))
79+
_, _ = w.Write([]byte("not found")) // Ignore errors in test mock server
8080
}
8181
},
8282
expectedAttempts: 1,

0 commit comments

Comments
 (0)