From 42aed9decbf63708b80dcd48d12a133705e29f48 Mon Sep 17 00:00:00 2001 From: Dmitrii Andreev Date: Mon, 7 Apr 2025 11:32:14 +0300 Subject: [PATCH] linter --- cmd/icecast-ripper/main.go | 6 +++++ internal/config/config.go | 6 ----- internal/database/database.go | 6 ++++- internal/hash/hash.go | 6 ++++- internal/recorder/recorder.go | 30 ++++++++++++++++++------- internal/streamchecker/streamchecker.go | 6 ++++- 6 files changed, 43 insertions(+), 17 deletions(-) diff --git a/cmd/icecast-ripper/main.go b/cmd/icecast-ripper/main.go index 7b7bcad..397a32b 100644 --- a/cmd/icecast-ripper/main.go +++ b/cmd/icecast-ripper/main.go @@ -35,6 +35,12 @@ func main() { slog.Info("Starting icecast-ripper", "version", "0.1.0") // Updated version + // Validate essential config + if cfg.StreamURL == "" { + slog.Error("Configuration error: STREAM_URL must be set.") + os.Exit(1) + } + // Initialize database db, err := database.InitDB(cfg.DatabasePath) if err != nil { diff --git a/internal/config/config.go b/internal/config/config.go index 04bc6f1..21a3188 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -67,11 +67,5 @@ func LoadConfig() (*Config, error) { return nil, err } - // Ensure required fields are set - if config.StreamURL == "" { - // Consider returning an error or logging a fatal error if essential config is missing - // return nil, errors.New("STREAM_URL environment variable is required") - } - return &config, nil } diff --git a/internal/database/database.go b/internal/database/database.go index 3e2d722..d71e6d6 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -87,7 +87,11 @@ func (db *DB) GetRecordedFiles(limit int) ([]RecordedFile, error) { if err != nil { return nil, fmt.Errorf("failed to query recorded files: %w", err) } - defer rows.Close() + defer func() { + if err := rows.Close(); err != nil { + slog.Error("Failed to close rows", "error", err) + } + }() var files []RecordedFile for rows.Next() { diff --git a/internal/hash/hash.go b/internal/hash/hash.go index 75e1a68..44bd88e 100644 --- a/internal/hash/hash.go +++ b/internal/hash/hash.go @@ -26,7 +26,11 @@ func GenerateFileHash(filePath string) (string, error) { if err != nil { return "", fmt.Errorf("failed to open file %s for hashing: %w", filePath, err) } - defer file.Close() + defer func() { + if err := file.Close(); err != nil { + slog.Error("Failed to close file", "path", filePath, "error", err) + } + }() hasher := sha256.New() diff --git a/internal/recorder/recorder.go b/internal/recorder/recorder.go index e13079d..3b63588 100644 --- a/internal/recorder/recorder.go +++ b/internal/recorder/recorder.go @@ -103,7 +103,9 @@ func (r *Recorder) recordStream(ctx context.Context, streamURL string) { if tempFilePath != "" { if _, err := os.Stat(tempFilePath); err == nil { slog.Warn("Removing leftover temporary file", "path", tempFilePath) - os.Remove(tempFilePath) + if err := os.Remove(tempFilePath); err != nil { + slog.Error("Failed to remove temporary file", "path", tempFilePath, "error", err) + } } } }() @@ -136,7 +138,9 @@ func (r *Recorder) recordStream(ctx context.Context, streamURL string) { } else { slog.Error("Failed to download stream", "error", err) // No need to keep the temp file if download failed unexpectedly - os.Remove(tempFilePath) + if err := os.Remove(tempFilePath); err != nil { + slog.Error("Failed to remove temporary file after download error", "path", tempFilePath, "error", err) + } tempFilePath = "" // Prevent deferred cleanup from trying again return } @@ -145,7 +149,9 @@ func (r *Recorder) recordStream(ctx context.Context, streamURL string) { // If no bytes were written (e.g., stream closed immediately or cancelled before data), discard. if bytesWritten == 0 { slog.Warn("No data written to temporary file, discarding.", "path", tempFilePath) - os.Remove(tempFilePath) + if err := os.Remove(tempFilePath); err != nil { + slog.Error("Failed to remove temporary file after no data written", "path", tempFilePath, "error", err) + } tempFilePath = "" // Prevent deferred cleanup return } @@ -157,8 +163,10 @@ func (r *Recorder) recordStream(ctx context.Context, streamURL string) { fileHash, err := hash.GenerateFileHash(tempFilePath) if err != nil { slog.Error("Failed to generate file hash", "path", tempFilePath, "error", err) - os.Remove(tempFilePath) // Clean up if hashing fails - tempFilePath = "" // Prevent deferred cleanup + if err := os.Remove(tempFilePath); err != nil { + slog.Error("Failed to remove temporary file after hash error", "path", tempFilePath, "error", err) + } + tempFilePath = "" // Prevent deferred cleanup return } @@ -170,8 +178,10 @@ func (r *Recorder) recordStream(ctx context.Context, streamURL string) { // Move temporary file to final location if err := os.Rename(tempFilePath, finalPath); err != nil { slog.Error("Failed to move temporary file to final location", "temp", tempFilePath, "final", finalPath, "error", err) - os.Remove(tempFilePath) // Attempt cleanup of temp file - tempFilePath = "" // Prevent deferred cleanup + if err := os.Remove(tempFilePath); err != nil { + slog.Error("Failed to remove temporary file after move error", "path", tempFilePath, "error", err) + } + tempFilePath = "" // Prevent deferred cleanup return } tempFilePath = "" // File moved, clear path to prevent deferred cleanup @@ -201,7 +211,11 @@ func (r *Recorder) downloadStream(ctx context.Context, streamURL string, writer } return 0, fmt.Errorf("failed to connect to stream: %w", err) } - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + slog.Error("Failed to close response body", "error", err) + } + }() if resp.StatusCode != http.StatusOK { return 0, fmt.Errorf("unexpected status code: %s", resp.Status) diff --git a/internal/streamchecker/streamchecker.go b/internal/streamchecker/streamchecker.go index 47e0c66..709324b 100644 --- a/internal/streamchecker/streamchecker.go +++ b/internal/streamchecker/streamchecker.go @@ -44,7 +44,11 @@ func (c *Checker) IsLive() (bool, error) { slog.Warn("Failed to connect to stream URL during check", "url", c.streamURL, "error", err) return false, nil // Treat connection errors as 'not live' } - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + slog.Error("Failed to close response body", "error", err) + } + }() // A 200 OK status generally indicates the stream is live and broadcasting. if resp.StatusCode == http.StatusOK {