From 283c5d76e9ee8e719ef01909729b93f1b43aeffc Mon Sep 17 00:00:00 2001 From: Mathis Maquenne <124215603+mathismqn@users.noreply.github.com> Date: Sat, 9 Aug 2025 19:17:29 +0200 Subject: [PATCH] feat(cli): add --strict flag to fail track download if quality is unavailable --- cmd/download.go | 3 ++- internal/downloader/client.go | 3 +++ internal/downloader/options.go | 13 ++++++++++--- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/cmd/download.go b/cmd/download.go index 08985cd..12d936a 100644 --- a/cmd/download.go +++ b/cmd/download.go @@ -29,6 +29,7 @@ func init() { downloadCmd.PersistentFlags().StringVarP(&opts.Quality, "quality", "q", "flac", "download quality [mp3_128, mp3_320, flac]") 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") + downloadCmd.PersistentFlags().BoolVar(&opts.Strict, "strict", false, "fail the song download if the quality is not available") downloadCmd.AddCommand( newDownloadCmd("album"), @@ -78,7 +79,7 @@ func newDownloadCmd(resourceType string) *cobra.Command { if resourceType == "artist" { cmd.Flags().IntVarP(&opts.Limit, "limit", "l", 10, "number of songs to download") - cmd.Short = fmt.Sprintf("Download top songs from an artist") + cmd.Short = "Download top songs from an artist" } return cmd diff --git a/internal/downloader/client.go b/internal/downloader/client.go index 800a376..6af653b 100644 --- a/internal/downloader/client.go +++ b/internal/downloader/client.go @@ -179,6 +179,9 @@ func (c *Client) downloadSong(ctx context.Context, resource deezer.Resource, son if err != nil { return nil, fmt.Errorf("failed to get media format: %w", err) } + if opts.Strict && strings.ToLower(mediaFormat) != opts.Quality { + return nil, fmt.Errorf("requested quality '%s' not available", opts.Quality) + } if path, skip := c.shouldSkipDownload(ctx, song.ID, mediaFormat); skip { return nil, SkipError{Path: path} diff --git a/internal/downloader/options.go b/internal/downloader/options.go index 8bbf11c..efd83c2 100644 --- a/internal/downloader/options.go +++ b/internal/downloader/options.go @@ -14,16 +14,23 @@ var validQualities = map[string]bool{ type Options struct { Quality string Timeout time.Duration - BPM bool Limit int + BPM bool + Strict bool } func (o *Options) Validate() error { if !validQualities[o.Quality] { return fmt.Errorf("invalid quality option: %s", o.Quality) } - if o.Limit < 0 { - return fmt.Errorf("limit must be a non-negative integer") + if o.Timeout <= 0 { + return fmt.Errorf("timeout must be a positive duration") + } + if o.Limit <= 0 { + return fmt.Errorf("limit must be a positive integer") + } + if o.Limit > 100 { + return fmt.Errorf("limit must not exceed 100") } return nil