diff --git a/.golangci.yml b/.golangci.yml index 16214a7e..c0be4888 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -29,9 +29,6 @@ linters: - path: ^internal/goclusterfs/ linters: [errcheck] - - path: ^cmd/gfsctl/ - linters: [errcheck] - - path: ^cmd/mount/ linters: [errcheck] @@ -41,6 +38,10 @@ linters: - path: ^tests/integration/ linters: [errcheck] + # gfsctl prints to the terminal and, like fmt.Printf, ignores write errors there. + - path: ^cmd/gfsctl/ + linters: [errcheck] + text: "`fmt\\.Fprint(f|ln)?` is not checked" - path: ^internal/goclusterfs/fs\.go$ linters: [unused] text: "errRetryCreate|field sb is unused" diff --git a/cmd/gfsctl/client/client.go b/cmd/gfsctl/client/client.go index e1b55eb8..1c65a318 100644 --- a/cmd/gfsctl/client/client.go +++ b/cmd/gfsctl/client/client.go @@ -113,7 +113,7 @@ func (c *Client) send(ctx context.Context, method, path string) ([]byte, int, er logging.Debugf("%s %s failed after %v: %v", method, url, time.Since(start).Round(time.Millisecond), err) return nil, 0, fmt.Errorf("request failed: %w", err) } - defer resp.Body.Close() + defer func() { _ = resp.Body.Close() }() body, err := io.ReadAll(io.LimitReader(resp.Body, MaxResponseSize+1)) logging.Debugf("%s %s: %s, %d bytes in %v", method, url, resp.Status, len(body), time.Since(start).Round(time.Millisecond)) diff --git a/cmd/gfsctl/client/client_test.go b/cmd/gfsctl/client/client_test.go index 2c7f0560..3216bc22 100644 --- a/cmd/gfsctl/client/client_test.go +++ b/cmd/gfsctl/client/client_test.go @@ -33,11 +33,11 @@ func TestAPIErrorCarriesStatusAndDetails(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Header.Get("Authorization") != "Bearer secret" { w.WriteHeader(http.StatusUnauthorized) - w.Write([]byte(`{"error":"invalid token","code":401}`)) + _, _ = w.Write([]byte(`{"error":"invalid token","code":401}`)) return } w.WriteHeader(http.StatusServiceUnavailable) - w.Write([]byte("not json")) + _, _ = w.Write([]byte("not json")) })) defer srv.Close() c := &Client{baseURL: srv.URL, httpClient: srv.Client()} diff --git a/cmd/gfsctl/commands/commands_test.go b/cmd/gfsctl/commands/commands_test.go index 2998eccf..ab141764 100644 --- a/cmd/gfsctl/commands/commands_test.go +++ b/cmd/gfsctl/commands/commands_test.go @@ -3,6 +3,7 @@ package commands import ( "bytes" "encoding/json" + "fmt" "net/http" "net/http/httptest" "net/url" @@ -25,12 +26,16 @@ func TestMain(m *testing.M) { if err != nil { panic(err) } - os.Setenv("XDG_CONFIG_HOME", dir) + if err := os.Setenv("XDG_CONFIG_HOME", dir); err != nil { + panic(err) + } for _, name := range []string{"GFSCTL_HOST", "GFSCTL_PORT", "GFSCTL_TOKEN", "GFSCTL_OUTPUT", "GFSCTL_TIMEOUT", "NO_COLOR"} { - os.Unsetenv(name) + if err := os.Unsetenv(name); err != nil { + panic(err) + } } code := m.Run() - os.RemoveAll(dir) + _ = os.RemoveAll(dir) os.Exit(code) } @@ -63,11 +68,11 @@ func startFakeAPI(t *testing.T, routes map[string]any) *fakeAPI { http.NotFound(w, r) case int: w.WriteHeader(v) - json.NewEncoder(w).Encode(api.ErrorResponse{Error: "stats not available", Code: v, Details: "no stats provider configured"}) + _ = json.NewEncoder(w).Encode(api.ErrorResponse{Error: "stats not available", Code: v, Details: "no stats provider configured"}) case string: - w.Write([]byte(v)) + _, _ = w.Write([]byte(v)) default: - json.NewEncoder(w).Encode(v) + _ = json.NewEncoder(w).Encode(v) } })) t.Cleanup(srv.Close) @@ -81,7 +86,9 @@ func startFakeAPI(t *testing.T, routes map[string]any) *fakeAPI { func resetFlags(c *cobra.Command) { reset := func(fl *pflag.Flag) { - fl.Value.Set(fl.DefValue) + if err := fl.Value.Set(fl.DefValue); err != nil { + panic(fmt.Sprintf("reset flag %s to %q: %v", fl.Name, fl.DefValue, err)) + } fl.Changed = false } c.Flags().VisitAll(reset) @@ -228,7 +235,7 @@ func writeConfig(t *testing.T, content string) { if err := os.WriteFile(path, []byte(content), 0o600); err != nil { t.Fatal(err) } - t.Cleanup(func() { os.Remove(path) }) + t.Cleanup(func() { _ = os.Remove(path) }) } func TestSettingsPrecedence(t *testing.T) { @@ -273,7 +280,7 @@ func TestBadSettingsAreRefused(t *testing.T) { t.Errorf("%s=%q %v: err=%v stderr=%q", tc.env, tc.value, tc.args, err, stderr) } if tc.env != "" { - os.Unsetenv(tc.env) + _ = os.Unsetenv(tc.env) } } } diff --git a/cmd/gfsctl/commands/errors_test.go b/cmd/gfsctl/commands/errors_test.go index 07e4b613..c6727a31 100644 --- a/cmd/gfsctl/commands/errors_test.go +++ b/cmd/gfsctl/commands/errors_test.go @@ -18,11 +18,11 @@ func serveHealth(t *testing.T, code int, status api.HealthStatus) *fakeAPI { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/api/v1/status" && r.Header.Get("Authorization") == "" { w.WriteHeader(http.StatusUnauthorized) - json.NewEncoder(w).Encode(api.ErrorResponse{Error: "unauthorized", Code: http.StatusUnauthorized}) + _ = json.NewEncoder(w).Encode(api.ErrorResponse{Error: "unauthorized", Code: http.StatusUnauthorized}) return } w.WriteHeader(code) - json.NewEncoder(w).Encode(api.HealthResponse{Status: status, Checks: map[string]api.ComponentHealth{ + _ = json.NewEncoder(w).Encode(api.HealthResponse{Status: status, Checks: map[string]api.ComponentHealth{ "recovery": {Status: status, Message: "replaying the journal of the crashed node in slot 3"}, }}) }))