From d3c277eb7f88ceba682bb97224c11fdc35ba4107 Mon Sep 17 00:00:00 2001 From: Niklas Kapelle Date: Fri, 9 Jan 2026 17:44:45 +0100 Subject: [PATCH] use propper logger --- internal/art.go | 24 +++++++++++++++++++----- internal/cfg.go | 4 ++++ internal/gamefile.go | 8 ++++++-- internal/gamelist.go | 6 ++++-- internal/oplcli.go | 38 +++++++++++++++++++------------------- 5 files changed, 52 insertions(+), 28 deletions(-) diff --git a/internal/art.go b/internal/art.go index 155c691..c2100b5 100644 --- a/internal/art.go +++ b/internal/art.go @@ -3,6 +3,7 @@ package oplcli import ( "fmt" "io" + "log/slog" "net/http" "os" "path" @@ -38,12 +39,21 @@ func getArtUrlForGameFromGithub(gameID string) artUrls { } } -func downloadArtForGame(gameID string, targetDir string) error { - +func downloadArtForGame(gameID string, targetDir string) { urls := getArtUrlForGameFromGithub(gameID) - dl := func(url, kind string) (bool, error) { - return downloadFile(url, path.Join(targetDir, fmt.Sprintf("%s_%s.png", gameID, kind))) + totalDownloads := 0 + + dl := func(url, kind string) { + file := path.Join(targetDir, fmt.Sprintf("%s_%s.png", gameID, kind)) + found, err := downloadFile(url, file) + if err != nil { + slog.Warn("Failed to download art file", "gameID", gameID, "url", url) + } + if found { + slog.Debug("Downloaded art file", "gameID", gameID, "kind", kind, "target", file) + totalDownloads++ + } } dl(urls.cover, "COV") @@ -54,7 +64,11 @@ func downloadArtForGame(gameID string, targetDir string) error { dl(urls.screenshot2, "SRC2") dl(urls.spine, "LAB") - return nil + if totalDownloads == 0 { + slog.Error("No art files found", "gameID", gameID) + } else { + slog.Info("Finished downloading art files", "gameID", gameID, "total", totalDownloads) + } } func downloadFile(url, target string) (bool, error) { diff --git a/internal/cfg.go b/internal/cfg.go index caefd1f..f5ba4a7 100644 --- a/internal/cfg.go +++ b/internal/cfg.go @@ -3,6 +3,7 @@ package oplcli import ( "fmt" "io" + "log/slog" "net/http" "os" "path" @@ -23,18 +24,21 @@ func downloadCfgForGame(gameID string, output string, mode FileMode) error { file, err := openFileBasedOnMode(filePath, mode) if err != nil { + slog.Error("Failed to open cfg file", "file", filePath, "gameID", gameID, "err", err) return err } defer file.Close() resp, err := http.Get(url) if err != nil { + slog.Error("Failed to download cfg file", "file", filePath, "gameID", gameID, "err", err) return err } defer resp.Body.Close() _, err = io.Copy(file, resp.Body) if err != nil { + slog.Error("Failed to write cfg file", "file", filePath, "gameID", gameID, "err", err) return err } diff --git a/internal/gamefile.go b/internal/gamefile.go index 80e3a19..f0e4fc1 100644 --- a/internal/gamefile.go +++ b/internal/gamefile.go @@ -2,6 +2,7 @@ package oplcli import ( "fmt" + "log/slog" "os" "path/filepath" "regexp" @@ -11,7 +12,8 @@ import ( func scanGameFileForID(filename string) (string, error) { file, err := os.Open(filename) if err != nil { - return "", fmt.Errorf("failed to open file: %w", err) + slog.Error("Failed to open file", "file", filename, "err", err) + return "", err } defer file.Close() @@ -20,11 +22,13 @@ func scanGameFileForID(filename string) (string, error) { n, err := file.Read(buffer) if err != nil && n == 0 { - return "", fmt.Errorf("failed to read file: %w", err) + slog.Error("Failed to read file", "file", file, "err", err) + return "", err } id, found := scanBufferForID(buffer[:n]) if !found { + slog.Error("No PS2 ID found in the file", "file", file) return "", fmt.Errorf("no PS2 game ID found in file") } diff --git a/internal/gamelist.go b/internal/gamelist.go index 8878db7..4e2ea72 100644 --- a/internal/gamelist.go +++ b/internal/gamelist.go @@ -3,6 +3,7 @@ package oplcli import ( "bufio" "fmt" + "log/slog" "os" "strings" ) @@ -16,15 +17,16 @@ func loadGamelist(filepath string) (map[string]string, error) { gameList := make(map[string]string) + lineNum := 0 scanner := bufio.NewScanner(file) for scanner.Scan() { line := scanner.Text() + lineNum++ substrings := strings.SplitN(line, " ", 2) if len(substrings) != 2 { - // TODO: something in the gamelist file is not formatted right - fmt.Printf("Malformed: %s\n", line) + slog.Warn("Line in gamelist is malformed", "line", lineNum) continue } diff --git a/internal/oplcli.go b/internal/oplcli.go index 408c770..6bbad79 100644 --- a/internal/oplcli.go +++ b/internal/oplcli.go @@ -1,7 +1,7 @@ package oplcli import ( - "fmt" + "log/slog" "os" ) @@ -12,67 +12,67 @@ type Config struct { func Scan(files []string, config Config) { gameList, err := loadGamelist(config.GameListLocation) if err != nil { - fmt.Printf("Failed to load game list: %v", err) + slog.Error("Failed to load game list", "file", config.GameListLocation) os.Exit(1) } for _, file := range files { - fmt.Printf("Scanning file: %s\n", file) + slog.Info("Scanning file", "file", file) gameID, err := scanGameFileForID(file) if err != nil { - fmt.Printf("Failed to scan game file: %s", err) + slog.Error("Failed to scan game file", "file", file) os.Exit(1) } - fmt.Printf("Found game ID: %s\n", gameID) + slog.Info("Found game ID", "id", gameID) gameName := gameList[gameID] - fmt.Printf("Found game name: %s\n", gameName) + slog.Info("Found game name", "name", gameName) } } func Rename(files []string, config Config) { gameList, err := loadGamelist(config.GameListLocation) if err != nil { - fmt.Printf("Failed to load game list: %v", err) + slog.Error("Failed to load game list", "file", config.GameListLocation) os.Exit(1) } for _, file := range files { - fmt.Printf("Scanning file: %s\n", file) + slog.Info("Scanning file", "file", file) gameID, err := scanGameFileForID(file) if err != nil { - fmt.Printf("Failed to scan game file: %s", err) + slog.Error("Failed to scan game file", "file", file) os.Exit(1) } - fmt.Printf("Found game ID: %s\n", gameID) + slog.Info("Found game ID", "id", gameID) gameName := gameList[gameID] - fmt.Printf("Found game name: %s\n", gameName) + slog.Info("Found game name", "name", gameName) newPath, err := renameGameFile(file, gameName) if err != nil { - fmt.Printf("Failed to rename file: %v", err) + slog.Error("Failed to reanme file", "file", file, "gameName", gameName, "err", err) os.Exit(1) } - fmt.Printf("Renamed %s to %s", file, newPath) + slog.Info("Renamed file", "file", file, "to", newPath) } } func DownloadGameArt(files []string, output string, config Config) { for _, file := range files { - fmt.Printf("Scanning file: %s\n", file) + slog.Info("Scanning file", "file", file) gameID, err := scanGameFileForID(file) if err != nil { - fmt.Printf("Failed to scan game file: %s", err) + slog.Error("Failed to scan game file", "file", file) os.Exit(1) } - fmt.Printf("Found game ID: %s\n", gameID) + slog.Info("Found game ID", "id", gameID) downloadArtForGame(gameID, output) } @@ -80,15 +80,15 @@ func DownloadGameArt(files []string, output string, config Config) { func DownloadCfg(files []string, output string, mode FileMode, config Config) { for _, file := range files { - fmt.Printf("Scanning file: %s\n", file) + slog.Info("Scanning file", "file", file) gameID, err := scanGameFileForID(file) if err != nil { - fmt.Printf("Failed to scan game file: %s", err) + slog.Error("Failed to scan game file", "file", file) os.Exit(1) } - fmt.Printf("Found game ID: %s\n", gameID) + slog.Info("Found game ID", "id", gameID) downloadCfgForGame(gameID, output, mode) }