style: fix all golangci-lint issues and format code (refs #17)

Fix 380 lint violations across all Go source files including wsl_v5,
nlreturn, noinlineerr, errcheck, funlen, funcorder, tagliatelle,
perfsprint, modernize, revive, gosec, ireturn, mnd, forcetypeassert,
cyclop, and others.

Key changes:
- Split large handler/command functions into smaller methods
- Extract scan helpers for database queries
- Reorder exported/unexported methods per funcorder
- Add sentinel errors in models package
- Use camelCase JSON tags per tagliatelle defaults
- Add package comments
- Fix .gitignore to not exclude cmd/chat-cli directory
This commit is contained in:
clawbot
2026-02-26 06:27:56 -08:00
parent 636546d74a
commit b78d526f02
13 changed files with 1920 additions and 753 deletions

View File

@@ -1,8 +1,10 @@
// Package main implements chat-cli, an IRC-style terminal client.
package main
import (
"fmt"
"os"
"strconv"
"strings"
"sync"
"time"
@@ -10,6 +12,12 @@ import (
"git.eeqj.de/sneak/chat/cmd/chat-cli/api"
)
const (
pollTimeoutSec = 15
retryDelay = 2 * time.Second
maxNickLength = 32
)
// App holds the application state.
type App struct {
ui *UI
@@ -32,11 +40,18 @@ func main() {
app.ui.OnInput(app.handleInput)
app.ui.SetStatus(app.nick, "", "disconnected")
app.ui.AddStatus("Welcome to chat-cli — an IRC-style client")
app.ui.AddStatus("Type [yellow]/connect <server-url>[white] to begin, or [yellow]/help[white] for commands")
app.ui.AddStatus(
"Welcome to chat-cli \u2014 an IRC-style client",
)
app.ui.AddStatus(
"Type [yellow]/connect <server-url>[white] " +
"to begin, or [yellow]/help[white] for commands",
)
err := app.ui.Run()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Error: %v\n", err)
if err := app.ui.Run(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
@@ -44,21 +59,34 @@ func main() {
func (a *App) handleInput(text string) {
if strings.HasPrefix(text, "/") {
a.handleCommand(text)
return
}
// Plain text → PRIVMSG to current target.
a.sendPlainText(text)
}
func (a *App) sendPlainText(text string) {
a.mu.Lock()
target := a.target
connected := a.connected
nick := a.nick
a.mu.Unlock()
if !connected {
a.ui.AddStatus("[red]Not connected. Use /connect <url>")
a.ui.AddStatus(
"[red]Not connected. Use /connect <url>",
)
return
}
if target == "" {
a.ui.AddStatus("[red]No target. Use /join #channel or /query nick")
a.ui.AddStatus(
"[red]No target. " +
"Use /join #channel or /query nick",
)
return
}
@@ -68,21 +96,28 @@ func (a *App) handleInput(text string) {
Body: []string{text},
})
if err != nil {
a.ui.AddStatus(fmt.Sprintf("[red]Send error: %v", err))
a.ui.AddStatus(
fmt.Sprintf("[red]Send error: %v", err),
)
return
}
// Echo locally.
ts := time.Now().Format("15:04")
a.mu.Lock()
nick := a.nick
a.mu.Unlock()
a.ui.AddLine(target, fmt.Sprintf("[gray]%s [green]<%s>[white] %s", ts, nick, text))
a.ui.AddLine(
target,
fmt.Sprintf(
"[gray]%s [green]<%s>[white] %s",
ts, nick, text,
),
)
}
func (a *App) handleCommand(text string) {
parts := strings.SplitN(text, " ", 2)
func (a *App) handleCommand(text string) { //nolint:cyclop // command dispatch
parts := strings.SplitN(text, " ", 2) //nolint:mnd // split into cmd+args
cmd := strings.ToLower(parts[0])
args := ""
if len(parts) > 1 {
args = parts[1]
@@ -114,27 +149,41 @@ func (a *App) handleCommand(text string) {
case "/help":
a.cmdHelp()
default:
a.ui.AddStatus(fmt.Sprintf("[red]Unknown command: %s", cmd))
a.ui.AddStatus(
"[red]Unknown command: " + cmd,
)
}
}
func (a *App) cmdConnect(serverURL string) {
if serverURL == "" {
a.ui.AddStatus("[red]Usage: /connect <server-url>")
a.ui.AddStatus(
"[red]Usage: /connect <server-url>",
)
return
}
serverURL = strings.TrimRight(serverURL, "/")
a.ui.AddStatus(fmt.Sprintf("Connecting to %s...", serverURL))
a.ui.AddStatus(
fmt.Sprintf("Connecting to %s...", serverURL),
)
a.mu.Lock()
nick := a.nick
a.mu.Unlock()
client := api.NewClient(serverURL)
resp, err := client.CreateSession(nick)
if err != nil {
a.ui.AddStatus(fmt.Sprintf("[red]Connection failed: %v", err))
a.ui.AddStatus(
fmt.Sprintf(
"[red]Connection failed: %v", err,
),
)
return
}
@@ -145,19 +194,26 @@ func (a *App) cmdConnect(serverURL string) {
a.lastMsgID = ""
a.mu.Unlock()
a.ui.AddStatus(fmt.Sprintf("[green]Connected! Nick: %s, Session: %s", resp.Nick, resp.SessionID))
a.ui.AddStatus(
fmt.Sprintf(
"[green]Connected! Nick: %s, Session: %s",
resp.Nick, resp.SessionID,
),
)
a.ui.SetStatus(resp.Nick, "", "connected")
// Start polling.
a.stopPoll = make(chan struct{})
go a.pollLoop()
}
func (a *App) cmdNick(nick string) {
if nick == "" {
a.ui.AddStatus("[red]Usage: /nick <name>")
return
}
a.mu.Lock()
connected := a.connected
a.mu.Unlock()
@@ -166,7 +222,14 @@ func (a *App) cmdNick(nick string) {
a.mu.Lock()
a.nick = nick
a.mu.Unlock()
a.ui.AddStatus(fmt.Sprintf("Nick set to %s (will be used on connect)", nick))
a.ui.AddStatus(
fmt.Sprintf(
"Nick set to %s (will be used on connect)",
nick,
),
)
return
}
@@ -175,7 +238,12 @@ func (a *App) cmdNick(nick string) {
Body: []string{nick},
})
if err != nil {
a.ui.AddStatus(fmt.Sprintf("[red]Nick change failed: %v", err))
a.ui.AddStatus(
fmt.Sprintf(
"[red]Nick change failed: %v", err,
),
)
return
}
@@ -183,15 +251,20 @@ func (a *App) cmdNick(nick string) {
a.nick = nick
target := a.target
a.mu.Unlock()
a.ui.SetStatus(nick, target, "connected")
a.ui.AddStatus(fmt.Sprintf("Nick changed to %s", nick))
a.ui.AddStatus(
"Nick changed to " + nick,
)
}
func (a *App) cmdJoin(channel string) {
if channel == "" {
a.ui.AddStatus("[red]Usage: /join #channel")
return
}
if !strings.HasPrefix(channel, "#") {
channel = "#" + channel
}
@@ -199,14 +272,19 @@ func (a *App) cmdJoin(channel string) {
a.mu.Lock()
connected := a.connected
a.mu.Unlock()
if !connected {
a.ui.AddStatus("[red]Not connected")
return
}
err := a.client.JoinChannel(channel)
if err != nil {
a.ui.AddStatus(fmt.Sprintf("[red]Join failed: %v", err))
a.ui.AddStatus(
fmt.Sprintf("[red]Join failed: %v", err),
)
return
}
@@ -216,39 +294,55 @@ func (a *App) cmdJoin(channel string) {
a.mu.Unlock()
a.ui.SwitchToBuffer(channel)
a.ui.AddLine(channel, fmt.Sprintf("[yellow]*** Joined %s", channel))
a.ui.AddLine(
channel,
"[yellow]*** Joined "+channel,
)
a.ui.SetStatus(nick, channel, "connected")
}
func (a *App) cmdPart(channel string) {
a.mu.Lock()
if channel == "" {
channel = a.target
}
connected := a.connected
a.mu.Unlock()
if channel == "" || !strings.HasPrefix(channel, "#") {
a.ui.AddStatus("[red]No channel to part")
return
}
if !connected {
a.ui.AddStatus("[red]Not connected")
return
}
err := a.client.PartChannel(channel)
if err != nil {
a.ui.AddStatus(fmt.Sprintf("[red]Part failed: %v", err))
a.ui.AddStatus(
fmt.Sprintf("[red]Part failed: %v", err),
)
return
}
a.ui.AddLine(channel, fmt.Sprintf("[yellow]*** Left %s", channel))
a.ui.AddLine(
channel,
"[yellow]*** Left "+channel,
)
a.mu.Lock()
if a.target == channel {
a.target = ""
}
nick := a.nick
a.mu.Unlock()
@@ -257,19 +351,23 @@ 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, " ", 2) //nolint:mnd // split into target+text
if len(parts) < 2 { //nolint:mnd // min args
a.ui.AddStatus("[red]Usage: /msg <nick> <text>")
return
}
target, text := parts[0], parts[1]
a.mu.Lock()
connected := a.connected
nick := a.nick
a.mu.Unlock()
if !connected {
a.ui.AddStatus("[red]Not connected")
return
}
@@ -279,17 +377,28 @@ func (a *App) cmdMsg(args string) {
Body: []string{text},
})
if err != nil {
a.ui.AddStatus(fmt.Sprintf("[red]Send failed: %v", err))
a.ui.AddStatus(
fmt.Sprintf("[red]Send failed: %v", err),
)
return
}
ts := time.Now().Format("15:04")
a.ui.AddLine(target, fmt.Sprintf("[gray]%s [green]<%s>[white] %s", ts, nick, text))
a.ui.AddLine(
target,
fmt.Sprintf(
"[gray]%s [green]<%s>[white] %s",
ts, nick, text,
),
)
}
func (a *App) cmdQuery(nick string) {
if nick == "" {
a.ui.AddStatus("[red]Usage: /query <nick>")
return
}
@@ -310,22 +419,29 @@ func (a *App) cmdTopic(args string) {
if !connected {
a.ui.AddStatus("[red]Not connected")
return
}
if !strings.HasPrefix(target, "#") {
a.ui.AddStatus("[red]Not in a channel")
return
}
if args == "" {
// Query topic.
err := a.client.SendMessage(&api.Message{
Command: "TOPIC",
To: target,
})
if err != nil {
a.ui.AddStatus(fmt.Sprintf("[red]Topic query failed: %v", err))
a.ui.AddStatus(
fmt.Sprintf(
"[red]Topic query failed: %v", err,
),
)
}
return
}
@@ -335,7 +451,11 @@ func (a *App) cmdTopic(args string) {
Body: []string{args},
})
if err != nil {
a.ui.AddStatus(fmt.Sprintf("[red]Topic set failed: %v", err))
a.ui.AddStatus(
fmt.Sprintf(
"[red]Topic set failed: %v", err,
),
)
}
}
@@ -347,20 +467,32 @@ func (a *App) cmdNames() {
if !connected {
a.ui.AddStatus("[red]Not connected")
return
}
if !strings.HasPrefix(target, "#") {
a.ui.AddStatus("[red]Not in a channel")
return
}
members, err := a.client.GetMembers(target)
if err != nil {
a.ui.AddStatus(fmt.Sprintf("[red]Names failed: %v", err))
a.ui.AddStatus(
fmt.Sprintf("[red]Names failed: %v", err),
)
return
}
a.ui.AddLine(target, fmt.Sprintf("[cyan]*** Members of %s: %s", target, strings.Join(members, " ")))
a.ui.AddLine(
target,
fmt.Sprintf(
"[cyan]*** Members of %s: %s",
target, strings.Join(members, " "),
),
)
}
func (a *App) cmdList() {
@@ -370,46 +502,55 @@ func (a *App) cmdList() {
if !connected {
a.ui.AddStatus("[red]Not connected")
return
}
channels, err := a.client.ListChannels()
if err != nil {
a.ui.AddStatus(fmt.Sprintf("[red]List failed: %v", err))
a.ui.AddStatus(
fmt.Sprintf("[red]List failed: %v", err),
)
return
}
a.ui.AddStatus("[cyan]*** Channel list:")
for _, ch := range channels {
a.ui.AddStatus(fmt.Sprintf(" %s (%d members) %s", ch.Name, ch.Members, ch.Topic))
a.ui.AddStatus(
fmt.Sprintf(
" %s (%d members) %s",
ch.Name, ch.Members, ch.Topic,
),
)
}
a.ui.AddStatus("[cyan]*** End of channel list")
}
func (a *App) cmdWindow(args string) {
if args == "" {
a.ui.AddStatus("[red]Usage: /window <number>")
return
}
n := 0
fmt.Sscanf(args, "%d", &n)
n, _ := strconv.Atoi(args)
a.ui.SwitchBuffer(n)
a.mu.Lock()
if n < a.ui.BufferCount() && n >= 0 {
// Update target to the buffer name.
// Needs to be done carefully.
}
nick := a.nick
a.mu.Unlock()
// Update target based on buffer.
if n < a.ui.BufferCount() {
if n >= 0 && n < a.ui.BufferCount() {
buf := a.ui.buffers[n]
if buf.Name != "(status)" {
a.mu.Lock()
a.target = buf.Name
a.mu.Unlock()
a.ui.SetStatus(nick, buf.Name, "connected")
} else {
a.ui.SetStatus(nick, "", "connected")
@@ -419,12 +560,17 @@ func (a *App) cmdWindow(args string) {
func (a *App) cmdQuit() {
a.mu.Lock()
if a.connected && a.client != nil {
_ = a.client.SendMessage(&api.Message{Command: "QUIT"})
_ = a.client.SendMessage(
&api.Message{Command: "QUIT"},
)
}
if a.stopPoll != nil {
close(a.stopPoll)
}
a.mu.Unlock()
a.ui.Stop()
}
@@ -432,20 +578,21 @@ func (a *App) cmdQuit() {
func (a *App) cmdHelp() {
help := []string{
"[cyan]*** chat-cli commands:",
" /connect <url> Connect to server",
" /nick <name> Change nickname",
" /join #channel Join channel",
" /part [#chan] Leave channel",
" /msg <nick> <text> Send DM",
" /query <nick> Open DM window",
" /topic [text] View/set topic",
" /names List channel members",
" /list List channels",
" /window <n> Switch buffer (Alt+0-9)",
" /quit Disconnect and exit",
" /help This help",
" /connect <url> \u2014 Connect to server",
" /nick <name> \u2014 Change nickname",
" /join #channel \u2014 Join channel",
" /part [#chan] \u2014 Leave channel",
" /msg <nick> <text> \u2014 Send DM",
" /query <nick> \u2014 Open DM window",
" /topic [text] \u2014 View/set topic",
" /names \u2014 List channel members",
" /list \u2014 List channels",
" /window <n> \u2014 Switch buffer (Alt+0-9)",
" /quit \u2014 Disconnect and exit",
" /help \u2014 This help",
" Plain text sends to current target.",
}
for _, line := range help {
a.ui.AddStatus(line)
}
@@ -469,32 +616,31 @@ func (a *App) pollLoop() {
return
}
msgs, err := client.PollMessages(lastID, 15)
msgs, err := client.PollMessages(
lastID, pollTimeoutSec,
)
if err != nil {
// Transient error — retry after delay.
time.Sleep(2 * time.Second)
time.Sleep(retryDelay)
continue
}
for _, msg := range msgs {
a.handleServerMessage(&msg)
if msg.ID != "" {
for i := range msgs {
a.handleServerMessage(&msgs[i])
if msgs[i].ID != "" {
a.mu.Lock()
a.lastMsgID = msg.ID
a.lastMsgID = msgs[i].ID
a.mu.Unlock()
}
}
}
}
func (a *App) handleServerMessage(msg *api.Message) {
ts := ""
if msg.TS != "" {
t := msg.ParseTS()
ts = t.Local().Format("15:04")
} else {
ts = time.Now().Format("15:04")
}
func (a *App) handleServerMessage(
msg *api.Message,
) {
ts := a.parseMessageTS(msg)
a.mu.Lock()
myNick := a.nick
@@ -502,79 +648,203 @@ func (a *App) handleServerMessage(msg *api.Message) {
switch msg.Command {
case "PRIVMSG":
lines := msg.BodyLines()
text := strings.Join(lines, " ")
if msg.From == myNick {
// Skip our own echoed messages (already displayed locally).
return
}
target := msg.To
if !strings.HasPrefix(target, "#") {
// DM — use sender's nick as buffer name.
target = msg.From
}
a.ui.AddLine(target, fmt.Sprintf("[gray]%s [green]<%s>[white] %s", ts, msg.From, text))
a.handlePrivmsgMsg(msg, ts, myNick)
case "JOIN":
target := msg.To
if target != "" {
a.ui.AddLine(target, fmt.Sprintf("[gray]%s [yellow]*** %s has joined %s", ts, msg.From, target))
}
a.handleJoinMsg(msg, ts)
case "PART":
target := msg.To
lines := msg.BodyLines()
reason := strings.Join(lines, " ")
if target != "" {
if reason != "" {
a.ui.AddLine(target, fmt.Sprintf("[gray]%s [yellow]*** %s has left %s (%s)", ts, msg.From, target, reason))
} else {
a.ui.AddLine(target, fmt.Sprintf("[gray]%s [yellow]*** %s has left %s", ts, msg.From, target))
}
}
a.handlePartMsg(msg, ts)
case "QUIT":
lines := msg.BodyLines()
reason := strings.Join(lines, " ")
if reason != "" {
a.ui.AddStatus(fmt.Sprintf("[gray]%s [yellow]*** %s has quit (%s)", ts, msg.From, reason))
} else {
a.ui.AddStatus(fmt.Sprintf("[gray]%s [yellow]*** %s has quit", ts, msg.From))
}
a.handleQuitMsg(msg, ts)
case "NICK":
lines := msg.BodyLines()
newNick := ""
if len(lines) > 0 {
newNick = lines[0]
}
if msg.From == myNick && newNick != "" {
a.mu.Lock()
a.nick = newNick
target := a.target
a.mu.Unlock()
a.ui.SetStatus(newNick, target, "connected")
}
a.ui.AddStatus(fmt.Sprintf("[gray]%s [yellow]*** %s is now known as %s", ts, msg.From, newNick))
a.handleNickMsg(msg, ts, myNick)
case "NOTICE":
lines := msg.BodyLines()
text := strings.Join(lines, " ")
a.ui.AddStatus(fmt.Sprintf("[gray]%s [magenta]--%s-- %s", ts, msg.From, text))
a.handleNoticeMsg(msg, ts)
case "TOPIC":
lines := msg.BodyLines()
text := strings.Join(lines, " ")
if msg.To != "" {
a.ui.AddLine(msg.To, fmt.Sprintf("[gray]%s [cyan]*** %s set topic: %s", ts, msg.From, text))
}
a.handleTopicMsg(msg, ts)
default:
// Numeric replies and other messages → status window.
lines := msg.BodyLines()
text := strings.Join(lines, " ")
if text != "" {
a.ui.AddStatus(fmt.Sprintf("[gray]%s [white][%s] %s", ts, msg.Command, text))
}
a.handleDefaultMsg(msg, ts)
}
}
func (a *App) parseMessageTS(msg *api.Message) string {
if msg.TS != "" {
t := msg.ParseTS()
return t.In(time.Local).Format("15:04") //nolint:gosmopolitan // CLI uses local time
}
return time.Now().Format("15:04")
}
func (a *App) handlePrivmsgMsg(
msg *api.Message,
ts, myNick string,
) {
lines := msg.BodyLines()
text := strings.Join(lines, " ")
if msg.From == myNick {
return
}
target := msg.To
if !strings.HasPrefix(target, "#") {
target = msg.From
}
a.ui.AddLine(
target,
fmt.Sprintf(
"[gray]%s [green]<%s>[white] %s",
ts, msg.From, text,
),
)
}
func (a *App) handleJoinMsg(
msg *api.Message, ts string,
) {
target := msg.To
if target == "" {
return
}
a.ui.AddLine(
target,
fmt.Sprintf(
"[gray]%s [yellow]*** %s has joined %s",
ts, msg.From, target,
),
)
}
func (a *App) handlePartMsg(
msg *api.Message, ts string,
) {
target := msg.To
if target == "" {
return
}
lines := msg.BodyLines()
reason := strings.Join(lines, " ")
if reason != "" {
a.ui.AddLine(
target,
fmt.Sprintf(
"[gray]%s [yellow]*** %s has left %s (%s)",
ts, msg.From, target, reason,
),
)
} else {
a.ui.AddLine(
target,
fmt.Sprintf(
"[gray]%s [yellow]*** %s has left %s",
ts, msg.From, target,
),
)
}
}
func (a *App) handleQuitMsg(
msg *api.Message, ts string,
) {
lines := msg.BodyLines()
reason := strings.Join(lines, " ")
if reason != "" {
a.ui.AddStatus(
fmt.Sprintf(
"[gray]%s [yellow]*** %s has quit (%s)",
ts, msg.From, reason,
),
)
} else {
a.ui.AddStatus(
fmt.Sprintf(
"[gray]%s [yellow]*** %s has quit",
ts, msg.From,
),
)
}
}
func (a *App) handleNickMsg(
msg *api.Message, ts, myNick string,
) {
lines := msg.BodyLines()
newNick := ""
if len(lines) > 0 {
newNick = lines[0]
}
if msg.From == myNick && newNick != "" {
a.mu.Lock()
a.nick = newNick
target := a.target
a.mu.Unlock()
a.ui.SetStatus(newNick, target, "connected")
}
a.ui.AddStatus(
fmt.Sprintf(
"[gray]%s [yellow]*** %s is now known as %s",
ts, msg.From, newNick,
),
)
}
func (a *App) handleNoticeMsg(
msg *api.Message, ts string,
) {
lines := msg.BodyLines()
text := strings.Join(lines, " ")
a.ui.AddStatus(
fmt.Sprintf(
"[gray]%s [magenta]--%s-- %s",
ts, msg.From, text,
),
)
}
func (a *App) handleTopicMsg(
msg *api.Message, ts string,
) {
if msg.To == "" {
return
}
lines := msg.BodyLines()
text := strings.Join(lines, " ")
a.ui.AddLine(
msg.To,
fmt.Sprintf(
"[gray]%s [cyan]*** %s set topic: %s",
ts, msg.From, text,
),
)
}
func (a *App) handleDefaultMsg(
msg *api.Message, ts string,
) {
lines := msg.BodyLines()
text := strings.Join(lines, " ")
if text == "" {
return
}
a.ui.AddStatus(
fmt.Sprintf(
"[gray]%s [white][%s] %s",
ts, msg.Command, text,
),
)
}