mirror of https://git.sr.ht/~ashkeel/strimertul
refactor: minor lint fixes
This commit is contained in:
parent
fbb943f307
commit
82b7d51df7
|
@ -0,0 +1,11 @@
|
|||
*.exe
|
||||
*.db
|
||||
*.db.lock
|
||||
/data/
|
||||
/backups/
|
||||
.vscode
|
||||
.idea
|
||||
strimertul_*
|
||||
/build/bin/
|
||||
*.log
|
||||
api.json
|
|
@ -0,0 +1,12 @@
|
|||
linters:
|
||||
enable:
|
||||
- errcheck
|
||||
- gosimple
|
||||
- govet
|
||||
- ineffassign
|
||||
- staticcheck
|
||||
- unused
|
||||
- revive
|
||||
- errorlint
|
||||
- errname
|
||||
- contextcheck
|
3
app.go
3
app.go
|
@ -34,7 +34,7 @@ import (
|
|||
type App struct {
|
||||
ctx context.Context
|
||||
cliParams *cli.Context
|
||||
driver database.DatabaseDriver
|
||||
driver database.Driver
|
||||
ready *sync.RWSync[bool]
|
||||
isFatalError *sync.RWSync[bool]
|
||||
backupOptions database.BackupOptions
|
||||
|
@ -269,6 +269,7 @@ func (a *App) SendCrashReport(errorData string, info string) (string, error) {
|
|||
logger.Error("Could not send crash report", zap.Error(err))
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Check the response
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
|
|
|
@ -13,7 +13,7 @@ import (
|
|||
"git.sr.ht/~ashkeel/strimertul/utils"
|
||||
)
|
||||
|
||||
func BackupTask(driver database.DatabaseDriver, options database.BackupOptions) {
|
||||
func BackupTask(driver database.Driver, options database.BackupOptions) {
|
||||
if options.BackupDir == "" {
|
||||
logger.Warn("Backup directory not set, database backups are disabled")
|
||||
return
|
||||
|
@ -34,7 +34,7 @@ func BackupTask(driver database.DatabaseDriver, options database.BackupOptions)
|
|||
}
|
||||
}
|
||||
|
||||
func performBackup(driver database.DatabaseDriver, options database.BackupOptions) {
|
||||
func performBackup(driver database.Driver, options database.BackupOptions) {
|
||||
// Run backup procedure
|
||||
file, err := os.Create(fmt.Sprintf("%s/%s.db", options.BackupDir, time.Now().Format("20060102-150405")))
|
||||
if err != nil {
|
||||
|
|
|
@ -13,8 +13,8 @@ import (
|
|||
"git.sr.ht/~ashkeel/strimertul/utils"
|
||||
)
|
||||
|
||||
// DatabaseDriver is a driver wrapping a supported database
|
||||
type DatabaseDriver interface {
|
||||
// Driver is a driver wrapping a supported database
|
||||
type Driver interface {
|
||||
Hub() *kv.Hub
|
||||
Close() error
|
||||
Import(map[string]string) error
|
||||
|
@ -46,7 +46,7 @@ func getDatabaseDriverName(ctx *cli.Context) string {
|
|||
return string(file)
|
||||
}
|
||||
|
||||
func GetDatabaseDriver(ctx *cli.Context) (DatabaseDriver, error) {
|
||||
func GetDatabaseDriver(ctx *cli.Context) (Driver, error) {
|
||||
name := getDatabaseDriverName(ctx)
|
||||
dbDirectory := ctx.String("database-dir")
|
||||
logger := ctx.Context.Value(utils.ContextLogger).(*zap.Logger)
|
||||
|
|
|
@ -32,7 +32,7 @@ func NewPrediction(teams []string, bettingTime time.Duration) *Prediction {
|
|||
}
|
||||
}
|
||||
|
||||
func (p *Prediction) AddBet(who string, teamId uint, amount uint64) error {
|
||||
func (p *Prediction) AddBet(who string, teamID uint, amount uint64) error {
|
||||
_, ok := p.Bets[who]
|
||||
if ok {
|
||||
return ErrPAlreadyBet
|
||||
|
@ -40,7 +40,7 @@ func (p *Prediction) AddBet(who string, teamId uint, amount uint64) error {
|
|||
|
||||
p.Bets[who] = PredictionBet{
|
||||
Amount: amount,
|
||||
Team: teamId,
|
||||
Team: teamID,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package loyalty
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
@ -233,14 +234,13 @@ func (m *Manager) cmdRedeemReward(bot *twitch.Bot, message irc.PrivateMessage) {
|
|||
Reward: reward,
|
||||
RequestText: text,
|
||||
}); err != nil {
|
||||
switch err {
|
||||
case ErrRedeemInCooldown:
|
||||
if errors.Is(err, ErrRedeemInCooldown) {
|
||||
nextAvailable := m.GetRewardCooldown(reward.ID)
|
||||
bot.Client.Say(message.Channel, fmt.Sprintf("%s: That reward is in cooldown (available in %s)", message.User.DisplayName,
|
||||
time.Until(nextAvailable).Truncate(time.Second)))
|
||||
default:
|
||||
m.logger.Error("Error while performing redeem", zap.Error(err))
|
||||
return
|
||||
}
|
||||
m.logger.Error("Error while performing redeem", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
|
|
2
main.go
2
main.go
|
@ -105,7 +105,7 @@ func main() {
|
|||
ctx.Context = context.WithValue(ctx.Context, utils.ContextLogger, logger)
|
||||
return nil
|
||||
},
|
||||
After: func(ctx *cli.Context) error {
|
||||
After: func(_ *cli.Context) error {
|
||||
if panicLog != nil {
|
||||
utils.Close(panicLog, logger)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
@echo off
|
||||
CD /D %~dp0
|
||||
C:\projects\strimertul\strimertul\build\bin\strimertul.exe
|
|
@ -55,7 +55,7 @@ func SetupTimers(bot *Bot) *BotTimerModule {
|
|||
|
||||
// Fill messages with zero values
|
||||
// (This can probably be done faster)
|
||||
for i := 0; i < AverageMessageWindow; i += 1 {
|
||||
for i := 0; i < AverageMessageWindow; i++ {
|
||||
mod.messages.Push(0)
|
||||
}
|
||||
|
||||
|
|
|
@ -76,14 +76,14 @@ func (c *Client) connectWebsocket(url string, oldConnection *websocket.Conn, use
|
|||
continue
|
||||
}
|
||||
|
||||
reconnectURL, err, done := c.processMessage(wsMessage, oldConnection, userClient)
|
||||
reconnectURL, done, err := c.processMessage(wsMessage, oldConnection, userClient)
|
||||
if done {
|
||||
return reconnectURL, connection, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) processMessage(wsMessage EventSubWebsocketMessage, oldConnection *websocket.Conn, userClient *helix.Client) (string, error, bool) {
|
||||
func (c *Client) processMessage(wsMessage EventSubWebsocketMessage, oldConnection *websocket.Conn, userClient *helix.Client) (string, bool, error) {
|
||||
switch wsMessage.Metadata.MessageType {
|
||||
case "session_keepalive":
|
||||
// Nothing to do
|
||||
|
@ -94,7 +94,7 @@ func (c *Client) processMessage(wsMessage EventSubWebsocketMessage, oldConnectio
|
|||
c.logger.Error("Error decoding EventSub welcome message", zap.String("message-type", wsMessage.Metadata.MessageType), zap.Error(err))
|
||||
break
|
||||
}
|
||||
c.logger.Info("Connection to EventSub websocket established", zap.String("session-id", welcomeData.Session.Id))
|
||||
c.logger.Info("Connection to EventSub websocket established", zap.String("session-id", welcomeData.Session.ID))
|
||||
|
||||
// We can only close the old connection once the new one has been established
|
||||
if oldConnection != nil {
|
||||
|
@ -102,7 +102,7 @@ func (c *Client) processMessage(wsMessage EventSubWebsocketMessage, oldConnectio
|
|||
}
|
||||
|
||||
// Add subscription to websocket session
|
||||
err = c.addSubscriptionsForSession(userClient, welcomeData.Session.Id)
|
||||
err = c.addSubscriptionsForSession(userClient, welcomeData.Session.ID)
|
||||
if err != nil {
|
||||
c.logger.Error("Could not add subscriptions", zap.Error(err))
|
||||
break
|
||||
|
@ -114,26 +114,26 @@ func (c *Client) processMessage(wsMessage EventSubWebsocketMessage, oldConnectio
|
|||
c.logger.Error("Error decoding EventSub session reconnect parameters", zap.String("message-type", wsMessage.Metadata.MessageType), zap.Error(err))
|
||||
break
|
||||
}
|
||||
c.logger.Info("EventSub websocket requested a reconnection", zap.String("session-id", reconnectData.Session.Id), zap.String("reconnect-url", reconnectData.Session.ReconnectUrl))
|
||||
c.logger.Info("EventSub websocket requested a reconnection", zap.String("session-id", reconnectData.Session.ID), zap.String("reconnect-url", reconnectData.Session.ReconnectURL))
|
||||
|
||||
return reconnectData.Session.ReconnectUrl, nil, true
|
||||
return reconnectData.Session.ReconnectURL, true, nil
|
||||
case "notification":
|
||||
go c.processEvent(wsMessage)
|
||||
case "revocation":
|
||||
// TODO idk what to do here
|
||||
}
|
||||
return "", nil, false
|
||||
return "", false, nil
|
||||
}
|
||||
|
||||
func (c *Client) processEvent(message EventSubWebsocketMessage) {
|
||||
// Check if we processed this already
|
||||
if message.Metadata.MessageId != "" {
|
||||
if c.eventCache.Contains(message.Metadata.MessageId) {
|
||||
c.logger.Debug("Received duplicate event, ignoring", zap.String("message-id", message.Metadata.MessageId))
|
||||
if message.Metadata.MessageID != "" {
|
||||
if c.eventCache.Contains(message.Metadata.MessageID) {
|
||||
c.logger.Debug("Received duplicate event, ignoring", zap.String("message-id", message.Metadata.MessageID))
|
||||
return
|
||||
}
|
||||
}
|
||||
defer c.eventCache.Add(message.Metadata.MessageId, message.Metadata.MessageTimestamp)
|
||||
defer c.eventCache.Add(message.Metadata.MessageID, message.Metadata.MessageTimestamp)
|
||||
|
||||
// Decode data
|
||||
var notificationData NotificationMessagePayload
|
||||
|
@ -218,11 +218,11 @@ type EventSubWebsocketMessage struct {
|
|||
|
||||
type WelcomeMessagePayload struct {
|
||||
Session struct {
|
||||
Id string `json:"id"`
|
||||
ID string `json:"id"`
|
||||
Status string `json:"status"`
|
||||
ConnectedAt time.Time `json:"connected_at"`
|
||||
KeepaliveTimeoutSeconds int `json:"keepalive_timeout_seconds"`
|
||||
ReconnectUrl string `json:"reconnect_url,omitempty"`
|
||||
ReconnectURL string `json:"reconnect_url,omitempty"`
|
||||
} `json:"session"`
|
||||
}
|
||||
|
||||
|
@ -233,7 +233,7 @@ type NotificationMessagePayload struct {
|
|||
}
|
||||
|
||||
type EventSubMetadata struct {
|
||||
MessageId string `json:"message_id"`
|
||||
MessageID string `json:"message_id"`
|
||||
MessageType string `json:"message_type"`
|
||||
MessageTimestamp time.Time `json:"message_timestamp"`
|
||||
SubscriptionType string `json:"subscription_type"`
|
||||
|
|
|
@ -244,9 +244,8 @@ func (c *Client) runStatusPoll() {
|
|||
if err != nil {
|
||||
c.logger.Error("Error checking stream status", zap.Error(err))
|
||||
return
|
||||
} else {
|
||||
c.streamOnline.Set(len(status.Data.Streams) > 0)
|
||||
}
|
||||
c.streamOnline.Set(len(status.Data.Streams) > 0)
|
||||
|
||||
err = c.db.PutJSON(StreamInfoKey, status.Data.Streams)
|
||||
if err != nil {
|
||||
|
|
|
@ -140,7 +140,7 @@ func (s *WebServer) makeMux() *http.ServeMux {
|
|||
|
||||
func healthFunc(w http.ResponseWriter, _ *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprint(w, "OK")
|
||||
_, _ = fmt.Fprint(w, "OK")
|
||||
}
|
||||
|
||||
func (s *WebServer) RegisterRoute(route string, handler http.Handler) {
|
||||
|
|
|
@ -67,6 +67,8 @@ func TestListen(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
t.Fatalf("Expected 200, got %d", resp.StatusCode)
|
||||
}
|
||||
|
@ -117,6 +119,8 @@ func TestCustomRoute(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
t.Fatalf("Expected 200, got %d", resp.StatusCode)
|
||||
}
|
||||
|
@ -137,6 +141,8 @@ func TestCustomRoute(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != 404 {
|
||||
t.Fatalf("Expected 404, got %d", resp.StatusCode)
|
||||
}
|
||||
|
|
|
@ -40,7 +40,7 @@ func (t *TestServer) Shutdown(_ context.Context) error {
|
|||
}
|
||||
|
||||
func (t *TestServer) Factory() ServerFactory {
|
||||
return func(h http.Handler, addr string) (Server, error) {
|
||||
return func(h http.Handler, _ string) (Server, error) {
|
||||
s := httptest.NewUnstartedServer(h)
|
||||
t.server.Set(s)
|
||||
return t, nil
|
||||
|
|
Loading…
Reference in New Issue