refactor: open track store explicitly and remove global state
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/mathismqn/godeez/internal/fileutil"
|
||||
"github.com/mathismqn/godeez/internal/store"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
@@ -14,7 +13,7 @@ type Config struct {
|
||||
OutputDir string
|
||||
}
|
||||
|
||||
func New() (*Config, error) {
|
||||
func Load() (*Config, error) {
|
||||
arl := os.Getenv("DEEZER_ARL")
|
||||
|
||||
homeDir, err := os.UserHomeDir()
|
||||
@@ -27,24 +26,6 @@ func New() (*Config, error) {
|
||||
return nil, fmt.Errorf("failed to create output directory: %w", err)
|
||||
}
|
||||
|
||||
// Migrate tracks.db from ~/.godeez/ to output dir
|
||||
oldDB := filepath.Join(homeDir, ".godeez", "tracks.db")
|
||||
newDB := filepath.Join(outputDir, ".tracks.db")
|
||||
if _, err := os.Stat(oldDB); err == nil {
|
||||
if _, err := os.Stat(newDB); os.IsNotExist(err) {
|
||||
os.Rename(oldDB, newDB)
|
||||
}
|
||||
}
|
||||
|
||||
// Clean up old config directory
|
||||
oldDir := filepath.Join(homeDir, ".godeez")
|
||||
os.Remove(filepath.Join(oldDir, "config.toml"))
|
||||
os.Remove(oldDir) // fails silently if not empty
|
||||
|
||||
if err := store.OpenDB(outputDir); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Config{
|
||||
ARLCookie: arl,
|
||||
OutputDir: outputDir,
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func MigrateLegacy(outputDir string) {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
oldDB := filepath.Join(homeDir, ".godeez", "tracks.db")
|
||||
newDB := filepath.Join(outputDir, ".tracks.db")
|
||||
if _, err := os.Stat(oldDB); err == nil {
|
||||
if _, err := os.Stat(newDB); os.IsNotExist(err) {
|
||||
os.Rename(oldDB, newDB)
|
||||
}
|
||||
}
|
||||
|
||||
oldDir := filepath.Join(homeDir, ".godeez")
|
||||
os.Remove(filepath.Join(oldDir, "config.toml"))
|
||||
os.Remove(oldDir)
|
||||
}
|
||||
Reference in New Issue
Block a user