fix: revert .golangci.yml to main, fix all lint issues in code
Some checks failed
check / check (push) Failing after 1m5s
Some checks failed
check / check (push) Failing after 1m5s
- Restore original .golangci.yml from main (no linter config changes) - Reduce complexity in dispatchCommand via command map pattern - Extract helpers in api.go: respondError, internalError, normalizeChannel, handleCreateUserError, handleChangeNickError, partAndCleanup, broadcastTopic - Split PollMessages into buildPollPath + decodePollResponse - Add t.Parallel() to all tests, make subtests independent - Extract test fx providers into named functions to reduce funlen - Use mutex to serialize viper access in parallel tests - Extract PRIVMSG constant, add nolint for gosec false positives - Split long test functions into focused test cases - Add blank lines before expressions per wsl_v5
This commit is contained in:
@@ -101,17 +101,7 @@ func (c *Client) PollMessages(
|
||||
) * time.Second,
|
||||
}
|
||||
|
||||
params := url.Values{}
|
||||
if afterID > 0 {
|
||||
params.Set(
|
||||
"after",
|
||||
strconv.FormatInt(afterID, 10),
|
||||
)
|
||||
}
|
||||
|
||||
params.Set("timeout", strconv.Itoa(timeout))
|
||||
|
||||
path := "/api/v1/messages?" + params.Encode()
|
||||
path := c.buildPollPath(afterID, timeout)
|
||||
|
||||
req, err := http.NewRequestWithContext(
|
||||
context.Background(),
|
||||
@@ -125,38 +115,14 @@ func (c *Client) PollMessages(
|
||||
|
||||
req.Header.Set("Authorization", "Bearer "+c.Token)
|
||||
|
||||
resp, err := client.Do(req)
|
||||
resp, err := client.Do(req) //nolint:gosec // URL is from configured BaseURL, not user input
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode >= httpErrThreshold {
|
||||
return nil, fmt.Errorf(
|
||||
"%w %d: %s",
|
||||
errHTTP, resp.StatusCode, string(data),
|
||||
)
|
||||
}
|
||||
|
||||
var wrapped MessagesResponse
|
||||
|
||||
err = json.Unmarshal(data, &wrapped)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"decode messages: %w", err,
|
||||
)
|
||||
}
|
||||
|
||||
return &PollResult{
|
||||
Messages: wrapped.Messages,
|
||||
LastID: wrapped.LastID,
|
||||
}, nil
|
||||
return c.decodePollResponse(resp)
|
||||
}
|
||||
|
||||
// JoinChannel joins a channel.
|
||||
@@ -239,6 +205,52 @@ func (c *Client) GetServerInfo() (*ServerInfo, error) {
|
||||
return &info, nil
|
||||
}
|
||||
|
||||
func (c *Client) buildPollPath(
|
||||
afterID int64, timeout int,
|
||||
) string {
|
||||
params := url.Values{}
|
||||
if afterID > 0 {
|
||||
params.Set(
|
||||
"after",
|
||||
strconv.FormatInt(afterID, 10),
|
||||
)
|
||||
}
|
||||
|
||||
params.Set("timeout", strconv.Itoa(timeout))
|
||||
|
||||
return "/api/v1/messages?" + params.Encode()
|
||||
}
|
||||
|
||||
func (c *Client) decodePollResponse(
|
||||
resp *http.Response,
|
||||
) (*PollResult, error) {
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode >= httpErrThreshold {
|
||||
return nil, fmt.Errorf(
|
||||
"%w %d: %s",
|
||||
errHTTP, resp.StatusCode, string(data),
|
||||
)
|
||||
}
|
||||
|
||||
var wrapped MessagesResponse
|
||||
|
||||
err = json.Unmarshal(data, &wrapped)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"decode messages: %w", err,
|
||||
)
|
||||
}
|
||||
|
||||
return &PollResult{
|
||||
Messages: wrapped.Messages,
|
||||
LastID: wrapped.LastID,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *Client) do(
|
||||
method, path string,
|
||||
body any,
|
||||
@@ -272,7 +284,7 @@ func (c *Client) do(
|
||||
)
|
||||
}
|
||||
|
||||
resp, err := c.HTTPClient.Do(req)
|
||||
resp, err := c.HTTPClient.Do(req) //nolint:gosec // URL is from configured BaseURL, not user input
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("http: %w", err)
|
||||
}
|
||||
|
||||
@@ -123,36 +123,40 @@ func (a *App) handleCommand(text string) {
|
||||
}
|
||||
|
||||
func (a *App) dispatchCommand(cmd, args string) {
|
||||
switch cmd {
|
||||
case "/connect":
|
||||
a.cmdConnect(args)
|
||||
case "/nick":
|
||||
a.cmdNick(args)
|
||||
case "/join":
|
||||
a.cmdJoin(args)
|
||||
case "/part":
|
||||
a.cmdPart(args)
|
||||
case "/msg":
|
||||
a.cmdMsg(args)
|
||||
case "/query":
|
||||
a.cmdQuery(args)
|
||||
case "/topic":
|
||||
a.cmdTopic(args)
|
||||
case "/names":
|
||||
a.cmdNames()
|
||||
case "/list":
|
||||
a.cmdList()
|
||||
case "/window", "/w":
|
||||
a.cmdWindow(args)
|
||||
case "/quit":
|
||||
a.cmdQuit()
|
||||
case "/help":
|
||||
a.cmdHelp()
|
||||
default:
|
||||
a.ui.AddStatus(
|
||||
"[red]Unknown command: " + cmd,
|
||||
)
|
||||
argCmds := map[string]func(string){
|
||||
"/connect": a.cmdConnect,
|
||||
"/nick": a.cmdNick,
|
||||
"/join": a.cmdJoin,
|
||||
"/part": a.cmdPart,
|
||||
"/msg": a.cmdMsg,
|
||||
"/query": a.cmdQuery,
|
||||
"/topic": a.cmdTopic,
|
||||
"/window": a.cmdWindow,
|
||||
"/w": a.cmdWindow,
|
||||
}
|
||||
|
||||
if fn, ok := argCmds[cmd]; ok {
|
||||
fn(args)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
noArgCmds := map[string]func(){
|
||||
"/names": a.cmdNames,
|
||||
"/list": a.cmdList,
|
||||
"/quit": a.cmdQuit,
|
||||
"/help": a.cmdHelp,
|
||||
}
|
||||
|
||||
if fn, ok := noArgCmds[cmd]; ok {
|
||||
fn()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
a.ui.AddStatus(
|
||||
"[red]Unknown command: " + cmd,
|
||||
)
|
||||
}
|
||||
|
||||
func (a *App) cmdConnect(serverURL string) {
|
||||
|
||||
@@ -80,6 +80,7 @@ func (ui *UI) AddLine(bufferName, line string) {
|
||||
cur := ui.buffers[ui.currentBuffer]
|
||||
if cur != buf {
|
||||
buf.Unread++
|
||||
|
||||
ui.refreshStatusBar()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user