feat(cli): add timeout flag
This commit is contained in:
+3
-1
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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,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 {
|
||||
|
||||
Reference in New Issue
Block a user