feat(cli): add --strict flag to fail track download if quality is unavailable
This commit is contained in:
+2
-1
@@ -29,6 +29,7 @@ func init() {
|
|||||||
downloadCmd.PersistentFlags().StringVarP(&opts.Quality, "quality", "q", "flac", "download quality [mp3_128, mp3_320, flac]")
|
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().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.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(
|
downloadCmd.AddCommand(
|
||||||
newDownloadCmd("album"),
|
newDownloadCmd("album"),
|
||||||
@@ -78,7 +79,7 @@ func newDownloadCmd(resourceType string) *cobra.Command {
|
|||||||
|
|
||||||
if resourceType == "artist" {
|
if resourceType == "artist" {
|
||||||
cmd.Flags().IntVarP(&opts.Limit, "limit", "l", 10, "number of songs to download")
|
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
|
return cmd
|
||||||
|
|||||||
@@ -179,6 +179,9 @@ func (c *Client) downloadSong(ctx context.Context, resource deezer.Resource, son
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get media format: %w", err)
|
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 {
|
if path, skip := c.shouldSkipDownload(ctx, song.ID, mediaFormat); skip {
|
||||||
return nil, SkipError{Path: path}
|
return nil, SkipError{Path: path}
|
||||||
|
|||||||
@@ -14,16 +14,23 @@ var validQualities = map[string]bool{
|
|||||||
type Options struct {
|
type Options struct {
|
||||||
Quality string
|
Quality string
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
BPM bool
|
|
||||||
Limit int
|
Limit int
|
||||||
|
BPM bool
|
||||||
|
Strict bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *Options) Validate() error {
|
func (o *Options) Validate() error {
|
||||||
if !validQualities[o.Quality] {
|
if !validQualities[o.Quality] {
|
||||||
return fmt.Errorf("invalid quality option: %s", o.Quality)
|
return fmt.Errorf("invalid quality option: %s", o.Quality)
|
||||||
}
|
}
|
||||||
if o.Limit < 0 {
|
if o.Timeout <= 0 {
|
||||||
return fmt.Errorf("limit must be a non-negative integer")
|
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
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user