fix: lint errors

This commit is contained in:
Felipe M 2025-04-22 11:56:33 +02:00
parent abcd3c3c44
commit 763a451251
Signed by: fmartingr
GPG key ID: CCFBC5637D4000A8
10 changed files with 91 additions and 55 deletions

View file

@ -194,7 +194,7 @@ func (a *Admin) addFlash(w http.ResponseWriter, r *http.Request, message string,
}
// Map internal categories to Bootstrap alert classes
alertClass := category
var alertClass string
switch category {
case "success":
alertClass = "success"
@ -249,16 +249,6 @@ func (a *Admin) getFlashes(w http.ResponseWriter, r *http.Request) []FlashMessag
return messages
}
// requireLogin middleware checks if the user is logged in
func (a *Admin) requireLogin(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if !a.isLoggedIn(r) {
http.Redirect(w, r, "/admin/login", http.StatusSeeOther)
return
}
next(w, r)
}
}
// render renders a template with the given data
func (a *Admin) render(w http.ResponseWriter, r *http.Request, templateName string, data TemplateData) {
@ -334,7 +324,10 @@ func (a *Admin) handleLogin(w http.ResponseWriter, r *http.Request) {
// Set session expiration
session.Options.MaxAge = 3600 * 24 * 7 // 1 week
session.Save(r, w)
err = session.Save(r, w)
if err != nil {
fmt.Printf("Error saving session: %v\n", err)
}
a.addFlash(w, r, "You were logged in", "success")
@ -715,4 +708,4 @@ func (a *Admin) handleChannelPluginDetailOrDelete(w http.ResponseWriter, r *http
// Redirect to channel plugins list
http.Redirect(w, r, "/admin/channelplugins", http.StatusSeeOther)
}
}

View file

@ -152,7 +152,9 @@ func (a *App) initializeRoutes() {
a.router.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]interface{}{})
if err := json.NewEncoder(w).Encode(map[string]interface{}{}); err != nil {
a.logger.Error("Error encoding response", "error", err)
}
})
// Platform webhook endpoints
@ -175,7 +177,9 @@ func (a *App) handleIncomingWebhook(w http.ResponseWriter, r *http.Request) {
if _, err := platform.Get(platformName); err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"error": "Unknown platform"})
if err := json.NewEncoder(w).Encode(map[string]string{"error": "Unknown platform"}); err != nil {
a.logger.Error("Error encoding response", "error", err)
}
return
}
@ -184,7 +188,9 @@ func (a *App) handleIncomingWebhook(w http.ResponseWriter, r *http.Request) {
if err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"error": "Failed to read request body"})
if err := json.NewEncoder(w).Encode(map[string]string{"error": "Failed to read request body"}); err != nil {
a.logger.Error("Error encoding response", "error", err)
}
return
}
@ -200,7 +206,9 @@ func (a *App) handleIncomingWebhook(w http.ResponseWriter, r *http.Request) {
// Respond with success
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]any{})
if err := json.NewEncoder(w).Encode(map[string]any{}); err != nil {
a.logger.Error("Error encoding response", "error", err)
}
}
// extractPlatformName extracts the platform name from the URL path
@ -382,4 +390,4 @@ func (a *App) processReminder(reminder *model.Reminder) {
if err := a.db.MarkReminderAsProcessed(reminder.ID); err != nil {
a.logger.Error("Error marking reminder as processed", "error", err)
}
}
}

View file

