return errors and handle os.Exit in cmd file
This commit is contained in:
@@ -2,6 +2,7 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"os"
|
||||||
|
|
||||||
ripsort "git.kapelle.org/niklas/ripsort/internal"
|
ripsort "git.kapelle.org/niklas/ripsort/internal"
|
||||||
"github.com/alexflint/go-arg"
|
"github.com/alexflint/go-arg"
|
||||||
@@ -49,17 +50,23 @@ func Run() {
|
|||||||
|
|
||||||
switch {
|
switch {
|
||||||
case args.Info != nil:
|
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:
|
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:
|
case args.SyncPlaylists != nil:
|
||||||
ripsort.SyncPlaylists(
|
if err := ripsort.SyncPlaylists(
|
||||||
args.SyncPlaylists.SpotifyClientID,
|
args.SyncPlaylists.SpotifyClientID,
|
||||||
args.SyncPlaylists.SpotifyClientSecret,
|
args.SyncPlaylists.SpotifyClientSecret,
|
||||||
args.SyncPlaylists.NavidromeURL,
|
args.SyncPlaylists.NavidromeURL,
|
||||||
args.SyncPlaylists.NavidromeUser,
|
args.SyncPlaylists.NavidromeUser,
|
||||||
args.SyncPlaylists.NavidromePass,
|
args.SyncPlaylists.NavidromePass,
|
||||||
)
|
); err != nil {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
p.Fail("Must specify command")
|
p.Fail("Must specify command")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,12 +10,12 @@ import (
|
|||||||
"git.kapelle.org/niklas/ripsort/internal/metadata"
|
"git.kapelle.org/niklas/ripsort/internal/metadata"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Scan(filePath string) {
|
func Scan(filePath string) error {
|
||||||
info, err := metadata.ReadAudioTags(filePath)
|
info, err := metadata.ReadAudioTags(filePath)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Reading metadata", "file", filePath, "err", err)
|
slog.Error("Reading metadata", "file", filePath, "err", err)
|
||||||
os.Exit(1)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Title: %s\n", info.Title)
|
fmt.Printf("Title: %s\n", info.Title)
|
||||||
@@ -27,26 +27,28 @@ func Scan(filePath string) {
|
|||||||
|
|
||||||
sortPath := pathForFile(filePath, *info)
|
sortPath := pathForFile(filePath, *info)
|
||||||
fmt.Printf("Sort path: %s\n", sortPath)
|
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)
|
info, err := os.Stat(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Failed to stat path", "file", path, "err", err)
|
slog.Error("Failed to stat path", "file", path, "err", err)
|
||||||
os.Exit(1)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle single file
|
// Handle single file
|
||||||
if !info.IsDir() {
|
if !info.IsDir() {
|
||||||
if !fileSupported(path) {
|
if !fileSupported(path) {
|
||||||
slog.Error("Unsupported file format", "file", path)
|
slog.Error("Unsupported file format", "file", path)
|
||||||
os.Exit(1)
|
return err
|
||||||
}
|
}
|
||||||
if err := sortSong(path, dst, true); err != nil {
|
if err := sortSong(path, dst, true); err != nil {
|
||||||
slog.Error("Failed to sort single file", "file", path, "err", err)
|
slog.Error("Failed to sort single file", "file", path, "err", err)
|
||||||
os.Exit(1)
|
return err
|
||||||
}
|
}
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle directory
|
// Handle directory
|
||||||
@@ -70,6 +72,7 @@ func Sort(dst, path string) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
os.Exit(1)
|
return err
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user