fix: resolve nlreturn, modernize, perfsprint, wsl_v5, and partial err113 lint issues

This commit is contained in:
clawbot
2026-02-20 02:59:15 -08:00
parent c65c9bbe5a
commit c1040ff69d
11 changed files with 189 additions and 44 deletions

View File

@@ -90,6 +90,7 @@ func NewTest(dsn string) (*Database, error) {
// Item 9: Enable foreign keys
if _, err := d.Exec("PRAGMA foreign_keys = ON"); err != nil {
_ = d.Close()
return nil, fmt.Errorf("enable foreign keys: %w", err)
}
@@ -219,6 +220,7 @@ func (s *Database) DeleteAuthToken(
_, err := s.db.ExecContext(ctx,
`DELETE FROM auth_tokens WHERE token = ?`, token,
)
return err
}
@@ -231,6 +233,7 @@ func (s *Database) UpdateUserLastSeen(
`UPDATE users SET last_seen_at = CURRENT_TIMESTAMP WHERE id = ?`,
userID,
)
return err
}
@@ -394,6 +397,7 @@ func (s *Database) DequeueMessages(
if err != nil {
return nil, err
}
defer func() { _ = rows.Close() }()
entries := []*models.MessageQueueEntry{}
@@ -423,7 +427,7 @@ func (s *Database) AckMessages(
}
placeholders := make([]string, len(entryIDs))
args := make([]interface{}, len(entryIDs))
args := make([]any, len(entryIDs))
for i, id := range entryIDs {
placeholders[i] = "?"

View File

@@ -62,7 +62,8 @@ func (s *Database) ListChannels(ctx context.Context, userID string) ([]ChannelIn
for rows.Next() {
var ch ChannelInfo
if err := rows.Scan(&ch.ID, &ch.Name, &ch.Topic); err != nil {
err := rows.Scan(&ch.ID, &ch.Name, &ch.Topic)
if err != nil {
return nil, err
}
@@ -95,7 +96,8 @@ func (s *Database) ChannelMembers(ctx context.Context, channelID string) ([]Memb
for rows.Next() {
var m MemberInfo
if err := rows.Scan(&m.ID, &m.Nick, &m.LastSeen); err != nil {
err := rows.Scan(&m.ID, &m.Nick, &m.LastSeen)
if err != nil {
return nil, err
}
@@ -182,7 +184,8 @@ func (s *Database) PollMessages(ctx context.Context, userID string, afterTS stri
for rows.Next() {
var m MessageInfo
if err := rows.Scan(&m.ID, &m.Channel, &m.Nick, &m.Content, &m.CreatedAt); err != nil {
err := rows.Scan(&m.ID, &m.Channel, &m.Nick, &m.Content, &m.CreatedAt)
if err != nil {
return nil, err
}
@@ -200,7 +203,7 @@ func (s *Database) GetMessagesBefore(ctx context.Context, target string, beforeT
var rows interface {
Next() bool
Scan(dest ...interface{}) error
Scan(dest ...any) error
Close() error
Err() error
}
@@ -233,7 +236,8 @@ func (s *Database) GetMessagesBefore(ctx context.Context, target string, beforeT
for rows.Next() {
var m MessageInfo
if err := rows.Scan(&m.ID, &m.Channel, &m.Nick, &m.Content, &m.CreatedAt); err != nil {
err := rows.Scan(&m.ID, &m.Channel, &m.Nick, &m.Content, &m.CreatedAt)
if err != nil {
return nil, err
}
@@ -256,7 +260,7 @@ func (s *Database) GetDMsBefore(ctx context.Context, userA, userB string, before
var rows interface {
Next() bool
Scan(dest ...interface{}) error
Scan(dest ...any) error
Close() error
Err() error
}
@@ -290,7 +294,8 @@ func (s *Database) GetDMsBefore(ctx context.Context, userA, userB string, before
for rows.Next() {
var m MessageInfo
if err := rows.Scan(&m.ID, &m.Nick, &m.Content, &m.DMTarget, &m.CreatedAt); err != nil {
err := rows.Scan(&m.ID, &m.Nick, &m.Content, &m.DMTarget, &m.CreatedAt)
if err != nil {
return nil, err
}
@@ -341,7 +346,8 @@ func (s *Database) ListAllChannels(ctx context.Context) ([]ChannelInfo, error) {
for rows.Next() {
var ch ChannelInfo
if err := rows.Scan(&ch.ID, &ch.Name, &ch.Topic); err != nil {
err := rows.Scan(&ch.ID, &ch.Name, &ch.Topic)
if err != nil {
return nil, err
}

View File

@@ -34,6 +34,7 @@ func (s *Handlers) requireAuth(w http.ResponseWriter, r *http.Request) (string,
uid, nick, err := s.authUser(r)
if err != nil {
s.respondJSON(w, r, map[string]string{"error": "unauthorized"}, http.StatusUnauthorized)
return "", "", false
}
@@ -52,6 +53,7 @@ func (s *Handlers) HandleCreateSession() http.HandlerFunc {
type request struct {
Nick string `json:"nick"`
}
type response struct {
ID string `json:"id"`
Nick string `json:"nick"`
@@ -62,12 +64,14 @@ func (s *Handlers) HandleCreateSession() http.HandlerFunc {
var req request
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
s.respondJSON(w, r, map[string]string{"error": "invalid request"}, http.StatusBadRequest)
return
}
req.Nick = strings.TrimSpace(req.Nick)
if req.Nick == "" || len(req.Nick) > 32 {
s.respondJSON(w, r, map[string]string{"error": "nick must be 1-32 characters"}, http.StatusBadRequest)
return
}
@@ -77,6 +81,7 @@ func (s *Handlers) HandleCreateSession() http.HandlerFunc {
if err != nil {
if strings.Contains(err.Error(), "UNIQUE") {
s.respondJSON(w, r, map[string]string{"error": "nick already taken"}, http.StatusConflict)
return
}
@@ -162,6 +167,7 @@ func (s *Handlers) HandleChannelMembers() http.HandlerFunc {
"SELECT id FROM channels WHERE name = ?", name).Scan(&chID)
if err != nil {
s.respondJSON(w, r, map[string]string{"error": "channel not found"}, http.StatusNotFound)
return
}
@@ -205,10 +211,10 @@ func (s *Handlers) HandleGetMessages() http.HandlerFunc {
// The "command" field dispatches to the appropriate logic.
func (s *Handlers) HandleSendCommand() http.HandlerFunc {
type request struct {
Command string `json:"command"`
To string `json:"to"`
Params []string `json:"params,omitempty"`
Body interface{} `json:"body,omitempty"`
Command string `json:"command"`
To string `json:"to"`
Params []string `json:"params,omitempty"`
Body any `json:"body,omitempty"`
}
return func(w http.ResponseWriter, r *http.Request) {
@@ -218,8 +224,10 @@ func (s *Handlers) HandleSendCommand() http.HandlerFunc {
}
var req request
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
s.respondJSON(w, r, map[string]string{"error": "invalid request"}, http.StatusBadRequest)
return
}
@@ -229,7 +237,7 @@ func (s *Handlers) HandleSendCommand() http.HandlerFunc {
// Helper to extract body as string lines.
bodyLines := func() []string {
switch v := req.Body.(type) {
case []interface{}:
case []any:
lines := make([]string, 0, len(v))
for _, item := range v {
@@ -267,6 +275,7 @@ func (s *Handlers) HandleSendCommand() http.HandlerFunc {
default:
_ = nick // suppress unused warning
s.respondJSON(w, r, map[string]string{"error": "unknown command: " + req.Command}, http.StatusBadRequest)
}
}
@@ -275,11 +284,13 @@ func (s *Handlers) HandleSendCommand() http.HandlerFunc {
func (s *Handlers) handlePrivmsg(w http.ResponseWriter, r *http.Request, uid, nick, to string, lines []string) {
if to == "" {
s.respondJSON(w, r, map[string]string{"error": "to field required"}, http.StatusBadRequest)
return
}
if len(lines) == 0 {
s.respondJSON(w, r, map[string]string{"error": "body required"}, http.StatusBadRequest)
return
}
@@ -293,6 +304,7 @@ func (s *Handlers) handlePrivmsg(w http.ResponseWriter, r *http.Request, uid, ni
"SELECT id FROM channels WHERE name = ?", to).Scan(&chID)
if err != nil {
s.respondJSON(w, r, map[string]string{"error": "channel not found"}, http.StatusNotFound)
return
}
@@ -310,6 +322,7 @@ func (s *Handlers) handlePrivmsg(w http.ResponseWriter, r *http.Request, uid, ni
targetUser, err := s.params.Database.GetUserByNick(r.Context(), to)
if err != nil {
s.respondJSON(w, r, map[string]string{"error": "user not found"}, http.StatusNotFound)
return
}
@@ -328,6 +341,7 @@ func (s *Handlers) handlePrivmsg(w http.ResponseWriter, r *http.Request, uid, ni
func (s *Handlers) handleJoin(w http.ResponseWriter, r *http.Request, uid, to string) {
if to == "" {
s.respondJSON(w, r, map[string]string{"error": "to field required"}, http.StatusBadRequest)
return
}
@@ -357,6 +371,7 @@ func (s *Handlers) handleJoin(w http.ResponseWriter, r *http.Request, uid, to st
func (s *Handlers) handlePart(w http.ResponseWriter, r *http.Request, uid, to string) {
if to == "" {
s.respondJSON(w, r, map[string]string{"error": "to field required"}, http.StatusBadRequest)
return
}
@@ -371,6 +386,7 @@ func (s *Handlers) handlePart(w http.ResponseWriter, r *http.Request, uid, to st
"SELECT id FROM channels WHERE name = ?", channel).Scan(&chID)
if err != nil {
s.respondJSON(w, r, map[string]string{"error": "channel not found"}, http.StatusNotFound)
return
}
@@ -387,18 +403,22 @@ func (s *Handlers) handlePart(w http.ResponseWriter, r *http.Request, uid, to st
func (s *Handlers) handleNick(w http.ResponseWriter, r *http.Request, uid string, lines []string) {
if len(lines) == 0 {
s.respondJSON(w, r, map[string]string{"error": "body required (new nick)"}, http.StatusBadRequest)
return
}
newNick := strings.TrimSpace(lines[0])
if newNick == "" || len(newNick) > 32 {
s.respondJSON(w, r, map[string]string{"error": "nick must be 1-32 characters"}, http.StatusBadRequest)
return
}
if err := s.params.Database.ChangeNick(r.Context(), uid, newNick); err != nil {
err := s.params.Database.ChangeNick(r.Context(), uid, newNick)
if err != nil {
if strings.Contains(err.Error(), "UNIQUE") {
s.respondJSON(w, r, map[string]string{"error": "nick already in use"}, http.StatusConflict)
return
}
@@ -414,11 +434,13 @@ func (s *Handlers) handleNick(w http.ResponseWriter, r *http.Request, uid string
func (s *Handlers) handleTopic(w http.ResponseWriter, r *http.Request, uid, to string, lines []string) {
if to == "" {
s.respondJSON(w, r, map[string]string{"error": "to field required"}, http.StatusBadRequest)
return
}
if len(lines) == 0 {
s.respondJSON(w, r, map[string]string{"error": "body required (topic text)"}, http.StatusBadRequest)
return
}
@@ -429,7 +451,8 @@ func (s *Handlers) handleTopic(w http.ResponseWriter, r *http.Request, uid, to s
channel = "#" + channel
}
if err := s.params.Database.SetTopic(r.Context(), channel, uid, topic); err != nil {
err := s.params.Database.SetTopic(r.Context(), channel, uid, topic)
if err != nil {
s.log.Error("set topic failed", "error", err)
s.respondJSON(w, r, map[string]string{"error": "internal error"}, http.StatusInternalServerError)
@@ -450,6 +473,7 @@ func (s *Handlers) HandleGetHistory() http.HandlerFunc {
target := r.URL.Query().Get("target")
if target == "" {
s.respondJSON(w, r, map[string]string{"error": "target required"}, http.StatusBadRequest)
return
}
@@ -468,6 +492,7 @@ func (s *Handlers) HandleGetHistory() http.HandlerFunc {
"SELECT id FROM channels WHERE name = ?", target).Scan(&chID)
if err != nil {
s.respondJSON(w, r, map[string]string{"error": "channel not found"}, http.StatusNotFound)
return
}
@@ -485,6 +510,7 @@ func (s *Handlers) HandleGetHistory() http.HandlerFunc {
targetUser, err := s.params.Database.GetUserByNick(r.Context(), target)
if err != nil {
s.respondJSON(w, r, map[string]string{"error": "user not found"}, http.StatusNotFound)
return
}

View File

@@ -2,7 +2,7 @@ package models
import (
"context"
"fmt"
"errors"
"time"
)
@@ -23,5 +23,5 @@ func (t *AuthToken) User(ctx context.Context) (*User, error) {
return ul.GetUserByID(ctx, t.UserID)
}
return nil, fmt.Errorf("user lookup not available")
return nil, errors.New("user lookup not available")
}

View File

@@ -2,7 +2,7 @@ package models
import (
"context"
"fmt"
"errors"
"time"
)
@@ -23,7 +23,7 @@ func (cm *ChannelMember) User(ctx context.Context) (*User, error) {
return ul.GetUserByID(ctx, cm.UserID)
}
return nil, fmt.Errorf("user lookup not available")
return nil, errors.New("user lookup not available")
}
// Channel returns the full Channel for this membership.
@@ -32,5 +32,5 @@ func (cm *ChannelMember) Channel(ctx context.Context) (*Channel, error) {
return cl.GetChannelByID(ctx, cm.ChannelID)
}
return nil, fmt.Errorf("channel lookup not available")
return nil, errors.New("channel lookup not available")
}

View File

@@ -2,7 +2,7 @@ package models
import (
"context"
"fmt"
"errors"
"time"
)
@@ -23,5 +23,5 @@ func (s *Session) User(ctx context.Context) (*User, error) {
return ul.GetUserByID(ctx, s.UserID)
}
return nil, fmt.Errorf("user lookup not available")
return nil, errors.New("user lookup not available")
}

View File

@@ -71,16 +71,20 @@ func (s *Server) SetupRoutes() {
s.log.Error("failed to get web dist filesystem", "error", err)
} else {
fileServer := http.FileServer(http.FS(distFS))
s.router.Get("/*", func(w http.ResponseWriter, r *http.Request) {
// Try to serve the file; if not found, serve index.html for SPA routing
f, err := distFS.(fs.ReadFileFS).ReadFile(r.URL.Path[1:])
if err != nil || len(f) == 0 {
indexHTML, _ := distFS.(fs.ReadFileFS).ReadFile("index.html")
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(indexHTML)
return
}
fileServer.ServeHTTP(w, r)
})
}