diff --git a/base/output.go b/base/output.go index 588b2c69..e9c14f77 100644 --- a/base/output.go +++ b/base/output.go @@ -37,18 +37,18 @@ func (d *DebugOutput) Config() *ChatDebugOutputConfig { return d.config } -func (d *DebugOutput) Debug(format string, args ...interface{}) { +func (d *DebugOutput) Debug(format string, args ...any) { msg := fmt.Sprintf(format, args...) fmt.Printf("%s: %s\n", d.name, msg) } -func (d *DebugOutput) Errorf(msg string, args ...interface{}) { +func (d *DebugOutput) Errorf(msg string, args ...any) { d.Debug(msg, args...) msg = fmt.Sprintf("```%s```", msg) d.Report(msg, args...) } -func (d *DebugOutput) Report(msg string, args ...interface{}) { +func (d *DebugOutput) Report(msg string, args ...any) { if d.config == nil { d.Debug("Errorf: Unable to report error to chat, errReportConv, chat debug not configured") } else if d.config.ErrReportConv == "" || d.config.KBC == nil { @@ -61,27 +61,27 @@ func (d *DebugOutput) Report(msg string, args ...interface{}) { } } -func (d *DebugOutput) ChatDebug(convID chat1.ConvIDStr, msg string, args ...interface{}) { +func (d *DebugOutput) ChatDebug(convID chat1.ConvIDStr, msg string, args ...any) { d.Debug(msg, args...) if _, err := d.config.KBC.SendMessageByConvID(convID, "Something went wrong!"); err != nil && !IsDeletedConvError(err) { d.Errorf("ChatDebug: failed to send error message: %s", err) } } -func (d *DebugOutput) ChatErrorf(convID chat1.ConvIDStr, msg string, args ...interface{}) { +func (d *DebugOutput) ChatErrorf(convID chat1.ConvIDStr, msg string, args ...any) { d.Errorf(msg, args...) if _, err := d.config.KBC.SendMessageByConvID(convID, "Something went wrong!"); err != nil && !IsDeletedConvError(err) { d.Errorf("ChatErrorf: failed to send error message: %s", err) } } -func (d *DebugOutput) ChatEcho(convID chat1.ConvIDStr, msg string, args ...interface{}) { +func (d *DebugOutput) ChatEcho(convID chat1.ConvIDStr, msg string, args ...any) { if _, err := d.config.KBC.SendMessageByConvID(convID, msg, args...); err != nil && !IsDeletedConvError(err) { d.Errorf("ChatEcho: failed to send echo message: %s", err) } } -func (d *DebugOutput) Trace(err *error, format string, args ...interface{}) func() { +func (d *DebugOutput) Trace(err *error, format string, args ...any) func() { msg := fmt.Sprintf(format, args...) start := time.Now() fmt.Printf("+ %s: %s\n", d.name, msg) diff --git a/base/server.go b/base/server.go index 1686e6c7..04b1cc14 100644 --- a/base/server.go +++ b/base/server.go @@ -6,6 +6,7 @@ import ( "io" "os" "os/signal" + "slices" "strings" "sync" "syscall" @@ -259,12 +260,7 @@ func (s *Server) listenForConvs(shutdownCh chan struct{}, sub *kbchat.Subscripti } func (s *Server) allowHiddenCommand(msg chat1.MsgSummary) bool { - for _, username := range s.botAdmins { - if username == msg.Sender.Username { - return true - } - } - return false + return slices.Contains(s.botAdmins, msg.Sender.Username) } func (s *Server) handleLogSend(msg chat1.MsgSummary) error { diff --git a/base/stats.go b/base/stats.go index 6eb2969d..bda9536a 100644 --- a/base/stats.go +++ b/base/stats.go @@ -83,7 +83,7 @@ func (s *StathatBackend) Shutdown() error { return nil } -func NewStatsBackend(btype StatsBackendType, config interface{}) (StatsBackend, error) { +func NewStatsBackend(btype StatsBackendType, config any) (StatsBackend, error) { switch btype { case StathatStatsBackendType: cfg, ok := config.(StathatConfig) diff --git a/base/util.go b/base/util.go index 2fe47738..e64db218 100644 --- a/base/util.go +++ b/base/util.go @@ -28,12 +28,12 @@ var DefaultBotAdmins = []string{ "mikem", } -func MsgpackDecode(dst interface{}, src []byte) error { +func MsgpackDecode(dst any, src []byte) error { h := codecHandle() return codec.NewDecoderBytes(src, h).Decode(dst) } -func MsgpackEncode(src interface{}) ([]byte, error) { +func MsgpackEncode(src any) ([]byte, error) { h := codecHandle() var ret []byte err := codec.NewEncoderBytes(&ret, h).Encode(src) @@ -279,7 +279,7 @@ func MakeSecret(repo string, convID chat1.ConvIDStr, secret string) string { return fmt.Sprintf("%x", sha256.Sum256([]byte(repo+string(ShortConvID(convID))+secret))) } -func SendByConvNameOrID(kbc *kbchat.API, debugOutput *DebugOutput, name, msg string, args ...interface{}) (err error) { +func SendByConvNameOrID(kbc *kbchat.API, debugOutput *DebugOutput, name, msg string, args ...any) (err error) { if _, err = kbc.SendMessageByConvID(chat1.ConvIDStr(name), msg, args...); err == nil { return nil } diff --git a/elastiwatch/elastiwatch/grouper.go b/elastiwatch/elastiwatch/grouper.go index 834f56cd..b99e6472 100644 --- a/elastiwatch/elastiwatch/grouper.go +++ b/elastiwatch/elastiwatch/grouper.go @@ -126,10 +126,7 @@ func (t *treeifyGrouper) buildForest(trees []*tree) (forest []*tree) { return trees } numParts := len(trees[0].words) - minDist := numParts - if minDist > t.maxGroups { - minDist = t.maxGroups - } + minDist := min(numParts, t.maxGroups) for _, loneTree := range trees { consumed := false for _, forestTree := range forest { diff --git a/elastiwatch/elastiwatch/handler.go b/elastiwatch/elastiwatch/handler.go index 7a7feb46..1abb7e48 100644 --- a/elastiwatch/elastiwatch/handler.go +++ b/elastiwatch/elastiwatch/handler.go @@ -51,15 +51,15 @@ func (h *Handler) handleDeferrals(convID chat1.ConvIDStr, _ string) error { if err != nil { return err } - body := "" + var body strings.Builder if len(deferrals) == 0 { h.ChatEcho(convID, "No deferrals in use") return nil } for _, d := range deferrals { - body += fmt.Sprintf("id: %d author: %s regex: %s (created: %v)\n", d.ID, d.Author, d.Regex, d.Ctime) + body.WriteString(fmt.Sprintf("id: %d author: %s regex: %s (created: %v)\n", d.ID, d.Author, d.Regex, d.Ctime)) } - h.ChatEcho(convID, "%s", body) + h.ChatEcho(convID, "%s", body.String()) return nil } diff --git a/elastiwatch/elastiwatch/logs.go b/elastiwatch/elastiwatch/logs.go index 2f7c9816..d911e221 100644 --- a/elastiwatch/elastiwatch/logs.go +++ b/elastiwatch/elastiwatch/logs.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "regexp" + "strings" "time" "github.com/keybase/go-keybase-chat-bot/kbchat/types/chat1" @@ -79,14 +80,15 @@ func (l *LogWatch) alertFromChunk(c chunk) { } func (l *LogWatch) alertEmail(subject string, chunks []chunk) { - body := fmt.Sprintf("Email sent: %s", subject) + var body strings.Builder + body.WriteString(fmt.Sprintf("Email sent: %s", subject)) for _, c := range chunks { if c.Severity == "INFO" { continue } - body += fmt.Sprintf("\n%s %d %s", c.Severity, c.Count, c.Message) + body.WriteString(fmt.Sprintf("\n%s %d %s", c.Severity, c.Count, c.Message)) } - l.ChatEcho(l.emailConvID, "```%s```", body) + l.ChatEcho(l.emailConvID, "```%s```", body.String()) } func (l *LogWatch) filterEntries(entries []*entry) (res []*entry) { diff --git a/gcalbot/gcalbot/account.go b/gcalbot/gcalbot/account.go index c9a8c02a..ca2839a7 100644 --- a/gcalbot/gcalbot/account.go +++ b/gcalbot/gcalbot/account.go @@ -28,7 +28,7 @@ func (h *Handler) handleAccountsList(msg chat1.MsgSummary) error { } accountListMessage := "Here are your connected accounts:" + strings.Repeat("\n• %s", len(accounts)) - accountInterfaces := make([]interface{}, len(accounts)) + accountInterfaces := make([]any, len(accounts)) for index := range accounts { accountInterfaces[index] = accounts[index].AccountNickname } diff --git a/gcalbot/gcalbot/calendar.go b/gcalbot/gcalbot/calendar.go index f261499f..98d7b809 100644 --- a/gcalbot/gcalbot/calendar.go +++ b/gcalbot/gcalbot/calendar.go @@ -41,7 +41,7 @@ func (h *Handler) handleCalendarsList(msg chat1.MsgSummary, args []string) error return nil } - data := []interface{}{accountNickname} + data := []any{accountNickname} for _, calendarItem := range calendarList { if calendarItem.SummaryOverride != "" { data = append(data, calendarItem.SummaryOverride) diff --git a/gcalbot/gcalbot/event.go b/gcalbot/gcalbot/event.go index 3a5f5c0c..b41f51f7 100644 --- a/gcalbot/gcalbot/event.go +++ b/gcalbot/gcalbot/event.go @@ -67,20 +67,20 @@ func FormatEvent( } } - var conferenceData string + var conferenceData strings.Builder if event.ConferenceData != nil { for _, entryPoint := range event.ConferenceData.EntryPoints { uri := strings.TrimPrefix(entryPoint.Uri, "https://") switch entryPoint.EntryPointType { case "video", "more": - conferenceData += fmt.Sprintf("\n> Join online: %s", uri) + conferenceData.WriteString(fmt.Sprintf("\n> Join online: %s", uri)) case "phone": - conferenceData += fmt.Sprintf("\n> Join by phone: %s", entryPoint.Label) + conferenceData.WriteString(fmt.Sprintf("\n> Join by phone: %s", entryPoint.Label)) if entryPoint.Pin != "" { - conferenceData += fmt.Sprintf(" PIN: %s", entryPoint.Pin) + conferenceData.WriteString(fmt.Sprintf(" PIN: %s", entryPoint.Pin)) } case "sip": - conferenceData += fmt.Sprintf("\n> Join by SIP: %s", entryPoint.Label) + conferenceData.WriteString(fmt.Sprintf("\n> Join by SIP: %s", entryPoint.Label)) } } } @@ -101,7 +101,7 @@ func FormatEvent( url := strings.TrimPrefix(event.HtmlLink, "https://") return fmt.Sprintf(message, - what, when, where, conferenceData, organizer, calendarSummary, description, url), nil + what, when, where, conferenceData.String(), organizer, calendarSummary, description, url), nil } func FormatEventSchedule( diff --git a/gcalbot/gcalbot/html.go b/gcalbot/gcalbot/html.go index 2bbbe623..79893032 100644 --- a/gcalbot/gcalbot/html.go +++ b/gcalbot/gcalbot/html.go @@ -524,7 +524,7 @@ var templates = template.Must(template.Must(template.Must(template.Must(template }, }).Parse(tmplConfig)) -func (h *HTTPSrv) servePage(w http.ResponseWriter, name string, data interface{}) { +func (h *HTTPSrv) servePage(w http.ResponseWriter, name string, data any) { var page bytes.Buffer if err := templates.ExecuteTemplate(&page, name, data); err != nil { w.WriteHeader(http.StatusInternalServerError) diff --git a/gcalbot/gcalbot/http.go b/gcalbot/gcalbot/http.go index 3575ca73..32c3c5a8 100644 --- a/gcalbot/gcalbot/http.go +++ b/gcalbot/gcalbot/http.go @@ -261,7 +261,7 @@ func (h *HTTPSrv) configHandler(w http.ResponseWriter, r *http.Request) { } // get list of times in half hour increments baseTime := time.Date(2006, time.January, 2, 0, 0, 0, 0, dsTimezone) - for i := 0; i < 48; i++ { + for i := range 48 { var title string minutes := i * 30 dateTime := baseTime.Add(time.Duration(minutes) * time.Minute) diff --git a/githubbot/githubbot/handler.go b/githubbot/githubbot/handler.go index f2c5176a..289cb031 100644 --- a/githubbot/githubbot/handler.go +++ b/githubbot/githubbot/handler.go @@ -199,9 +199,9 @@ func (h *Handler) handleListSubscriptions(msg chat1.MsgSummary) (err error) { return nil } - var res string + var res strings.Builder for repo, f := range features { - res += fmt.Sprintf("- *%s* (%s)\n", repo, &f) + res.WriteString(fmt.Sprintf("- *%s* (%s)\n", repo, &f)) if f.Commits { branches, err := h.db.GetAllBranchesForRepo(msg.ConvID, repo) if err != nil { @@ -209,11 +209,11 @@ func (h *Handler) handleListSubscriptions(msg chat1.MsgSummary) (err error) { } for _, branch := range branches { - res += fmt.Sprintf(" - %s\n", branch) + res.WriteString(fmt.Sprintf(" - %s\n", branch)) } } } - h.ChatEcho(msg.ConvID, "%s", res) + h.ChatEcho(msg.ConvID, "%s", res.String()) return nil } diff --git a/githubbot/githubbot/http.go b/githubbot/githubbot/http.go index abcb988d..33944163 100644 --- a/githubbot/githubbot/http.go +++ b/githubbot/githubbot/http.go @@ -134,7 +134,7 @@ func (h *HTTPSrv) handleWebhook(_ http.ResponseWriter, r *http.Request) { } } -func (h *HTTPSrv) formatMessage(convID chat1.ConvIDStr, event interface{}, repo string, client *github.Client) (message string, branch string) { +func (h *HTTPSrv) formatMessage(convID chat1.ConvIDStr, event any, repo string, client *github.Client) (message string, branch string) { parsedRepo := strings.Split(repo, "/") if len(parsedRepo) != 2 { h.Debug("invalid repo: %s", repo) diff --git a/githubbot/githubbot/util.go b/githubbot/githubbot/util.go index 78b16571..339305c3 100644 --- a/githubbot/githubbot/util.go +++ b/githubbot/githubbot/util.go @@ -205,7 +205,7 @@ func getPossibleKBUser(kbc *kbchat.API, d *DB, debug *base.DebugOutput, githubUs } // pref checking -func shouldParseEvent(event interface{}, features *Features) bool { +func shouldParseEvent(event any, features *Features) bool { if features == nil { return true } diff --git a/gitlabbot/gitlabbot/handler.go b/gitlabbot/gitlabbot/handler.go index 04650385..216a65a7 100644 --- a/gitlabbot/gitlabbot/handler.go +++ b/gitlabbot/gitlabbot/handler.go @@ -142,10 +142,10 @@ func (h *Handler) handleListSubscriptions(msg chat1.MsgSummary) (err error) { return nil } - var res string + var res strings.Builder for _, repo := range subscriptions { - res += fmt.Sprintf("- *%s*\n", repo) + res.WriteString(fmt.Sprintf("- *%s*\n", repo)) } - h.ChatEcho(msg.ConvID, "%s", res) + h.ChatEcho(msg.ConvID, "%s", res.String()) return nil } diff --git a/macrobot/macrobot/handler.go b/macrobot/macrobot/handler.go index c008837a..399f16c3 100644 --- a/macrobot/macrobot/handler.go +++ b/macrobot/macrobot/handler.go @@ -162,7 +162,7 @@ func (h *Handler) handleList(msg chat1.MsgSummary) error { return nil } - data := []interface{}{getChannelType(msg.Channel, true)} + data := []any{getChannelType(msg.Channel, true)} hasConvs := false for i, macro := range macroList { // If we have a team and conv command defined with the same name we diff --git a/pollbot/pollbot/handler.go b/pollbot/pollbot/handler.go index ef3e3d99..f8446954 100644 --- a/pollbot/pollbot/handler.go +++ b/pollbot/pollbot/handler.go @@ -58,12 +58,12 @@ func (h *Handler) generateAnonymousPoll(convID chat1.ConvIDStr, prompt string, o return fmt.Errorf("failed to get ID of prompt message") } promptMsgID := *sendRes.Result.MessageID - var body string + var body strings.Builder for index, option := range options { - body += fmt.Sprintf("\n%s *%s*\n%s\n", base.NumberToEmoji(index+1), option, - h.generateVoteLink(id, index+1)) + body.WriteString(fmt.Sprintf("\n%s *%s*\n%s\n", base.NumberToEmoji(index+1), option, + h.generateVoteLink(id, index+1))) } - h.ChatEcho(convID, "%s", body) + h.ChatEcho(convID, "%s", body.String()) if sendRes, err = h.kbc.SendMessageByConvID(convID, "*Results*\n_No votes yet_"); err != nil { return fmt.Errorf("failed to send poll: %s", err) } @@ -78,12 +78,13 @@ func (h *Handler) generateAnonymousPoll(convID chat1.ConvIDStr, prompt string, o } func (h *Handler) generatePoll(convID chat1.ConvIDStr, prompt string, options []string) error { - body := fmt.Sprintf("Poll: *%s*\n\n", prompt) + var body strings.Builder + body.WriteString(fmt.Sprintf("Poll: *%s*\n\n", prompt)) for index, option := range options { - body += fmt.Sprintf("%s %s\n", base.NumberToEmoji(index+1), option) + body.WriteString(fmt.Sprintf("%s %s\n", base.NumberToEmoji(index+1), option)) } - body += "Tap a reaction below to register your vote!" - sendRes, err := h.kbc.SendMessageByConvID(convID, "%s", body) + body.WriteString("Tap a reaction below to register your vote!") + sendRes, err := h.kbc.SendMessageByConvID(convID, "%s", body.String()) if err != nil { return fmt.Errorf("failed to send poll: %s", err) } diff --git a/pollbot/pollbot/util.go b/pollbot/pollbot/util.go index 5b7f7b1a..c24ba17d 100644 --- a/pollbot/pollbot/util.go +++ b/pollbot/pollbot/util.go @@ -22,7 +22,7 @@ func formatTally(tally Tally, numChoices int) (res string) { for _, t := range tally { tallyMap[t.choice] = t } - for i := 0; i < numChoices; i++ { + for i := range numChoices { t, ok := tallyMap[i+1] if !ok { t.choice = i + 1 diff --git a/webhookbot/webhookbot/handler.go b/webhookbot/webhookbot/handler.go index 8e6cbb41..153a2fe5 100644 --- a/webhookbot/webhookbot/handler.go +++ b/webhookbot/webhookbot/handler.go @@ -96,11 +96,11 @@ func (h *Handler) handleList(_ string, msg chat1.MsgSummary) (err error) { h.ChatEcho(convID, "No hooks in this conversation") return nil } - var body string + var body strings.Builder for _, hook := range hooks { - body += fmt.Sprintf("%s, %s\n", hook.Name, h.formURL(hook.ID)) + body.WriteString(fmt.Sprintf("%s, %s\n", hook.Name, h.formURL(hook.ID))) } - if _, err := h.kbc.SendMessageByTlfName(msg.Sender.Username, "%s", body); err != nil { + if _, err := h.kbc.SendMessageByTlfName(msg.Sender.Username, "%s", body.String()); err != nil { h.Debug("handleList: failed to send hook: %s", err) } h.ChatEcho(convID, "List sent to @%s", msg.Sender.Username)