fix: add golangci-lint config and fix all lint errors
- Add .golangci.yml with v2 config (errcheck, govet, staticcheck, misspell, etc.) - Fix 32 errcheck issues across config, discord, ntfy, server packages - Fix misspelling: cancelled → canceled - Fix staticcheck: use append(slice...) instead of loop - Fix staticcheck: remove empty error branch - Use t.Setenv instead of os.Setenv/Unsetenv in tests - Update CI workflow: add lint job, release tagging, ntfy notifications
This commit is contained in:
@@ -180,7 +180,7 @@ func (c *Config) watchFileSecrets(ctx context.Context) {
|
||||
slog.Error("failed to create fsnotify watcher", "error", err)
|
||||
return
|
||||
}
|
||||
defer watcher.Close()
|
||||
defer func() { _ = watcher.Close() }()
|
||||
|
||||
// Watch the secrets directory
|
||||
// Kubernetes updates secrets by changing the symlink, so watch the parent
|
||||
|
||||
@@ -10,8 +10,7 @@ import (
|
||||
|
||||
func TestGetEnv(t *testing.T) {
|
||||
// Set a test env var
|
||||
os.Setenv("TEST_CONFIG_VAR", "test_value")
|
||||
defer os.Unsetenv("TEST_CONFIG_VAR")
|
||||
t.Setenv("TEST_CONFIG_VAR", "test_value")
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -112,12 +111,8 @@ func TestConfig_LoadWebhookFromSecret_TrimsWhitespace(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLoad_ParsesTopics(t *testing.T) {
|
||||
os.Setenv("NTFY_TOPICS", "alerts, updates , notifications")
|
||||
os.Setenv("VAULT_ENABLED", "false")
|
||||
defer func() {
|
||||
os.Unsetenv("NTFY_TOPICS")
|
||||
os.Unsetenv("VAULT_ENABLED")
|
||||
}()
|
||||
t.Setenv("NTFY_TOPICS", "alerts, updates , notifications")
|
||||
t.Setenv("VAULT_ENABLED", "false")
|
||||
|
||||
cfg, err := Load(context.Background())
|
||||
if err != nil {
|
||||
@@ -138,9 +133,9 @@ func TestLoad_ParsesTopics(t *testing.T) {
|
||||
|
||||
func TestLoad_Defaults(t *testing.T) {
|
||||
// Clear any existing env vars
|
||||
os.Unsetenv("NTFY_URL")
|
||||
os.Unsetenv("HTTP_PORT")
|
||||
os.Unsetenv("VAULT_ENABLED")
|
||||
t.Setenv("NTFY_URL", "")
|
||||
t.Setenv("HTTP_PORT", "")
|
||||
t.Setenv("VAULT_ENABLED", "")
|
||||
|
||||
cfg, err := Load(context.Background())
|
||||
if err != nil {
|
||||
@@ -161,8 +156,7 @@ func TestLoad_Defaults(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLoad_VaultEnabled(t *testing.T) {
|
||||
os.Setenv("VAULT_ENABLED", "true")
|
||||
defer os.Unsetenv("VAULT_ENABLED")
|
||||
t.Setenv("VAULT_ENABLED", "true")
|
||||
|
||||
// This will fail to init Vault (no server), but should gracefully fall back
|
||||
cfg, err := Load(context.Background())
|
||||
@@ -182,12 +176,8 @@ func TestLoad_VaultEnabled(t *testing.T) {
|
||||
|
||||
func TestLoad_FallsBackToEnvVar(t *testing.T) {
|
||||
webhookURL := "https://discord.com/api/webhooks/env/test"
|
||||
os.Setenv("DISCORD_WEBHOOK_URL", webhookURL)
|
||||
os.Setenv("VAULT_ENABLED", "false")
|
||||
defer func() {
|
||||
os.Unsetenv("DISCORD_WEBHOOK_URL")
|
||||
os.Unsetenv("VAULT_ENABLED")
|
||||
}()
|
||||
t.Setenv("DISCORD_WEBHOOK_URL", webhookURL)
|
||||
t.Setenv("VAULT_ENABLED", "false")
|
||||
|
||||
cfg, err := Load(context.Background())
|
||||
if err != nil {
|
||||
|
||||
@@ -25,10 +25,7 @@ func FuzzParseTopics(f *testing.F) {
|
||||
return
|
||||
}
|
||||
|
||||
topics := make([]string, 0)
|
||||
for _, topic := range splitAndTrim(input) {
|
||||
topics = append(topics, topic)
|
||||
}
|
||||
topics := append([]string{}, splitAndTrim(input)...)
|
||||
|
||||
// Accessing results should not panic
|
||||
_ = len(topics)
|
||||
|
||||
@@ -116,10 +116,7 @@ func FuzzWebhookPayloadJSON(f *testing.F) {
|
||||
}
|
||||
|
||||
// Marshaling should not panic
|
||||
_, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
// JSON encoding errors are acceptable for invalid UTF-8
|
||||
// but should not panic
|
||||
}
|
||||
// Marshaling should not panic; JSON encoding errors are acceptable for invalid UTF-8
|
||||
_, _ = json.Marshal(payload)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -117,7 +117,7 @@ func (c *Client) Send(ctx context.Context, webhookURL string, msg ntfy.Message)
|
||||
if err != nil {
|
||||
return fmt.Errorf("send request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
// Handle rate limiting
|
||||
if resp.StatusCode == http.StatusTooManyRequests {
|
||||
|
||||
@@ -61,7 +61,7 @@ func (c *Client) Subscribe(ctx context.Context, msgCh chan<- Message) {
|
||||
err := c.connect(ctx, msgCh)
|
||||
if err != nil {
|
||||
if ctx.Err() != nil {
|
||||
return // Context cancelled
|
||||
return // Context canceled
|
||||
}
|
||||
slog.Error("ntfy connection failed", "error", err, "backoff", backoff)
|
||||
time.Sleep(backoff)
|
||||
@@ -90,7 +90,7 @@ func (c *Client) connect(ctx context.Context, msgCh chan<- Message) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("connect: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
body, _ := io.ReadAll(io.LimitReader(resp.Body, 1024))
|
||||
|
||||
@@ -80,7 +80,7 @@ func TestClient_Subscribe_ReceivesMessages(t *testing.T) {
|
||||
|
||||
for _, msg := range messages {
|
||||
data, _ := json.Marshal(msg)
|
||||
fmt.Fprintf(w, "%s\n", data)
|
||||
_, _ = fmt.Fprintf(w, "%s\n", data)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
@@ -134,7 +134,7 @@ func TestClient_Subscribe_FilterEvents(t *testing.T) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
for _, msg := range messages {
|
||||
data, _ := json.Marshal(msg)
|
||||
fmt.Fprintf(w, "%s\n", data)
|
||||
_, _ = fmt.Fprintf(w, "%s\n", data)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
@@ -194,7 +194,7 @@ func TestClient_Subscribe_ContextCancellation(t *testing.T) {
|
||||
func TestClient_connect_ServerError(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte("internal error"))
|
||||
_, _ = w.Write([]byte("internal error"))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
@@ -218,7 +218,7 @@ func TestClient_connect_URLConstruction(t *testing.T) {
|
||||
defer server.Close()
|
||||
|
||||
client := NewClient(server.URL, []string{"alerts", "updates"})
|
||||
client.connect(context.Background(), make(chan Message))
|
||||
_ = client.connect(context.Background(), make(chan Message))
|
||||
|
||||
expected := "/alerts,updates/json"
|
||||
if requestedURL != expected {
|
||||
|
||||
@@ -71,7 +71,7 @@ func (s *Server) handleHealth(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(status)
|
||||
_ = json.NewEncoder(w).Encode(status)
|
||||
}
|
||||
|
||||
func (s *Server) handleReady(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -89,5 +89,5 @@ func (s *Server) handleReady(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(status)
|
||||
_ = json.NewEncoder(w).Encode(status)
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ func TestServer_HealthEndpoint_StatusCodes(t *testing.T) {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write([]byte(`{"status":"ok","healthy":true}`))
|
||||
_, _ = w.Write([]byte(`{"status":"ok","healthy":true}`))
|
||||
})
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/health", nil)
|
||||
@@ -35,7 +35,7 @@ func TestServer_ReadyEndpoint_StatusCodes(t *testing.T) {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/ready", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write([]byte(`{"status":"ready","ready":true}`))
|
||||
_, _ = w.Write([]byte(`{"status":"ready","ready":true}`))
|
||||
})
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/ready", nil)
|
||||
@@ -56,7 +56,7 @@ func TestServer_Shutdown(t *testing.T) {
|
||||
}
|
||||
|
||||
// Start in background
|
||||
go srv.ListenAndServe()
|
||||
go func() { _ = srv.ListenAndServe() }()
|
||||
|
||||
// Give it a moment to start
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
@@ -77,7 +77,7 @@ func TestServer_MetricsEndpoint(t *testing.T) {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("# metrics here"))
|
||||
_, _ = w.Write([]byte("# metrics here"))
|
||||
})
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/metrics", nil)
|
||||
|
||||
Reference in New Issue
Block a user