refactor: simplify codebase (-209 lines)
This commit is contained in:
+25
-25
@@ -12,6 +12,10 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type contextKey string
|
||||
|
||||
const appConfigKey contextKey = "appConfig"
|
||||
|
||||
var (
|
||||
opts downloader.Options
|
||||
cfgPath string
|
||||
@@ -41,51 +45,47 @@ func init() {
|
||||
}
|
||||
|
||||
func newDownloadCmd(resourceType string) *cobra.Command {
|
||||
article := "a"
|
||||
if resourceType == "album" {
|
||||
article = "an"
|
||||
}
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: fmt.Sprintf("%s <%s_id>", resourceType, resourceType),
|
||||
Short: fmt.Sprintf("Download songs from %s %s", article, resourceType),
|
||||
Short: downloadShort(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))
|
||||
cmd.SetContext(context.WithValue(cmd.Context(), appConfigKey, appConfig))
|
||||
|
||||
opts.Quality = strings.ToLower(opts.Quality)
|
||||
|
||||
return opts.Validate()
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
ctx := cmd.Context()
|
||||
appConfigVal := ctx.Value("appConfig")
|
||||
appConfig, _ := appConfigVal.(*config.Config)
|
||||
appConfig := cmd.Context().Value(appConfigKey).(*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
|
||||
}
|
||||
|
||||
return err
|
||||
err := downloader.New(appConfig, resourceType).Run(cmd.Context(), opts, args[0])
|
||||
if errors.Is(err, context.Canceled) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return nil
|
||||
return err
|
||||
},
|
||||
}
|
||||
|
||||
switch resourceType {
|
||||
case "artist":
|
||||
cmd.Short = "Download top songs from an artist"
|
||||
if resourceType == "artist" {
|
||||
cmd.Flags().IntVarP(&opts.Limit, "limit", "l", 10, "number of songs to download")
|
||||
case "track":
|
||||
cmd.Short = "Download a single track"
|
||||
}
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func downloadShort(resourceType string) string {
|
||||
switch resourceType {
|
||||
case "artist":
|
||||
return "Download top songs from an artist"
|
||||
case "track":
|
||||
return "Download a single track"
|
||||
case "album":
|
||||
return "Download songs from an album"
|
||||
default:
|
||||
return fmt.Sprintf("Download songs from a %s", resourceType)
|
||||
}
|
||||
}
|
||||
|
||||
-19
@@ -8,23 +8,4 @@ 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 {
|
||||
// TEMPORARILY DISABLED:
|
||||
// Watcher autostart (EnsureAutostart) has been disabled due to
|
||||
// concurrency issues with database access (e.g., when using `download`).
|
||||
// To re-enable, uncomment the line below.
|
||||
|
||||
/*
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get home directory: %w", err)
|
||||
}
|
||||
|
||||
if err := watcher.EnsureAutostart(homeDir); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: failed to install autostart for watcher: %v\n", err)
|
||||
}
|
||||
*/
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
+2
-4
@@ -4,15 +4,13 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// NOTE: The watch command is disabled due to database concurrency issues.
|
||||
// To re-enable, uncomment RootCmd.AddCommand(watchCmd) in init().
|
||||
var watchCmd = &cobra.Command{
|
||||
Use: "watch",
|
||||
Short: "Watch playlists and auto-download new tracks",
|
||||
}
|
||||
|
||||
// TEMPORARILY DISABLED:
|
||||
// The `watch` command and all its subcommands are currently disabled
|
||||
// due to known issues (e.g., database access conflicts with `download`).
|
||||
// To re-enable, uncomment the line below.
|
||||
func init() {
|
||||
// RootCmd.AddCommand(watchCmd)
|
||||
}
|
||||
|
||||
+2
-2
@@ -15,6 +15,7 @@ var watchAddCmd = &cobra.Command{
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
id := args[0]
|
||||
|
||||
ok, err := store.IsWatched(id)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -30,12 +31,11 @@ var watchAddCmd = &cobra.Command{
|
||||
BPM: opts.BPM,
|
||||
Timeout: opts.Timeout,
|
||||
}
|
||||
|
||||
if err := playlist.Save(); err != nil {
|
||||
return fmt.Errorf("failed to add playlist %s to watch list: %w", id, err)
|
||||
}
|
||||
fmt.Printf("Playlist %s added to watch list\n", id)
|
||||
|
||||
fmt.Printf("Playlist %s added to watch list\n", id)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
+3
-7
@@ -17,15 +17,11 @@ var watchRunCmd = &cobra.Command{
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
cmd.SetContext(context.WithValue(cmd.Context(), "appConfig", appConfig))
|
||||
cmd.SetContext(context.WithValue(cmd.Context(), appConfigKey, 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)
|
||||
appConfig, _ := cmd.Context().Value(appConfigKey).(*config.Config)
|
||||
watcher.New(appConfig).Run(cmd.Context(), opts)
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user