feat(dl): continue and restart flags

This commit is contained in:
iyear 2023-02-14 22:35:37 +08:00
parent d25f79d4d1
commit 32129dbbb7
3 changed files with 33 additions and 8 deletions

View File

@ -34,6 +34,9 @@ type Options struct {
Exclude []string
Desc bool
PoolSize int64
// resume opts
Continue, Restart bool
}
type parser struct {
@ -85,10 +88,15 @@ func Run(ctx context.Context, opts *Options) error {
return err
}
// resume download and ask user to continue
if err = resume(ctx, kvd, iter); err != nil {
return err
if !opts.Restart {
// resume download and ask user to continue
if err = resume(ctx, kvd, iter, !opts.Continue); err != nil {
return err
}
} else {
color.Yellow("Restart download by 'restart' flag")
}
defer func() { // save progress
if rerr != nil { // download is interrupted
multierr.AppendInto(&rerr, saveProgress(ctx, kvd, iter))
@ -132,7 +140,7 @@ func collectDialogs(ctx context.Context, pool dcpool.Pool, kvd kv.KV, parsers []
return dialogs, nil
}
func resume(ctx context.Context, kvd kv.KV, iter *dliter.Iter) error {
func resume(ctx context.Context, kvd kv.KV, iter *dliter.Iter, ask bool) error {
logger.From(ctx).Debug("Check resume key",
zap.String("fingerprint", iter.Fingerprint()))
@ -155,10 +163,16 @@ func resume(ctx context.Context, kvd kv.KV, iter *dliter.Iter) error {
}
confirm := false
if err = survey.AskOne(&survey.Confirm{
Message: fmt.Sprintf("Found unfinished download, continue from '%d/%d'?", len(finished), iter.Total(ctx)),
}, &confirm); err != nil {
return err
resumeStr := fmt.Sprintf("Found unfinished download, continue from '%d/%d'", len(finished), iter.Total(ctx))
if ask {
if err = survey.AskOne(&survey.Confirm{
Message: color.YellowString(resumeStr + "?"),
}, &confirm); err != nil {
return err
}
} else {
color.Yellow(resumeStr)
confirm = true
}
logger.From(ctx).Debug("Resume download",

View File

@ -22,6 +22,11 @@ var Cmd = &cobra.Command{
return errors.New("only one of `include` and `exclude` can be specified")
}
// only one of continue and restart can be specified
if opts.Continue && opts.Restart {
return errors.New("only one of `continue` and `restart` can be specified, or none of them")
}
// mkdir if not exists
if err := os.MkdirAll(opts.Dir, os.ModePerm); err != nil {
return err
@ -48,5 +53,9 @@ func init() {
Cmd.Flags().Int64Var(&opts.PoolSize, consts.FlagDlPool, 3, "specify the size of the DC pool")
Cmd.Flags().BoolVar(&opts.Desc, consts.FlagDlDesc, false, "download files from the newest to the oldest ones (may affect resume download)")
// resume flags, if both false then ask user
Cmd.Flags().BoolVar(&opts.Continue, consts.FlagDlContinue, false, "continue the last download directly")
Cmd.Flags().BoolVar(&opts.Restart, consts.FlagDlRestart, false, "restart the last download directly")
_ = viper.BindPFlag(consts.FlagDlTemplate, Cmd.Flags().Lookup(consts.FlagDlTemplate))
}

View File

@ -22,4 +22,6 @@ const (
FlagDlRewriteExt = "rewrite-ext"
FlagDlSkipSame = "skip-same"
FlagDlPool = "pool"
FlagDlContinue = "continue"
FlagDlRestart = "restart"
)