220 lines
6.4 KiB
Go
220 lines
6.4 KiB
Go
// Package config loads and validates the applesync TOML configuration.
|
|
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/BurntSushi/toml"
|
|
|
|
"applesync/internal/state"
|
|
)
|
|
|
|
type Config struct {
|
|
Global GlobalConfig `toml:"global"`
|
|
Apple AppleConfig `toml:"apple"`
|
|
Qobuz QobuzConfig `toml:"qobuz"`
|
|
Rip RipConfig `toml:"rip"`
|
|
Beets BeetsConfig `toml:"beets"`
|
|
Playlists []PlaylistConfig `toml:"playlist"`
|
|
}
|
|
|
|
type GlobalConfig struct {
|
|
DownloadsDir string `toml:"downloads_dir"`
|
|
LibraryDir string `toml:"library_dir"`
|
|
M3UDir string `toml:"m3u_dir"`
|
|
M3UPaths string `toml:"m3u_paths"` // relative | absolute
|
|
Mode string `toml:"mode"` // replace | append
|
|
ExtendedM3U bool `toml:"extended_m3u"`
|
|
Missing string `toml:"missing"` // warn | fail
|
|
DryRun bool `toml:"dry_run"`
|
|
Concurrency int `toml:"concurrency"`
|
|
StateDir string `toml:"state_dir"`
|
|
LibraryScan string `toml:"library_scan"` // auto | albums | all
|
|
Report string `toml:"report"`
|
|
}
|
|
|
|
type AppleConfig struct {
|
|
Language string `toml:"language"`
|
|
}
|
|
|
|
type QobuzConfig struct {
|
|
AppID string `toml:"app_id"`
|
|
}
|
|
|
|
type RipConfig struct {
|
|
Binary string `toml:"binary"`
|
|
ConfigPath string `toml:"config_path"`
|
|
Quality int `toml:"quality"`
|
|
Codec string `toml:"codec"`
|
|
ExtraArgs []string `toml:"extra_args"`
|
|
NoProgress bool `toml:"no_progress"`
|
|
}
|
|
|
|
type BeetsConfig struct {
|
|
Enabled bool `toml:"enabled"`
|
|
Binary string `toml:"binary"`
|
|
}
|
|
|
|
type PlaylistConfig struct {
|
|
URL string `toml:"url"`
|
|
Name string `toml:"name"`
|
|
M3U string `toml:"m3u"`
|
|
Mode string `toml:"mode"`
|
|
Enabled *bool `toml:"enabled"`
|
|
}
|
|
|
|
func (p PlaylistConfig) IsEnabled() bool {
|
|
return p.Enabled == nil || *p.Enabled
|
|
}
|
|
|
|
func defaults() Config {
|
|
return Config{
|
|
Global: GlobalConfig{
|
|
M3UPaths: "relative",
|
|
Mode: "replace",
|
|
ExtendedM3U: true,
|
|
Missing: "warn",
|
|
Concurrency: 4,
|
|
StateDir: "~/.local/state/applesync",
|
|
LibraryScan: "auto",
|
|
},
|
|
Apple: AppleConfig{Language: "en-US"},
|
|
Rip: RipConfig{
|
|
Binary: "rip",
|
|
Quality: 3,
|
|
},
|
|
Beets: BeetsConfig{Binary: "beet"},
|
|
}
|
|
}
|
|
|
|
// FindPath resolves the config path: an explicit --config wins, otherwise
|
|
// ./applesync.toml and ~/.config/applesync/config.toml are tried.
|
|
func FindPath(explicit string) (string, error) {
|
|
if strings.TrimSpace(explicit) != "" {
|
|
path := state.ExpandPath(strings.TrimSpace(explicit))
|
|
if _, err := os.Stat(path); err != nil {
|
|
return "", fmt.Errorf("config %s: %w", path, err)
|
|
}
|
|
return path, nil
|
|
}
|
|
candidates := []string{"applesync.toml", "~/.config/applesync/config.toml"}
|
|
for _, candidate := range candidates {
|
|
path := state.ExpandPath(candidate)
|
|
if info, err := os.Stat(path); err == nil && !info.IsDir() {
|
|
return path, nil
|
|
}
|
|
}
|
|
return "", fmt.Errorf("no config found; pass --config or create applesync.toml")
|
|
}
|
|
|
|
// Load reads and validates a TOML config.
|
|
func Load(path string) (*Config, error) {
|
|
cfg := defaults()
|
|
if _, err := toml.DecodeFile(path, &cfg); err != nil {
|
|
return nil, fmt.Errorf("load %s: %w", path, err)
|
|
}
|
|
cfg.Global.DownloadsDir = state.ExpandPath(strings.TrimSpace(cfg.Global.DownloadsDir))
|
|
cfg.Global.LibraryDir = state.ExpandPath(strings.TrimSpace(cfg.Global.LibraryDir))
|
|
if cfg.Global.LibraryDir == "" {
|
|
cfg.Global.LibraryDir = cfg.Global.DownloadsDir
|
|
}
|
|
cfg.Global.M3UDir = state.ExpandPath(strings.TrimSpace(cfg.Global.M3UDir))
|
|
cfg.Global.StateDir = state.ExpandPath(strings.TrimSpace(cfg.Global.StateDir))
|
|
cfg.Global.Report = state.ExpandPath(strings.TrimSpace(cfg.Global.Report))
|
|
cfg.Rip.ConfigPath = state.ExpandPath(strings.TrimSpace(cfg.Rip.ConfigPath))
|
|
cfg.Beets.Binary = strings.TrimSpace(cfg.Beets.Binary)
|
|
|
|
if err := cfg.Validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
return &cfg, nil
|
|
}
|
|
|
|
// Validate checks field values and cross-field rules.
|
|
func (c *Config) Validate() error {
|
|
g := &c.Global
|
|
if g.DownloadsDir == "" {
|
|
return fmt.Errorf("global.downloads_dir is required")
|
|
}
|
|
if g.M3UDir == "" {
|
|
return fmt.Errorf("global.m3u_dir is required")
|
|
}
|
|
g.Mode = strings.ToLower(strings.TrimSpace(g.Mode))
|
|
if g.Mode != "replace" && g.Mode != "append" {
|
|
return fmt.Errorf("global.mode must be replace or append, got %q", g.Mode)
|
|
}
|
|
g.M3UPaths = strings.ToLower(strings.TrimSpace(g.M3UPaths))
|
|
if g.M3UPaths != "relative" && g.M3UPaths != "absolute" {
|
|
return fmt.Errorf("global.m3u_paths must be relative or absolute, got %q", g.M3UPaths)
|
|
}
|
|
g.Missing = strings.ToLower(strings.TrimSpace(g.Missing))
|
|
if g.Missing != "warn" && g.Missing != "fail" {
|
|
return fmt.Errorf("global.missing must be warn or fail, got %q", g.Missing)
|
|
}
|
|
g.LibraryScan = strings.ToLower(strings.TrimSpace(g.LibraryScan))
|
|
if g.LibraryScan == "" {
|
|
g.LibraryScan = "auto"
|
|
}
|
|
if g.LibraryScan != "auto" && g.LibraryScan != "albums" && g.LibraryScan != "all" {
|
|
return fmt.Errorf("global.library_scan must be auto, albums or all, got %q", g.LibraryScan)
|
|
}
|
|
if g.Concurrency < 1 || g.Concurrency > 16 {
|
|
return fmt.Errorf("global.concurrency must be between 1 and 16, got %d", g.Concurrency)
|
|
}
|
|
if strings.TrimSpace(g.StateDir) == "" {
|
|
g.StateDir = "~/.local/state/applesync"
|
|
}
|
|
if len(c.Playlists) == 0 {
|
|
return fmt.Errorf("at least one [[playlist]] is required")
|
|
}
|
|
if strings.TrimSpace(c.Apple.Language) == "" {
|
|
c.Apple.Language = "en-US"
|
|
}
|
|
if strings.TrimSpace(c.Rip.Binary) == "" {
|
|
c.Rip.Binary = "rip"
|
|
}
|
|
if strings.TrimSpace(c.Beets.Binary) == "" {
|
|
c.Beets.Binary = "beet"
|
|
}
|
|
if c.Rip.Quality < 0 || c.Rip.Quality > 4 {
|
|
return fmt.Errorf("rip.quality must be between 0 and 4, got %d", c.Rip.Quality)
|
|
}
|
|
|
|
enabled := 0
|
|
explicitM3U := map[string]int{}
|
|
for i := range c.Playlists {
|
|
p := &c.Playlists[i]
|
|
p.URL = strings.TrimSpace(p.URL)
|
|
p.Name = strings.TrimSpace(p.Name)
|
|
p.M3U = strings.TrimSpace(p.M3U)
|
|
p.Mode = strings.ToLower(strings.TrimSpace(p.Mode))
|
|
if p.Mode == "" {
|
|
p.Mode = g.Mode
|
|
}
|
|
if p.Mode != "replace" && p.Mode != "append" {
|
|
return fmt.Errorf("playlist %d: mode must be replace or append, got %q", i+1, p.Mode)
|
|
}
|
|
if !p.IsEnabled() {
|
|
continue
|
|
}
|
|
enabled++
|
|
if p.URL == "" {
|
|
return fmt.Errorf("playlist %d: url is required", i+1)
|
|
}
|
|
if p.M3U != "" {
|
|
key := filepath.Clean(p.M3U)
|
|
explicitM3U[key]++
|
|
if explicitM3U[key] > 1 {
|
|
return fmt.Errorf("playlist %d: m3u %q is used by more than one playlist", i+1, p.M3U)
|
|
}
|
|
}
|
|
}
|
|
if enabled == 0 {
|
|
return fmt.Errorf("no enabled [[playlist]] entries")
|
|
}
|
|
return nil
|
|
}
|