refactor: replace all bare command string literals with named constants
All checks were successful
check / check (push) Successful in 2m15s

Extract cmdLusers, cmdMode, cmdMotd, cmdNames, cmdNotice, cmdPing, cmdPong
constants in internal/handlers/api.go. Add corresponding constants in
cmd/neoirc-cli/main.go and cmd/neoirc-cli/api/client.go. Replace every bare
IRC command string literal in switch cases and command dispatch code with the
named constant. Zero bare command strings remain in any dispatch path.
This commit is contained in:
2026-03-09 15:23:00 -07:00
parent a193831ed4
commit 5efb4b6949
3 changed files with 49 additions and 28 deletions

View File

@@ -19,6 +19,9 @@ const (
httpTimeout = 30 * time.Second
pollExtraTime = 5
httpErrThreshold = 400
cmdJoin = "JOIN"
cmdPart = "PART"
)
var errHTTP = errors.New("HTTP error")
@@ -168,7 +171,7 @@ func (client *Client) PollMessages(
func (client *Client) JoinChannel(channel string) error {
return client.SendMessage(
&Message{ //nolint:exhaustruct // only command+to needed
Command: "JOIN", To: channel,
Command: cmdJoin, To: channel,
},
)
}
@@ -177,7 +180,7 @@ func (client *Client) JoinChannel(channel string) error {
func (client *Client) PartChannel(channel string) error {
return client.SendMessage(
&Message{ //nolint:exhaustruct // only command+to needed
Command: "PART", To: channel,
Command: cmdPart, To: channel,
},
)
}

View File

@@ -16,6 +16,17 @@ const (
pollTimeout = 15
pollRetry = 2 * time.Second
timeFormat = "15:04"
cmdJoin = "JOIN"
cmdMotd = "MOTD"
cmdNick = "NICK"
cmdNotice = "NOTICE"
cmdPart = "PART"
cmdPrivmsg = "PRIVMSG"
cmdQuit = "QUIT"
cmdTopic = "TOPIC"
cmdWho = "WHO"
cmdWhois = "WHOIS"
)
// App holds the application state.
@@ -86,7 +97,7 @@ func (a *App) handleInput(text string) {
}
err := a.client.SendMessage(&api.Message{ //nolint:exhaustruct
Command: "PRIVMSG",
Command: cmdPrivmsg,
To: target,
Body: []string{text},
})
@@ -241,7 +252,7 @@ func (a *App) cmdNick(nick string) {
}
err := a.client.SendMessage(&api.Message{ //nolint:exhaustruct
Command: "NICK",
Command: cmdNick,
Body: []string{nick},
})
if err != nil {
@@ -376,7 +387,7 @@ func (a *App) cmdMsg(args string) {
}
err := a.client.SendMessage(&api.Message{ //nolint:exhaustruct
Command: "PRIVMSG",
Command: cmdPrivmsg,
To: target,
Body: []string{text},
})
@@ -434,7 +445,7 @@ func (a *App) cmdTopic(args string) {
if args == "" {
err := a.client.SendMessage(&api.Message{ //nolint:exhaustruct
Command: "TOPIC",
Command: cmdTopic,
To: target,
})
if err != nil {
@@ -447,7 +458,7 @@ func (a *App) cmdTopic(args string) {
}
err := a.client.SendMessage(&api.Message{ //nolint:exhaustruct
Command: "TOPIC",
Command: cmdTopic,
To: target,
Body: []string{args},
})
@@ -535,7 +546,7 @@ func (a *App) cmdMotd() {
}
err := a.client.SendMessage(
&api.Message{Command: "MOTD"}, //nolint:exhaustruct
&api.Message{Command: cmdMotd}, //nolint:exhaustruct
)
if err != nil {
a.ui.AddStatus(fmt.Sprintf(
@@ -572,7 +583,7 @@ func (a *App) cmdWho(args string) {
err := a.client.SendMessage(
&api.Message{ //nolint:exhaustruct
Command: "WHO", To: channel,
Command: cmdWho, To: channel,
},
)
if err != nil {
@@ -603,7 +614,7 @@ func (a *App) cmdWhois(args string) {
err := a.client.SendMessage(
&api.Message{ //nolint:exhaustruct
Command: "WHOIS", To: args,
Command: cmdWhois, To: args,
},
)
if err != nil {
@@ -653,7 +664,7 @@ func (a *App) cmdQuit() {
if a.connected && a.client != nil {
_ = a.client.SendMessage(
&api.Message{Command: "QUIT"}, //nolint:exhaustruct
&api.Message{Command: cmdQuit}, //nolint:exhaustruct
)
}
@@ -738,19 +749,19 @@ func (a *App) handleServerMessage(msg *api.Message) {
a.mu.Unlock()
switch msg.Command {
case "PRIVMSG":
case cmdPrivmsg:
a.handlePrivmsgEvent(msg, timestamp, myNick)
case "JOIN":
case cmdJoin:
a.handleJoinEvent(msg, timestamp)
case "PART":
case cmdPart:
a.handlePartEvent(msg, timestamp)
case "QUIT":
case cmdQuit:
a.handleQuitEvent(msg, timestamp)
case "NICK":
case cmdNick:
a.handleNickEvent(msg, timestamp, myNick)
case "NOTICE":
case cmdNotice:
a.handleNoticeEvent(msg, timestamp)
case "TOPIC":
case cmdTopic:
a.handleTopicEvent(msg, timestamp)
default:
a.handleDefaultEvent(msg, timestamp)

View File

@@ -29,8 +29,15 @@ const (
maxHistLimit = 500
cmdJoin = "JOIN"
cmdList = "LIST"
cmdLusers = "LUSERS"
cmdMode = "MODE"
cmdMotd = "MOTD"
cmdNames = "NAMES"
cmdNick = "NICK"
cmdNotice = "NOTICE"
cmdPart = "PART"
cmdPing = "PING"
cmdPong = "PONG"
cmdPrivmsg = "PRIVMSG"
cmdQuit = "QUIT"
cmdTopic = "TOPIC"
@@ -758,7 +765,7 @@ func (hdlr *Handlers) dispatchCommand(
bodyLines func() []string,
) {
switch command {
case cmdPrivmsg, "NOTICE":
case cmdPrivmsg, cmdNotice:
hdlr.handlePrivmsg(
writer, request,
sessionID, clientID, nick,
@@ -789,7 +796,7 @@ func (hdlr *Handlers) dispatchCommand(
hdlr.handleQuit(
writer, request, sessionID, nick, body,
)
case "MOTD", cmdList, cmdWho, cmdWhois, "PING":
case cmdMotd, cmdList, cmdWho, cmdWhois, cmdPing:
hdlr.dispatchInfoCommand(
writer, request,
sessionID, clientID, nick,
@@ -812,13 +819,13 @@ func (hdlr *Handlers) dispatchQueryCommand(
bodyLines func() []string,
) {
switch command {
case "MODE":
case cmdMode:
hdlr.handleMode(
writer, request,
sessionID, clientID, nick,
target, bodyLines,
)
case "NAMES":
case cmdNames:
hdlr.handleNames(
writer, request,
sessionID, clientID, nick, target,
@@ -839,7 +846,7 @@ func (hdlr *Handlers) dispatchQueryCommand(
writer, request,
sessionID, clientID, nick, target,
)
case "LUSERS":
case cmdLusers:
hdlr.handleLusers(
writer, request,
sessionID, clientID, nick,
@@ -1571,7 +1578,7 @@ func (hdlr *Handlers) dispatchInfoCommand(
okResp := map[string]string{"status": "ok"}
switch command {
case "MOTD":
case cmdMotd:
hdlr.deliverMOTD(
request, clientID, sessionID, nick,
)
@@ -1589,10 +1596,10 @@ func (hdlr *Handlers) dispatchInfoCommand(
request, clientID, sessionID, nick,
target, bodyLines,
)
case "PING":
case cmdPing:
hdlr.respondJSON(writer, request,
map[string]string{
"command": "PONG",
"command": cmdPong,
"from": hdlr.serverName(),
},
http.StatusOK)
@@ -1892,7 +1899,7 @@ func (hdlr *Handlers) handleMode(
if target == "" {
hdlr.respondIRCError(
writer, request, clientID, sessionID,
"461", nick, []string{"MODE"},
"461", nick, []string{cmdMode},
"Not enough parameters",
)
@@ -1981,7 +1988,7 @@ func (hdlr *Handlers) handleNames(
if target == "" {
hdlr.respondIRCError(
writer, request, clientID, sessionID,
"461", nick, []string{"NAMES"},
"461", nick, []string{cmdNames},
"Not enough parameters",
)