refactor: simplify homeDir and appConfig usage

This commit is contained in:
Mathis Maquenne
2025-06-23 20:58:08 -04:00
parent a619b37922
commit 33e774d078
7 changed files with 46 additions and 31 deletions
+15 -2
View File
@@ -6,11 +6,15 @@ import (
"fmt"
"time"
"github.com/mathismqn/godeez/internal/config"
"github.com/mathismqn/godeez/internal/downloader"
"github.com/spf13/cobra"
)
var opts downloader.Options
var (
opts downloader.Options
cfgPath string
)
var downloadCmd = &cobra.Command{
Use: "download",
@@ -20,6 +24,7 @@ var downloadCmd = &cobra.Command{
func init() {
RootCmd.AddCommand(downloadCmd)
downloadCmd.PersistentFlags().StringVar(&cfgPath, "config", "", "config file (default ~/.godeez/config.toml)")
downloadCmd.PersistentFlags().StringVarP(&opts.Quality, "quality", "q", "best", "download quality [mp3_128, mp3_320, flac, best]")
downloadCmd.PersistentFlags().DurationVarP(&opts.Timeout, "timeout", "t", 2*time.Minute, "timeout for each download (e.g. 10s, 1m, 2m30s)")
downloadCmd.PersistentFlags().BoolVar(&opts.BPM, "bpm", false, "fetch BPM/key and add to file tags")
@@ -42,12 +47,20 @@ func newDownloadCmd(resourceType string) *cobra.Command {
Short: fmt.Sprintf("Download songs from %s %s", article, resourceType),
Args: cobra.ExactArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
appConfig, err := config.New(cfgPath)
if err != nil {
return err
}
cmd.SetContext(context.WithValue(cmd.Context(), "appConfig", appConfig))
return opts.Validate()
},
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
dl := downloader.New(appConfig, resourceType)
appConfigVal := ctx.Value("appConfig")
appConfig, _ := appConfigVal.(*config.Config)
dl := downloader.New(appConfig, resourceType)
if err := dl.Run(ctx, opts, args[0]); err != nil {
if errors.Is(err, context.Canceled) {
return nil
+3 -14
View File
@@ -4,35 +4,24 @@ import (
"fmt"
"os"
"github.com/mathismqn/godeez/internal/config"
"github.com/mathismqn/godeez/internal/watcher"
"github.com/spf13/cobra"
)
var (
cfgPath string
appConfig *config.Config
)
var RootCmd = &cobra.Command{
Use: "godeez",
Short: "GoDeez is a tool to download music from Deezer",
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
var err error
appConfig, err = config.New(cfgPath)
homeDir, err := os.UserHomeDir()
if err != nil {
return err
return fmt.Errorf("failed to get home directory: %w", err)
}
if err := watcher.EnsureAutostart(); err != nil {
if err := watcher.EnsureAutostart(homeDir); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to install autostart for watcher: %v\n", err)
}
return nil
},
}
func init() {
RootCmd.PersistentFlags().StringVar(&cfgPath, "config", "", "config file (default ~/.godeez/config.toml)")
}
+13
View File
@@ -1,6 +1,9 @@
package cmd
import (
"context"
"github.com/mathismqn/godeez/internal/config"
"github.com/mathismqn/godeez/internal/watcher"
"github.com/spf13/cobra"
)
@@ -9,8 +12,18 @@ var watchRunCmd = &cobra.Command{
Use: "run",
Short: "Start the background playlist watcher",
Hidden: true,
PreRun: func(cmd *cobra.Command, args []string) {
appConfig, err := config.New("")
if err != nil {
return
}
cmd.SetContext(context.WithValue(cmd.Context(), "appConfig", appConfig))
},
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
appConfigVal := ctx.Value("appConfig")
appConfig, _ := appConfigVal.(*config.Config)
w := watcher.New(appConfig)
w.Run(ctx, opts)
},