From afcf2c711b907514a91e5c4d45d2da7f118bb2b8 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sun, 17 Aug 2025 13:52:55 +0800 Subject: [PATCH] Lint ipc --- forged/.golangci.yaml | 1 + forged/internal/ipc/git2c/client.go | 13 +++++++++---- forged/internal/ipc/git2c/cmd_index.go | 6 ++++-- forged/internal/ipc/git2c/cmd_treeraw.go | 9 ++++++--- forged/internal/ipc/git2c/perror.go | 3 +-- forged/internal/ipc/irc/bot.go | 23 +++++++++++++++-------- forged/internal/ipc/irc/conn.go | 13 +++++++++++-- forged/internal/ipc/irc/message.go | 18 +++++++++--------- forged/internal/ipc/irc/source.go | 1 + diff --git a/forged/.golangci.yaml b/forged/.golangci.yaml index 7d93d90338b97c899a86f9c3a7f3827622d8665a..499136bb6475c378aa468bfe1360244d74a379f5 100644 --- a/forged/.golangci.yaml +++ b/forged/.golangci.yaml @@ -19,6 +19,7 @@ - lll - mnd # tmp - revive # tmp - godox # tmp + - nestif # tmp linters-settings: revive: diff --git a/forged/internal/ipc/git2c/client.go b/forged/internal/ipc/git2c/client.go index 9f1b55c26d963a3335a3819470510f466e57cce2..8b11035361c2befaaf2216a600f7f16c719fb873 100644 --- a/forged/internal/ipc/git2c/client.go +++ b/forged/internal/ipc/git2c/client.go @@ -4,6 +4,7 @@ package git2c import ( + "context" "fmt" "net" @@ -19,8 +20,9 @@ reader *bare.Reader } // NewClient establishes a connection to a git2d socket and returns a new Client. -func NewClient(socketPath string) (*Client, error) { - conn, err := net.Dial("unix", socketPath) +func NewClient(ctx context.Context, socketPath string) (*Client, error) { + dialer := &net.Dialer{} //exhaustruct:ignore + conn, err := dialer.DialContext(ctx, "unix", socketPath) if err != nil { return nil, fmt.Errorf("git2d connection failed: %w", err) } @@ -37,9 +39,12 @@ }, nil } // Close terminates the underlying socket connection. -func (c *Client) Close() error { +func (c *Client) Close() (err error) { if c.conn != nil { - return c.conn.Close() + err = c.conn.Close() + if err != nil { + return fmt.Errorf("close underlying socket: %w", err) + } } return nil } diff --git a/forged/internal/ipc/git2c/cmd_index.go b/forged/internal/ipc/git2c/cmd_index.go index 8862b2cd828ee970bdc8d42a54bbefd82aed6c23..e9fc435583ba38b6559da4643ec24210e8edcf46 100644 --- a/forged/internal/ipc/git2c/cmd_index.go +++ b/forged/internal/ipc/git2c/cmd_index.go @@ -13,10 +13,12 @@ // CmdIndex requests a repository index from git2d and returns the list of commits // and the contents of a README file if available. func (c *Client) CmdIndex(repoPath string) ([]Commit, *FilenameContents, error) { - if err := c.writer.WriteData([]byte(repoPath)); err != nil { + err := c.writer.WriteData([]byte(repoPath)) + if err != nil { return nil, nil, fmt.Errorf("sending repo path failed: %w", err) } - if err := c.writer.WriteUint(1); err != nil { + err = c.writer.WriteUint(1) + if err != nil { return nil, nil, fmt.Errorf("sending command failed: %w", err) } diff --git a/forged/internal/ipc/git2c/cmd_treeraw.go b/forged/internal/ipc/git2c/cmd_treeraw.go index 492cb849b1672a8d37622d693b2f27db0560e6c0..89b702c76d9cdc35cfce94dcdf0b8eb94debf393 100644 --- a/forged/internal/ipc/git2c/cmd_treeraw.go +++ b/forged/internal/ipc/git2c/cmd_treeraw.go @@ -12,13 +12,16 @@ // CmdTreeRaw queries git2d for a tree or blob object at the given path within the repository. // It returns either a directory listing or the contents of a file. func (c *Client) CmdTreeRaw(repoPath, pathSpec string) ([]TreeEntry, string, error) { - if err := c.writer.WriteData([]byte(repoPath)); err != nil { + err := c.writer.WriteData([]byte(repoPath)) + if err != nil { return nil, "", fmt.Errorf("sending repo path failed: %w", err) } - if err := c.writer.WriteUint(2); err != nil { + err = c.writer.WriteUint(2) + if err != nil { return nil, "", fmt.Errorf("sending command failed: %w", err) } - if err := c.writer.WriteData([]byte(pathSpec)); err != nil { + err = c.writer.WriteData([]byte(pathSpec)) + if err != nil { return nil, "", fmt.Errorf("sending path failed: %w", err) } diff --git a/forged/internal/ipc/git2c/perror.go b/forged/internal/ipc/git2c/perror.go index 96bffd58c70e25aec9ff5bc9a227a748bc1e7e65..6bc759586b44c086651ba39d4650927497ed9680 100644 --- a/forged/internal/ipc/git2c/perror.go +++ b/forged/internal/ipc/git2c/perror.go @@ -8,7 +8,6 @@ import "errors" var ( - Success error ErrUnknown = errors.New("git2c: unknown error") ErrPath = errors.New("git2c: get tree entry by path failed") ErrRevparse = errors.New("git2c: revparse failed") @@ -24,7 +23,7 @@ func Perror(errno uint) error { switch errno { case 0: - return Success + return nil case 3: return ErrPath case 4: diff --git a/forged/internal/ipc/irc/bot.go b/forged/internal/ipc/irc/bot.go index 611391380ca627919b3629d3b7d62c96a44e2079..c7a188b65cfc08bb74072d1c067df00e2045645e 100644 --- a/forged/internal/ipc/irc/bot.go +++ b/forged/internal/ipc/irc/bot.go @@ -4,7 +4,9 @@ package irc import ( + "context" "crypto/tls" + "fmt" "log/slog" "net" @@ -25,6 +27,7 @@ } // Bot represents an IRC bot client that handles events and allows for sending messages. type Bot struct { + // TODO: Use each config field instead of embedding Config here. config *Config ircSendBuffered chan string ircSendDirectChan chan misc.ErrorBack[string] @@ -34,24 +37,28 @@ // NewBot creates a new Bot instance using the provided configuration. func NewBot(c *Config) (b *Bot) { b = &Bot{ config: c, - } + } //exhaustruct:ignore return } // Connect establishes a new IRC session and starts handling incoming and outgoing messages. // This method blocks until an error occurs or the connection is closed. -func (b *Bot) Connect() error { +func (b *Bot) Connect(ctx context.Context) error { var err error var underlyingConn net.Conn if b.config.TLS { - underlyingConn, err = tls.Dial(b.config.Net, b.config.Addr, nil) + dialer := tls.Dialer{} //exhaustruct:ignore + underlyingConn, err = dialer.DialContext(ctx, b.config.Net, b.config.Addr) } else { - underlyingConn, err = net.Dial(b.config.Net, b.config.Addr) + dialer := net.Dialer{} //exhaustruct:ignore + underlyingConn, err = dialer.DialContext(ctx, b.config.Net, b.config.Addr) } if err != nil { - return err + return fmt.Errorf("dialing irc: %w", err) } - defer underlyingConn.Close() + defer func() { + _ = underlyingConn.Close() + }() conn := NewConn(underlyingConn) @@ -164,12 +171,12 @@ } // ConnectLoop continuously attempts to maintain an IRC session. // If the connection drops, it automatically retries with no delay. -func (b *Bot) ConnectLoop() { +func (b *Bot) ConnectLoop(ctx context.Context) { b.ircSendBuffered = make(chan string, b.config.SendQ) b.ircSendDirectChan = make(chan misc.ErrorBack[string]) for { - err := b.Connect() + err := b.Connect(ctx) slog.Error("irc session error", "error", err) } } diff --git a/forged/internal/ipc/irc/conn.go b/forged/internal/ipc/irc/conn.go index 15294eed322c90eed725b415bad43eefa0c641c8..b9b208cd120c1e0c99d209865d3dbbe8a57632ff 100644 --- a/forged/internal/ipc/irc/conn.go +++ b/forged/internal/ipc/irc/conn.go @@ -2,6 +2,7 @@ package irc import ( "bufio" + "fmt" "net" "slices" @@ -41,9 +42,17 @@ return } func (c *Conn) Write(p []byte) (n int, err error) { - return c.netConn.Write(p) + n, err = c.netConn.Write(p) + if err != nil { + err = fmt.Errorf("write to connection: %w", err) + } + return n, err } func (c *Conn) WriteString(s string) (n int, err error) { - return c.netConn.Write(misc.StringToBytes(s)) + n, err = c.netConn.Write(misc.StringToBytes(s)) + if err != nil { + err = fmt.Errorf("write to connection: %w", err) + } + return n, err } diff --git a/forged/internal/ipc/irc/message.go b/forged/internal/ipc/irc/message.go index 5843226905e7b1207365ccd7909028149bcf509a..3387becde0175427063abca1003c2666ebb09178 100644 --- a/forged/internal/ipc/irc/message.go +++ b/forged/internal/ipc/irc/message.go @@ -24,18 +24,18 @@ if bytes.HasPrefix(sp[0], []byte{'@'}) { // TODO: Check size manually if len(sp[0]) < 2 { err = ErrMalformedMsg - return + return msg, err } sp[0] = sp[0][1:] msg.Tags, err = tagsToMap(sp[0]) if err != nil { - return + return msg, err } if len(sp) < 2 { err = ErrMalformedMsg - return + return msg, err } sp = sp[1:] } else { @@ -45,7 +45,7 @@ if bytes.HasPrefix(sp[0], []byte{':'}) { // TODO: Check size manually if len(sp[0]) < 2 { err = ErrMalformedMsg - return + return msg, err } sp[0] = sp[0][1:] @@ -53,14 +53,14 @@ msg.Source = parseSource(sp[0]) if len(sp) < 2 { err = ErrMalformedMsg - return + return msg, err } sp = sp[1:] } msg.Command = misc.BytesToString(sp[0]) if len(sp) < 2 { - return + return msg, err } sp = sp[1:] @@ -81,7 +81,7 @@ } msg.Args = append(msg.Args, misc.BytesToString(sp[i])) } - return + return msg, err } var ircv3TagEscapes = map[byte]byte{ //nolint:gochecknoglobals @@ -97,7 +97,7 @@ for rawTag := range bytes.SplitSeq(raw, []byte{';'}) { key, value, found := bytes.Cut(rawTag, []byte{'='}) if !found { err = ErrInvalidIRCv3Tag - return + return tags, err } if len(value) == 0 { tags[misc.BytesToString(key)] = "" @@ -122,5 +122,5 @@ tags[misc.BytesToString(key)] = misc.BytesToString(valueUnescaped.Bytes()) } } } - return + return tags, err } diff --git a/forged/internal/ipc/irc/source.go b/forged/internal/ipc/irc/source.go index c6baf75adb4c1eca78971a26649d26e042059053..938751f4758107f11b56ed2aa50449de3dc4e016 100644 --- a/forged/internal/ipc/irc/source.go +++ b/forged/internal/ipc/irc/source.go @@ -13,6 +13,7 @@ type Source interface { AsSourceString() string } +//nolint:ireturn func parseSource(s []byte) Source { nick, userhost, found := bytes.Cut(s, []byte{'!'}) if !found { -- 2.48.1