From f6ca154315787e050000aa32fde013f0c19157e4 Mon Sep 17 00:00:00 2001 From: user Date: Fri, 20 Feb 2026 03:20:26 -0800 Subject: [PATCH] fix: resolve mnd lint issues with named constants --- cmd/chat-cli/api/client.go | 17 +++++++++++++---- cmd/chat-cli/main.go | 19 ++++++++++++++----- internal/handlers/api.go | 4 +++- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/cmd/chat-cli/api/client.go b/cmd/chat-cli/api/client.go index 51b5b0b..6ff52db 100644 --- a/cmd/chat-cli/api/client.go +++ b/cmd/chat-cli/api/client.go @@ -14,6 +14,15 @@ import ( "time" ) +const ( + // httpClientTimeout is the default HTTP client timeout in seconds. + httpClientTimeout = 30 + // httpStatusErrorThreshold is the minimum status code considered an error. + httpStatusErrorThreshold = 400 + // pollTimeoutBuffer is extra seconds added to HTTP timeout beyond the poll timeout. + pollTimeoutBuffer = 5 +) + var ( // ErrHTTPStatus is returned when the server responds with an error status code. ErrHTTPStatus = errors.New("HTTP error") @@ -33,7 +42,7 @@ func NewClient(baseURL string) *Client { return &Client{ BaseURL: baseURL, HTTPClient: &http.Client{ - Timeout: 30 * time.Second, + Timeout: httpClientTimeout * time.Second, }, } } @@ -72,7 +81,7 @@ func (c *Client) do(method, path string, body any) ([]byte, error) { return nil, fmt.Errorf("read body: %w", err) } - if resp.StatusCode >= 400 { + if resp.StatusCode >= httpStatusErrorThreshold { return data, fmt.Errorf("%w: %d: %s", ErrHTTPStatus, resp.StatusCode, string(data)) } @@ -125,7 +134,7 @@ func (c *Client) SendMessage(msg *Message) error { // PollMessages long-polls for new messages. func (c *Client) PollMessages(afterID string, timeout int) ([]Message, error) { // Use a longer HTTP timeout than the server long-poll timeout. - client := &http.Client{Timeout: time.Duration(timeout+5) * time.Second} + client := &http.Client{Timeout: time.Duration(timeout+pollTimeoutBuffer) * time.Second} params := url.Values{} if afterID != "" { @@ -158,7 +167,7 @@ func (c *Client) PollMessages(afterID string, timeout int) ([]Message, error) { return nil, err } - if resp.StatusCode >= 400 { + if resp.StatusCode >= httpStatusErrorThreshold { return nil, fmt.Errorf("%w: %d: %s", ErrHTTPStatus, resp.StatusCode, string(data)) } diff --git a/cmd/chat-cli/main.go b/cmd/chat-cli/main.go index 78268dd..99d4531 100644 --- a/cmd/chat-cli/main.go +++ b/cmd/chat-cli/main.go @@ -11,6 +11,15 @@ import ( "git.eeqj.de/sneak/chat/cmd/chat-cli/api" ) +const ( + // splitParts is the number of parts to split a command into (command + args). + splitParts = 2 + // pollTimeout is the long-poll timeout in seconds. + pollTimeout = 15 + // pollRetryDelay is the delay before retrying a failed poll. + pollRetryDelay = 2 * time.Second +) + // App holds the application state. type App struct { ui *UI @@ -89,7 +98,7 @@ func (a *App) handleInput(text string) { } func (a *App) handleCommand(text string) { - parts := strings.SplitN(text, " ", 2) + parts := strings.SplitN(text, " ", splitParts) cmd := strings.ToLower(parts[0]) args := "" @@ -285,8 +294,8 @@ func (a *App) cmdPart(channel string) { } func (a *App) cmdMsg(args string) { - parts := strings.SplitN(args, " ", 2) - if len(parts) < 2 { + parts := strings.SplitN(args, " ", splitParts) + if len(parts) < splitParts { a.ui.AddStatus("[red]Usage: /msg ") return @@ -514,10 +523,10 @@ func (a *App) pollLoop() { return } - msgs, err := client.PollMessages(lastID, 15) + msgs, err := client.PollMessages(lastID, pollTimeout) if err != nil { // Transient error — retry after delay. - time.Sleep(2 * time.Second) + time.Sleep(pollRetryDelay) continue } diff --git a/internal/handlers/api.go b/internal/handlers/api.go index 585a184..bbf3b1f 100644 --- a/internal/handlers/api.go +++ b/internal/handlers/api.go @@ -41,8 +41,10 @@ func (s *Handlers) requireAuth(w http.ResponseWriter, r *http.Request) (string, return uid, nick, true } +const idBytes = 16 + func generateID() string { - b := make([]byte, 16) + b := make([]byte, idBytes) _, _ = rand.Read(b) return hex.EncodeToString(b)