refactor(up): use tRun to start session

This commit is contained in:
iyear 2023-12-11 00:42:32 +08:00
parent dfbb368ed9
commit ef751457d8
2 changed files with 33 additions and 32 deletions

View File

@ -5,6 +5,7 @@ import (
"github.com/fatih/color"
"github.com/go-faster/errors"
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/peers"
"github.com/gotd/td/tg"
"github.com/spf13/viper"
@ -13,6 +14,7 @@ import (
"github.com/iyear/tdl/app/internal/tgc"
"github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/dcpool"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/pkg/prog"
"github.com/iyear/tdl/pkg/storage"
"github.com/iyear/tdl/pkg/uploader"
@ -27,7 +29,7 @@ type Options struct {
Photo bool
}
func Run(ctx context.Context, opts *Options) error {
func Run(ctx context.Context, c *telegram.Client, kvd kv.KV, opts Options) (rerr error) {
files, err := walk(opts.Paths, opts.Excludes)
if err != nil {
return err
@ -35,12 +37,6 @@ func Run(ctx context.Context, opts *Options) error {
color.Blue("Files count: %d", len(files))
c, kvd, err := tgc.NoLogin(ctx)
if err != nil {
return err
}
return tgc.RunWithAuth(ctx, c, func(ctx context.Context) (rerr error) {
middlewares, err := tgc.NewDefaultMiddlewares(ctx)
if err != nil {
return errors.Wrap(err, "create middlewares")
@ -74,7 +70,6 @@ func Run(ctx context.Context, opts *Options) error {
defer prog.Wait(ctx, upProgress)
return up.Upload(ctx, viper.GetInt(consts.FlagLimit))
})
}
func resolveDestPeer(ctx context.Context, manager *peers.Manager, chat string) (peers.Peer, error) {

View File

@ -1,9 +1,13 @@
package cmd
import (
"context"
"github.com/gotd/td/telegram"
"github.com/spf13/cobra"
"github.com/iyear/tdl/app/up"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/pkg/logger"
)
@ -15,7 +19,9 @@ func NewUpload() *cobra.Command {
Aliases: []string{"up"},
Short: "Upload anything to Telegram",
RunE: func(cmd *cobra.Command, args []string) error {
return up.Run(logger.Named(cmd.Context(), "up"), &opts)
return tRun(cmd.Context(), false, func(ctx context.Context, c *telegram.Client, kvd kv.KV) error {
return up.Run(logger.Named(ctx, "up"), c, kvd, opts)
})
},
}