refactor: minor lint fixes

This commit is contained in:
Ash Keel 2024-02-25 14:46:59 +01:00
parent e34974aaa3
commit fbb943f307
No known key found for this signature in database
GPG Key ID: 53A9E9A6035DD109
11 changed files with 48 additions and 30 deletions

4
app.go
View File

@ -100,7 +100,7 @@ func (a *App) startup(ctx context.Context) {
logger.Info("Strimertul is ready")
// Add logs I/O to UI
_, a.cancelLogs = a.listenForLogs()
a.cancelLogs, _ = a.listenForLogs()
go a.forwardLogs()
// Run HTTP server
@ -160,7 +160,7 @@ func (a *App) initializeComponents() error {
return nil
}
func (a *App) listenForLogs() (error, database.CancelFunc) {
func (a *App) listenForLogs() (database.CancelFunc, error) {
return a.db.SubscribeKey(docs.LogRPCKey, func(newValue string) {
var entry docs.ExternalLog
if err := json.Unmarshal([]byte(newValue), &entry); err != nil {

View File

@ -74,26 +74,26 @@ func (mod *LocalDBClient) PutKey(key string, data string) error {
return err
}
func (mod *LocalDBClient) SubscribePrefix(fn kv.SubscriptionCallback, prefixes ...string) (err error, cancelFn CancelFunc) {
func (mod *LocalDBClient) SubscribePrefix(fn kv.SubscriptionCallback, prefixes ...string) (cancelFn CancelFunc, err error) {
var ids []int64
for _, prefix := range prefixes {
_, err = mod.makeRequest(kv.CmdSubscribePrefix, map[string]any{"prefix": prefix})
if err != nil {
return err, nil
return nil, err
}
ids = append(ids, mod.client.SetPrefixSubCallback(prefix, fn))
}
return nil, func() {
return func() {
for _, id := range ids {
mod.client.UnsetCallback(id)
}
}
}, nil
}
func (mod *LocalDBClient) SubscribeKey(key string, fn func(string)) (err error, cancelFn CancelFunc) {
func (mod *LocalDBClient) SubscribeKey(key string, fn func(string)) (cancelFn CancelFunc, err error) {
_, err = mod.makeRequest(kv.CmdSubscribeKey, map[string]any{"key": key})
if err != nil {
return err, nil
return nil, err
}
id := mod.client.SetKeySubCallback(key, func(changedKey string, value string) {
if key != changedKey {
@ -101,9 +101,9 @@ func (mod *LocalDBClient) SubscribeKey(key string, fn func(string)) (err error,
}
fn(value)
})
return nil, func() {
return func() {
mod.client.UnsetCallback(id)
}
}, nil
}
func (mod *LocalDBClient) GetJSON(key string, dst any) error {

View File

@ -1,6 +1,7 @@
package database
import (
"errors"
"testing"
"time"
@ -251,7 +252,7 @@ func TestLocalDBClientRemoveKey(t *testing.T) {
_, err = store.Get(key)
if err == nil {
t.Fatal("expected key to be removed")
} else if err != kv.ErrorKeyNotFound {
} else if !errors.Is(err, kv.ErrorKeyNotFound) {
t.Fatalf("expected key to be removed, got %s", err)
}
}
@ -263,7 +264,7 @@ func TestLocalDBClientSubscribeKey(t *testing.T) {
// Subscribe to a key using the local client
key := "test"
ch := make(chan string, 1)
err, cancel := client.SubscribeKey(key, func(newValue string) {
cancel, err := client.SubscribeKey(key, func(newValue string) {
ch <- newValue
})
if err != nil {
@ -294,7 +295,7 @@ func TestLocalDBClientSubscribePrefix(t *testing.T) {
// Subscribe to a prefix using the local client
prefix := "test"
ch := make(chan string, 1)
err, cancel := client.SubscribePrefix(func(newKey, newValue string) {
cancel, err := client.SubscribePrefix(func(_, newValue string) {
ch <- newValue
}, prefix)
if err != nil {

View File

@ -120,7 +120,7 @@ func NewManager(db *database.LocalDBClient, twitchManager *twitch.Manager, logge
}
// SubscribePrefix for changes
err, loyalty.cancelSub = db.SubscribePrefix(loyalty.update, "loyalty/")
loyalty.cancelSub, err = db.SubscribePrefix(loyalty.update, "loyalty/")
if err != nil {
logger.Error("Could not setup loyalty reload subscription", zap.Error(err))
}

View File

@ -120,7 +120,7 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
mod.compileTemplates()
err, mod.cancelAlertSub = bot.api.db.SubscribeKey(BotAlertsKey, func(value string) {
mod.cancelAlertSub, err = bot.api.db.SubscribeKey(BotAlertsKey, func(value string) {
err := json.UnmarshalFromString(value, &mod.Config)
if err != nil {
bot.logger.Warn("Error loading alert config", zap.Error(err))
@ -133,7 +133,7 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
bot.logger.Error("Could not set-up bot alert reload subscription", zap.Error(err))
}
err, mod.cancelTwitchEventSub = bot.api.db.SubscribeKey(EventSubEventKey, mod.onEventSubEvent)
mod.cancelTwitchEventSub, err = bot.api.db.SubscribeKey(EventSubEventKey, mod.onEventSubEvent)
if err != nil {
bot.logger.Error("Could not setup twitch alert subscription", zap.Error(err))
}

View File

@ -128,15 +128,15 @@ func newBotWithClient(client IRCBot, api *Client, config BotConfig) *Bot {
if err != nil {
bot.logger.Error("Failed to parse custom commands", zap.Error(err))
}
err, bot.cancelUpdateSub = api.db.SubscribeKey(CustomCommandsKey, bot.updateCommands)
bot.cancelUpdateSub, err = api.db.SubscribeKey(CustomCommandsKey, bot.updateCommands)
if err != nil {
bot.logger.Error("Could not set-up bot command reload subscription", zap.Error(err))
}
err, bot.cancelWritePlainRPCSub = api.db.SubscribeKey(WritePlainMessageRPC, bot.handleWritePlainMessageRPC)
bot.cancelWritePlainRPCSub, err = api.db.SubscribeKey(WritePlainMessageRPC, bot.handleWritePlainMessageRPC)
if err != nil {
bot.logger.Error("Could not set-up bot command reload subscription", zap.Error(err))
}
err, bot.cancelWriteRPCSub = api.db.SubscribeKey(WriteMessageRPC, bot.handleWriteMessageRPC)
bot.cancelWriteRPCSub, err = api.db.SubscribeKey(WriteMessageRPC, bot.handleWriteMessageRPC)
if err != nil {
bot.logger.Error("Could not set-up bot command reload subscription", zap.Error(err))
}
@ -278,8 +278,7 @@ func (b *Bot) handleWritePlainMessageRPC(value string) {
func (b *Bot) handleWriteMessageRPC(value string) {
var request WriteMessageRequest
err := json.Unmarshal([]byte(value), &request)
if err != nil {
if err := json.Unmarshal([]byte(value), &request); err != nil {
b.logger.Warn("Failed to decode write message request", zap.Error(err))
return
}
@ -289,6 +288,10 @@ func (b *Bot) handleWriteMessageRPC(value string) {
}
if request.WhisperTo != nil && *request.WhisperTo != "" {
client, err := b.api.GetUserClient(false)
if err != nil {
b.logger.Error("Failed to retrieve client", zap.Error(err))
return
}
reply, err := client.SendUserWhisper(&helix.SendUserWhisperParams{
FromUserID: b.api.User.ID,
ToUserID: *request.WhisperTo,
@ -304,6 +307,10 @@ func (b *Bot) handleWriteMessageRPC(value string) {
}
if request.Announce {
client, err := b.api.GetUserClient(false)
if err != nil {
b.logger.Error("Failed to retrieve client", zap.Error(err))
return
}
reply, err := client.SendChatAnnouncement(&helix.SendChatAnnouncementParams{
BroadcasterID: b.api.User.ID,
ModeratorID: b.api.User.ID,

View File

@ -73,7 +73,7 @@ func SetupTimers(bot *Bot) *BotTimerModule {
}
}
err, mod.cancelTimerSub = bot.api.db.SubscribeKey(BotTimersKey, func(value string) {
mod.cancelTimerSub, err = bot.api.db.SubscribeKey(BotTimersKey, func(value string) {
err := json.UnmarshalFromString(value, &mod.Config)
if err != nil {
bot.logger.Debug("Error reloading timer config", zap.Error(err))

View File

@ -58,7 +58,7 @@ func NewManager(db *database.LocalDBClient, server *webserver.WebServer, logger
}
// Listen for client config changes
err, cancelConfigSub := db.SubscribeKey(ConfigKey, func(value string) {
cancelConfigSub, err := db.SubscribeKey(ConfigKey, func(value string) {
var newConfig Config
if err := json.UnmarshalFromString(value, &newConfig); err != nil {
logger.Error("Failed to decode Twitch integration config", zap.Error(err))
@ -88,7 +88,7 @@ func NewManager(db *database.LocalDBClient, server *webserver.WebServer, logger
}
// Listen for bot config changes
err, cancelBotSub := db.SubscribeKey(BotConfigKey, func(value string) {
cancelBotSub, err := db.SubscribeKey(BotConfigKey, func(value string) {
newBotConfig := defaultBotConfig()
if err := json.UnmarshalFromString(value, &newBotConfig); err != nil {
logger.Error("Failed to decode bot config", zap.Error(err))

View File

@ -2,13 +2,13 @@ package twitch
import (
"bytes"
"github.com/Masterminds/sprig/v3"
"math/rand"
"strconv"
"strings"
"text/template"
"time"
"github.com/Masterminds/sprig/v3"
irc "github.com/gempir/go-twitch-irc/v4"
"github.com/nicklaw5/helix/v2"
"go.uber.org/zap"
@ -68,6 +68,10 @@ func cmdCustom(bot *Bot, cmd string, data BotCustomCommand, message irc.PrivateM
bot.Client.Reply(message.Channel, message.ID, buf.String())
case ResponseTypeWhisper:
client, err := bot.api.GetUserClient(false)
if err != nil {
bot.logger.Error("Failed to retrieve client", zap.Error(err))
return
}
reply, err := client.SendUserWhisper(&helix.SendUserWhisperParams{
FromUserID: bot.api.User.ID,
ToUserID: message.User.ID,
@ -82,6 +86,10 @@ func cmdCustom(bot *Bot, cmd string, data BotCustomCommand, message irc.PrivateM
case ResponseTypeAnnounce:
client, err := bot.api.GetUserClient(false)
if err != nil {
bot.logger.Error("Failed to retrieve client", zap.Error(err))
return
}
reply, err := client.SendChatAnnouncement(&helix.SendChatAnnouncementParams{
BroadcasterID: bot.api.User.ID,
ModeratorID: bot.api.User.ID,
@ -170,7 +178,7 @@ func (b *Bot) setupFunctions() {
if byt, err := b.api.db.GetKey(counterKey); err == nil {
counter, _ = strconv.Atoi(byt)
}
counter += 1
counter++
err := b.api.db.PutKey(counterKey, strconv.Itoa(counter))
if err != nil {
b.logger.Error("Error saving key", zap.Error(err), zap.String("key", counterKey))

View File

@ -14,7 +14,7 @@ var (
)
func setStdHandle(stdhandle int32, handle syscall.Handle) error {
r0, _, e1 := syscall.Syscall(procSetStdHandle.Addr(), 2, uintptr(stdhandle), uintptr(handle), 0)
r0, _, e1 := syscall.SyscallN(procSetStdHandle.Addr(), 2, uintptr(stdhandle), uintptr(handle), 0)
if r0 == 0 {
if e1 != 0 {
return error(e1)

View File

@ -10,6 +10,7 @@ import (
mrand "math/rand"
"net/http"
"net/http/pprof"
"time"
"git.sr.ht/~ashkeel/containers/sync"
jsoniter "github.com/json-iterator/go"
@ -49,7 +50,7 @@ func NewServer(db *database.LocalDBClient, logger *zap.Logger, serverFactory Ser
var config ServerConfig
err := db.GetJSON(ServerConfigKey, &config)
if err != nil {
if err != database.ErrEmptyKey {
if !errors.Is(err, database.ErrEmptyKey) {
logger.Warn("HTTP config is corrupted or could not be read", zap.Error(err))
}
// Initialize with default config
@ -75,7 +76,7 @@ func NewServer(db *database.LocalDBClient, logger *zap.Logger, serverFactory Ser
Password: server.Config.Get().KVPassword,
})
err, server.cancelConfigSub = db.SubscribeKey(ServerConfigKey, server.onConfigUpdate)
server.cancelConfigSub, err = db.SubscribeKey(ServerConfigKey, server.onConfigUpdate)
if err != nil {
return nil, fmt.Errorf("error while handling subscription to HTTP config changes: %w", err)
}
@ -239,7 +240,8 @@ func generatePassword() string {
_, err := crand.Read(b)
if err != nil {
// fallback to bad rand, but this will never fail
mrand.Read(b)
source := mrand.NewSource(time.Now().Unix())
return fmt.Sprintf("IS-%x%x", source.Int63(), source.Int63())
}
return base64.URLEncoding.EncodeToString(b)
}