@ -234,7 +234,11 @@ func (d *Database) GetChannelPlugins(channelID int64) ([]*model.ChannelPlugin, e
if err != nil {
return nil, err
}
defer rows.Close()
defer func() {
if err := rows.Close(); err != nil {
fmt.Printf("Error closing rows: %v\n", err)
}
}()
var plugins []*model.ChannelPlugin
@ -415,7 +419,11 @@ func (d *Database) GetAllChannels() ([]*model.Channel, error) {
if err != nil {
return nil, err
}
defer rows.Close()
defer func() {
if err := rows.Close(); err != nil {
fmt.Printf("Error closing rows: %v\n", err)
}
}()
var channels []*model.Channel
@ -454,10 +462,9 @@ func (d *Database) GetAllChannels() ([]*model.Channel, error) {
continue // Skip this channel if plugins can't be retrieved
}
if plugins != nil {
for _, plugin := range plugins {
channel.Plugins[plugin.PluginID] = plugin
}
// Add plugins to channel
for _, plugin := range plugins {
channel.Plugins[plugin.PluginID] = plugin
}
channels = append(channels, channel)
@ -646,7 +653,11 @@ func (d *Database) GetPendingReminders() ([]*model.Reminder, error) {
if err != nil {
return nil, err
}
defer rows.Close()
defer func() {
if err := rows.Close(); err != nil {
fmt.Printf("Error closing rows: %v\n", err)
}
}()
var reminders []*model.Reminder
@ -763,4 +774,4 @@ func initDatabase(db *sql.DB) error {
}
return nil
}
}

View file

@ -49,7 +49,11 @@ func GetAppliedMigrations(db *sql.DB) ([]int, error) {
if err != nil {
return nil, err
}
defer rows.Close()
defer func() {
if err := rows.Close(); err != nil {
fmt.Printf("Error closing rows: %v\n", err)
}
}()
var versions []int
for rows.Next() {
@ -128,7 +132,9 @@ func Migrate(db *sql.DB) error {
// Apply the migration
if err := migration.Up(db); err != nil {
tx.Rollback()
if err := tx.Rollback(); err != nil {
fmt.Printf("Error rolling back transaction: %v\n", err)
}
return fmt.Errorf("failed to apply migration %d: %w", version, err)
}
@ -137,7 +143,9 @@ func Migrate(db *sql.DB) error {
"INSERT INTO schema_migrations (version, applied_at) VALUES (?, ?)",
version, time.Now(),
); err != nil {
tx.Rollback()
if err := tx.Rollback(); err != nil {
fmt.Printf("Error rolling back transaction: %v\n", err)
}
return fmt.Errorf("failed to mark migration %d as applied: %w", version, err)
}
@ -188,13 +196,17 @@ func MigrateDown(db *sql.DB, targetVersion int) error {
// Apply the down migration
if err := migration.Down(db); err != nil {
tx.Rollback()
if err := tx.Rollback(); err != nil {
fmt.Printf("Error rolling back transaction: %v\n", err)
}
return fmt.Errorf("failed to roll back migration %d: %w", version, err)
}
// Remove from applied list
if _, err := tx.Exec("DELETE FROM schema_migrations WHERE version = ?", version); err != nil {
tx.Rollback()
if err := tx.Rollback(); err != nil {
fmt.Printf("Error rolling back transaction: %v\n", err)
}
return fmt.Errorf("failed to remove migration %d from applied list: %w", version, err)
}
@ -208,4 +220,4 @@ func MigrateDown(db *sql.DB, targetVersion int) error {
}
return nil
}
}

View file

@ -4,7 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"strings"
"time"
@ -37,11 +37,15 @@ func (s *SlackPlatform) Init(_ *config.Config) error {
// ParseIncomingMessage parses an incoming Slack message
func (s *SlackPlatform) ParseIncomingMessage(r *http.Request) (*model.Message, error) {
// Read request body
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
return nil, err
}
defer r.Body.Close()
defer func() {
if err := r.Body.Close(); err != nil {
fmt.Printf("Error closing request body: %v\n", err)
}
}()
// Parse JSON
var requestData map[string]interface{}
@ -194,7 +198,11 @@ func (s *SlackPlatform) SendMessage(msg *model.Message) error {
if err != nil {
return err
}
defer resp.Body.Close()
defer func() {
if err := resp.Body.Close(); err != nil {
fmt.Printf("Error closing response body: %v\n", err)
}
}()
// Check response
if resp.StatusCode != http.StatusOK {
@ -209,4 +217,4 @@ func parseInt64(s string) (int64, error) {
var n int64
_, err := fmt.Sscanf(s, "%d", &n)
return n, err
}
}

View file

@ -62,7 +62,11 @@ func (t *TelegramPlatform) Init(cfg *config.Config) error {
t.log.Error("Failed to set webhook", "error", err)
return fmt.Errorf("failed to set webhook: %w", err)
}
defer resp.Body.Close()
defer func() {
if err := resp.Body.Close(); err != nil {
t.log.Error("Error closing response body", "error", err)
}
}()
if resp.StatusCode != http.StatusOK {
bodyBytes, _ := io.ReadAll(resp.Body)
@ -85,7 +89,11 @@ func (t *TelegramPlatform) ParseIncomingMessage(r *http.Request) (*model.Message
t.log.Error("Failed to read request body", "error", err)
return nil, err
}
defer r.Body.Close()
defer func() {
if err := r.Body.Close(); err != nil {
t.log.Error("Error closing request body", "error", err)
}
}()
// Parse JSON
var update struct {
@ -251,7 +259,11 @@ func (t *TelegramPlatform) SendMessage(msg *model.Message) error {
t.log.Error("Failed to send message", "error", err)
return err
}
defer resp.Body.Close()
defer func() {
if err := resp.Body.Close(); err != nil {
t.log.Error("Error closing response body", "error", err)
}
}()
// Check response
if resp.StatusCode != http.StatusOK {
@ -263,4 +275,4 @@ func (t *TelegramPlatform) SendMessage(msg *model.Message) error {
t.log.Debug("Message sent successfully")
return nil
}
}

View file

@ -107,9 +107,10 @@ func (p *DicePlugin) rollDice(formula string) (int, error) {
return 0, fmt.Errorf("invalid modifier")
}
if matches[3] == "+" {
switch matches[3] {
case "+":
total += modifier
} else if matches[3] == "-" {
case "-":
total -= modifier
}
}

View file

@ -44,14 +44,7 @@ func New(creator ReminderCreator) *Reminder {
func (r *Reminder) OnMessage(msg *model.Message, config map[string]interface{}) []*model.Message {
// Only process replies to messages
if msg.ReplyTo == "" {
return []*model.Message{
{
Text: "Please reply to a message with `!remindme <duration>` to set a reminder.",
Chat: msg.Chat,
Channel: msg.Channel,
ReplyTo: msg.ID,
},
}
return nil
}
// Check if the message is a reminder command

View file

@ -161,4 +161,4 @@ func TestReminderOnMessage(t *testing.T) {
}
})
}
}
}

View file

@ -53,9 +53,7 @@ func (p *InstagramExpander) OnMessage(msg *model.Message, config map[string]inte
}
// Change the host
if strings.Contains(parsedURL.Host, "instagram.com") {
parsedURL.Host = strings.Replace(parsedURL.Host, "instagram.com", "ddinstagram.com", 1)
}
parsedURL.Host = strings.Replace(parsedURL.Host, "instagram.com", "ddinstagram.com", 1)
// Remove query parameters
parsedURL.RawQuery = ""