return errors and handle os.Exit in cmd file
This commit is contained in:
@@ -2,6 +2,7 @@ package cmd
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"os"
|
||||
|
||||
ripsort "git.kapelle.org/niklas/ripsort/internal"
|
||||
"github.com/alexflint/go-arg"
|
||||
@@ -49,17 +50,23 @@ func Run() {
|
||||
|
||||
switch {
|
||||
case args.Info != nil:
|
||||
ripsort.Scan(args.Info.File)
|
||||
if err := ripsort.Scan(args.Info.File); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
case args.Sort != nil:
|
||||
ripsort.Sort(args.Sort.Dst, args.Sort.Path)
|
||||
if err := ripsort.Sort(args.Sort.Dst, args.Sort.Path); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
case args.SyncPlaylists != nil:
|
||||
ripsort.SyncPlaylists(
|
||||
if err := ripsort.SyncPlaylists(
|
||||
args.SyncPlaylists.SpotifyClientID,
|
||||
args.SyncPlaylists.SpotifyClientSecret,
|
||||
args.SyncPlaylists.NavidromeURL,
|
||||
args.SyncPlaylists.NavidromeUser,
|
||||
args.SyncPlaylists.NavidromePass,
|
||||
)
|
||||
); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
default:
|
||||
p.Fail("Must specify command")
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user