feat(cli): add timeout flag

This commit is contained in:
Mathis Maquenne
2025-05-01 20:45:27 +02:00
parent 0bae78a1ff
commit 67af80403b
3 changed files with 7 additions and 3 deletions
+3 -1
View File
@@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"time"
"github.com/mathismqn/godeez/internal/downloader"
"github.com/spf13/cobra"
@@ -21,6 +22,7 @@ func init() {
downloadCmd.PersistentFlags().StringVarP(&opts.OutputDir, "output", "o", "", "output directory (default is $HOME/Music/GoDeez)")
downloadCmd.PersistentFlags().StringVarP(&opts.Quality, "quality", "q", "", "download quality [mp3_128, mp3_320, flac, best] (default is best)")
downloadCmd.PersistentFlags().DurationVarP(&opts.Timeout, "timeout", "t", 2*time.Minute, "timeout for each download (e.g. 10s, 1m, 2m30s) (default is 2m)")
downloadCmd.AddCommand(
newDownloadCmd("album"),
@@ -41,7 +43,7 @@ func newDownloadCmd(resourceType string) *cobra.Command {
dl := downloader.New(appCtx, resourceType)
if err := dl.Run(ctx, opts, args); err != nil {
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
if errors.Is(err, context.Canceled) {
return nil
}
+2 -2
View File
@@ -83,7 +83,7 @@ func (c *Client) Run(ctx context.Context, opts Options, ids []string) error {
}
if err := c.downloadSong(ctx, resource, song, opts, outputDir); err != nil {
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
if errors.Is(err, context.Canceled) {
return err
}
@@ -132,7 +132,7 @@ func (c *Client) downloadSong(ctx context.Context, resource deezer.Resource, son
return fmt.Errorf("media stream unavailable: %w", err)
}
dlCtx, cancel := context.WithTimeout(ctx, 2*time.Minute)
dlCtx, cancel := context.WithTimeout(ctx, opts.Timeout)
defer cancel()
if err := c.streamToFile(dlCtx, stream, outputPath, song.ID); err != nil {
+2
View File
@@ -2,6 +2,7 @@ package downloader
import (
"fmt"
"time"
)
var validQualities = map[string]bool{
@@ -14,6 +15,7 @@ var validQualities = map[string]bool{
type Options struct {
OutputDir string
Quality string
Timeout time.Duration
}
func (o *Options) Validate(appDir string) error {