return errors and handle os.Exit in cmd file

This commit is contained in:
2026-05-11 22:51:47 +02:00
parent 81a9fe5bfc
commit fbd80ba52d
2 changed files with 22 additions and 12 deletions

View File

@@ -10,12 +10,12 @@ import (
"git.kapelle.org/niklas/ripsort/internal/metadata"
)
func Scan(filePath string) {
func Scan(filePath string) error {
info, err := metadata.ReadAudioTags(filePath)
if err != nil {
slog.Error("Reading metadata", "file", filePath, "err", err)
os.Exit(1)
return err
}
fmt.Printf("Title: %s\n", info.Title)
@@ -27,26 +27,28 @@ func Scan(filePath string) {
sortPath := pathForFile(filePath, *info)
fmt.Printf("Sort path: %s\n", sortPath)
return nil
}
func Sort(dst, path string) {
func Sort(dst, path string) error {
info, err := os.Stat(path)
if err != nil {
slog.Error("Failed to stat path", "file", path, "err", err)
os.Exit(1)
return err
}
// Handle single file
if !info.IsDir() {
if !fileSupported(path) {
slog.Error("Unsupported file format", "file", path)
os.Exit(1)
return err
}
if err := sortSong(path, dst, true); err != nil {
slog.Error("Failed to sort single file", "file", path, "err", err)
os.Exit(1)
return err
}
return
return nil
}
// Handle directory
@@ -70,6 +72,7 @@ func Sort(dst, path string) {
})
if err != nil {
os.Exit(1)
return err
}
return nil
}