chore: add golangci-lint config and fix all lint issues
Add .golangci.yml enabling errcheck, staticcheck, govet, gosec, ineffassign, and unused linters. Fix 66 issues across 16 files: - Check all unchecked errors (errcheck) - Use HTTP status constants instead of numeric literals (staticcheck) - Replace deprecated LineUp/LineDown with ScrollUp/ScrollDown (staticcheck) - Convert sprintf+write patterns to fmt.Fprintf (staticcheck) - Add ReadHeaderTimeout to http.Server (gosec) - Remove unused types and functions (unused) - Add nolint comments for intentional patterns (InsecureSkipVerify, math/rand for jitter, dialect-only SQL formatting)
This commit is contained in:
@@ -2,6 +2,7 @@ package store
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"log"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
@@ -99,15 +100,31 @@ func (d *PostgresDialect) UpsertNodeSQL() string {
|
||||
func (d *PostgresDialect) ResetSequenceOnEmpty(db *sql.DB, table string) {}
|
||||
|
||||
func (d *PostgresDialect) ImportWipe(tx *sql.Tx) {
|
||||
tx.Exec("TRUNCATE TABLE sites RESTART IDENTITY CASCADE")
|
||||
tx.Exec("TRUNCATE TABLE alerts RESTART IDENTITY CASCADE")
|
||||
tx.Exec("TRUNCATE TABLE users RESTART IDENTITY CASCADE")
|
||||
tx.Exec("TRUNCATE TABLE maintenance_windows RESTART IDENTITY CASCADE")
|
||||
if _, err := tx.Exec("TRUNCATE TABLE sites RESTART IDENTITY CASCADE"); err != nil {
|
||||
log.Printf("import wipe error: %v", err)
|
||||
}
|
||||
if _, err := tx.Exec("TRUNCATE TABLE alerts RESTART IDENTITY CASCADE"); err != nil {
|
||||
log.Printf("import wipe error: %v", err)
|
||||
}
|
||||
if _, err := tx.Exec("TRUNCATE TABLE users RESTART IDENTITY CASCADE"); err != nil {
|
||||
log.Printf("import wipe error: %v", err)
|
||||
}
|
||||
if _, err := tx.Exec("TRUNCATE TABLE maintenance_windows RESTART IDENTITY CASCADE"); err != nil {
|
||||
log.Printf("import wipe error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *PostgresDialect) ImportResetSequences(tx *sql.Tx) {
|
||||
tx.Exec("SELECT setval('sites_id_seq', (SELECT COALESCE(MAX(id), 1) FROM sites))")
|
||||
tx.Exec("SELECT setval('alerts_id_seq', (SELECT COALESCE(MAX(id), 1) FROM alerts))")
|
||||
tx.Exec("SELECT setval('users_id_seq', (SELECT COALESCE(MAX(id), 1) FROM users))")
|
||||
tx.Exec("SELECT setval('maintenance_windows_id_seq', (SELECT COALESCE(MAX(id), 1) FROM maintenance_windows))")
|
||||
if _, err := tx.Exec("SELECT setval('sites_id_seq', (SELECT COALESCE(MAX(id), 1) FROM sites))"); err != nil {
|
||||
log.Printf("sequence reset error: %v", err)
|
||||
}
|
||||
if _, err := tx.Exec("SELECT setval('alerts_id_seq', (SELECT COALESCE(MAX(id), 1) FROM alerts))"); err != nil {
|
||||
log.Printf("sequence reset error: %v", err)
|
||||
}
|
||||
if _, err := tx.Exec("SELECT setval('users_id_seq', (SELECT COALESCE(MAX(id), 1) FROM users))"); err != nil {
|
||||
log.Printf("sequence reset error: %v", err)
|
||||
}
|
||||
if _, err := tx.Exec("SELECT setval('maintenance_windows_id_seq', (SELECT COALESCE(MAX(id), 1) FROM maintenance_windows))"); err != nil {
|
||||
log.Printf("sequence reset error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
+29
-10
@@ -2,6 +2,7 @@ package store
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"log"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
@@ -98,21 +99,39 @@ func (d *SQLiteDialect) UpsertNodeSQL() string {
|
||||
|
||||
func (d *SQLiteDialect) ResetSequenceOnEmpty(db *sql.DB, table string) {
|
||||
var count int
|
||||
db.QueryRow("SELECT COUNT(*) FROM " + table).Scan(&count)
|
||||
_ = db.QueryRow("SELECT COUNT(*) FROM " + table).Scan(&count) //nolint:errcheck
|
||||
if count == 0 {
|
||||
db.Exec("DELETE FROM sqlite_sequence WHERE name=?", table)
|
||||
if _, err := db.Exec("DELETE FROM sqlite_sequence WHERE name=?", table); err != nil {
|
||||
log.Printf("sequence cleanup error: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (d *SQLiteDialect) ImportWipe(tx *sql.Tx) {
|
||||
tx.Exec("DELETE FROM sites")
|
||||
tx.Exec("DELETE FROM sqlite_sequence WHERE name='sites'")
|
||||
tx.Exec("DELETE FROM alerts")
|
||||
tx.Exec("DELETE FROM sqlite_sequence WHERE name='alerts'")
|
||||
tx.Exec("DELETE FROM users")
|
||||
tx.Exec("DELETE FROM sqlite_sequence WHERE name='users'")
|
||||
tx.Exec("DELETE FROM maintenance_windows")
|
||||
tx.Exec("DELETE FROM sqlite_sequence WHERE name='maintenance_windows'")
|
||||
if _, err := tx.Exec("DELETE FROM sites"); err != nil {
|
||||
log.Printf("import wipe error: %v", err)
|
||||
}
|
||||
if _, err := tx.Exec("DELETE FROM sqlite_sequence WHERE name='sites'"); err != nil {
|
||||
log.Printf("import wipe error: %v", err)
|
||||
}
|
||||
if _, err := tx.Exec("DELETE FROM alerts"); err != nil {
|
||||
log.Printf("import wipe error: %v", err)
|
||||
}
|
||||
if _, err := tx.Exec("DELETE FROM sqlite_sequence WHERE name='alerts'"); err != nil {
|
||||
log.Printf("import wipe error: %v", err)
|
||||
}
|
||||
if _, err := tx.Exec("DELETE FROM users"); err != nil {
|
||||
log.Printf("import wipe error: %v", err)
|
||||
}
|
||||
if _, err := tx.Exec("DELETE FROM sqlite_sequence WHERE name='users'"); err != nil {
|
||||
log.Printf("import wipe error: %v", err)
|
||||
}
|
||||
if _, err := tx.Exec("DELETE FROM maintenance_windows"); err != nil {
|
||||
log.Printf("import wipe error: %v", err)
|
||||
}
|
||||
if _, err := tx.Exec("DELETE FROM sqlite_sequence WHERE name='maintenance_windows'"); err != nil {
|
||||
log.Printf("import wipe error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *SQLiteDialect) ImportResetSequences(tx *sql.Tx) {}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"go-upkeep/internal/models"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -48,14 +49,16 @@ func (s *SQLStore) Init() error {
|
||||
}
|
||||
}
|
||||
for _, m := range s.dialect.MigrationsSQL() {
|
||||
s.db.Exec(m)
|
||||
if _, err := s.db.Exec(m); err != nil {
|
||||
log.Printf("migration error: %v", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SQLStore) GetSites() ([]models.Site, error) {
|
||||
bf := s.dialect.BoolFalse()
|
||||
query := fmt.Sprintf(
|
||||
query := fmt.Sprintf( //nolint:gosec // bf is a dialect boolean literal, not user input
|
||||
"SELECT id, COALESCE(name, url), url, COALESCE(type, 'http'), COALESCE(token, ''), interval, alert_id, check_ssl, threshold, max_retries, COALESCE(hostname, ''), COALESCE(port, 0), COALESCE(timeout, 0), COALESCE(method, 'GET'), COALESCE(description, ''), COALESCE(parent_id, 0), COALESCE(accepted_codes, '200-299'), COALESCE(dns_resolve_type, ''), COALESCE(dns_server, ''), COALESCE(ignore_tls, %s), COALESCE(paused, %s), COALESCE(regions, '') FROM sites",
|
||||
bf, bf,
|
||||
)
|
||||
@@ -95,7 +98,7 @@ func (s *SQLStore) AddSite(site models.Site) error {
|
||||
|
||||
func (s *SQLStore) UpdateSite(site models.Site) error {
|
||||
var existingToken string
|
||||
s.db.QueryRow(s.q("SELECT token FROM sites WHERE id=?"), site.ID).Scan(&existingToken)
|
||||
_ = s.db.QueryRow(s.q("SELECT token FROM sites WHERE id=?"), site.ID).Scan(&existingToken) //nolint:errcheck
|
||||
if site.Type == "push" && existingToken == "" {
|
||||
var err error
|
||||
existingToken, err = generateToken()
|
||||
@@ -125,7 +128,7 @@ func (s *SQLStore) DeleteSite(id int) error {
|
||||
|
||||
func (s *SQLStore) GetSiteByName(name string) (models.Site, error) {
|
||||
bf := s.dialect.BoolFalse()
|
||||
query := fmt.Sprintf(
|
||||
query := fmt.Sprintf( //nolint:gosec // bf is a dialect boolean literal, not user input
|
||||
"SELECT id, COALESCE(name, url), url, COALESCE(type, 'http'), COALESCE(token, ''), interval, alert_id, check_ssl, threshold, max_retries, COALESCE(hostname, ''), COALESCE(port, 0), COALESCE(timeout, 0), COALESCE(method, 'GET'), COALESCE(description, ''), COALESCE(parent_id, 0), COALESCE(accepted_codes, '200-299'), COALESCE(dns_resolve_type, ''), COALESCE(dns_server, ''), COALESCE(ignore_tls, %s), COALESCE(paused, %s), COALESCE(regions, '') FROM sites WHERE name = %s",
|
||||
bf, bf, s.q("?"),
|
||||
)
|
||||
@@ -502,7 +505,7 @@ func (s *SQLStore) ImportData(data models.Backup) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
defer tx.Rollback() //nolint:errcheck
|
||||
|
||||
s.dialect.ImportWipe(tx)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user