tdl/cmd/root.go

265 lines
8.3 KiB
Go
Raw Normal View History

2022-09-01 15:16:59 +08:00
package cmd
import (
"context"
2023-12-04 10:26:39 +08:00
"fmt"
"net/http"
"os"
2023-08-20 21:00:06 +08:00
"path/filepath"
"strings"
"time"
2023-12-04 10:26:39 +08:00
"github.com/go-faster/errors"
"github.com/gotd/td/telegram"
2024-06-23 17:27:16 +08:00
"github.com/ivanpirog/coloredcobra"
2022-09-01 15:16:59 +08:00
"github.com/spf13/cobra"
"github.com/spf13/viper"
2023-12-04 10:26:39 +08:00
"go.uber.org/multierr"
2023-01-30 19:56:08 +08:00
"go.uber.org/zap"
"golang.org/x/net/proxy"
2023-08-20 21:00:06 +08:00
"github.com/iyear/tdl/core/logctx"
tclientcore "github.com/iyear/tdl/core/tclient"
"github.com/iyear/tdl/core/util/fsutil"
"github.com/iyear/tdl/core/util/logutil"
"github.com/iyear/tdl/core/util/netutil"
2023-08-20 21:00:06 +08:00
"github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/extensions"
2023-08-20 21:00:06 +08:00
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/pkg/tclient"
)
var (
defaultBoltPath = filepath.Join(consts.DataDir, "data")
DefaultLegacyStorage = map[string]string{
kv.DriverTypeKey: kv.DriverLegacy.String(),
"path": filepath.Join(consts.DataDir, "data.kv"),
}
DefaultBoltStorage = map[string]string{
kv.DriverTypeKey: kv.DriverBolt.String(),
"path": defaultBoltPath,
}
2022-09-01 15:16:59 +08:00
)
2024-06-23 17:10:48 +08:00
// command groups
var (
groupAccount = &cobra.Group{
ID: "account",
Title: "Account related",
}
groupTools = &cobra.Group{
ID: "tools",
Title: "Tools",
}
groupExtensions = &cobra.Group{
ID: "extensions",
Title: "Extensions",
}
2024-06-23 17:10:48 +08:00
)
2023-03-29 18:20:23 +08:00
func New() *cobra.Command {
// allow PersistentPreRun to be called for every command
cobra.EnableTraverseRunHooks = true
em := extensions.NewManager(consts.ExtensionsPath)
2023-03-29 18:20:23 +08:00
cmd := &cobra.Command{
2023-10-02 19:33:19 +08:00
Use: "tdl",
Short: "Telegram Downloader, but more than a downloader",
SilenceErrors: true,
SilenceUsage: true,
2023-12-04 10:26:39 +08:00
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
2023-03-29 18:20:23 +08:00
// init logger
debug, level := viper.GetBool(consts.FlagDebug), zap.InfoLevel
if debug {
level = zap.DebugLevel
}
cmd.SetContext(logctx.With(cmd.Context(),
logutil.New(level, filepath.Join(consts.LogPath, "latest.log"))))
2022-10-19 10:42:03 +08:00
2023-03-29 18:20:23 +08:00
ns := viper.GetString(consts.FlagNamespace)
if ns != "" {
logctx.From(cmd.Context()).Info("Namespace",
2023-03-29 18:20:23 +08:00
zap.String("namespace", ns))
}
2023-12-04 10:26:39 +08:00
// v0.14.0: default storage changed from legacy to bolt, so we need to auto migrate to keep compatibility
if !cmd.Flags().Lookup(consts.FlagStorage).Changed && !fsutil.PathExists(defaultBoltPath) {
if err := migrateLegacyToBolt(); err != nil {
return errors.Wrap(err, "migrate legacy to bolt")
}
}
storage, err := kv.NewWithMap(viper.GetStringMapString(consts.FlagStorage))
2023-12-04 10:26:39 +08:00
if err != nil {
return errors.Wrap(err, "create kv storage")
}
cmd.SetContext(kv.With(cmd.Context(), storage))
// extension manager client proxy
var dialer proxy.ContextDialer = proxy.Direct
if p := viper.GetString(consts.FlagProxy); p != "" {
if t, err := netutil.NewProxy(p); err == nil {
dialer = t
}
}
em.SetClient(&http.Client{Transport: &http.Transport{
DialContext: dialer.DialContext,
}})
2023-12-04 10:26:39 +08:00
return nil
2023-03-29 18:20:23 +08:00
},
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
2023-12-04 10:26:39 +08:00
return multierr.Combine(
kv.From(cmd.Context()).Close(),
logctx.From(cmd.Context()).Sync(),
2023-12-04 10:26:39 +08:00
)
2023-03-29 18:20:23 +08:00
},
}
2024-06-23 17:27:16 +08:00
coloredcobra.Init(&coloredcobra.Config{
RootCmd: cmd,
Headings: coloredcobra.HiCyan + coloredcobra.Bold + coloredcobra.Underline,
Commands: coloredcobra.HiGreen + coloredcobra.Bold,
CmdShortDescr: coloredcobra.None,
ExecName: coloredcobra.Bold,
Flags: coloredcobra.Bold + coloredcobra.Yellow,
FlagsDataType: coloredcobra.Blue,
FlagsDescr: coloredcobra.None,
Aliases: coloredcobra.None,
Example: coloredcobra.None,
NoExtraNewlines: true,
NoBottomNewline: true,
})
cmd.AddGroup(groupAccount, groupTools, groupExtensions)
2024-06-23 17:10:48 +08:00
2023-11-16 23:04:07 +08:00
cmd.AddCommand(NewVersion(), NewLogin(), NewDownload(), NewForward(),
NewChat(), NewUpload(), NewBackup(), NewRecover(), NewMigrate(),
NewGen(), NewExtension(em))
// append extension command to root
exts, _ := em.List(context.Background(), false)
for _, e := range exts {
cmd.AddCommand(NewExtensionCmd(em, e, os.Stdin, os.Stdout, os.Stderr))
}
2022-09-01 15:16:59 +08:00
cmd.PersistentFlags().StringToString(consts.FlagStorage,
DefaultBoltStorage,
fmt.Sprintf("storage options, format: type=driver,key1=value1,key2=value2. Available drivers: [%s]",
strings.Join(kv.DriverNames(), ",")))
2023-12-04 10:26:39 +08:00
2023-12-04 15:58:53 +08:00
cmd.PersistentFlags().String(consts.FlagProxy, "", "proxy address, format: protocol://username:password@host:port")
2023-12-17 20:11:21 +08:00
cmd.PersistentFlags().StringP(consts.FlagNamespace, "n", "default", "namespace for Telegram session")
2022-09-19 20:48:16 +08:00
cmd.PersistentFlags().Bool(consts.FlagDebug, false, "enable debug mode")
2022-09-01 15:16:59 +08:00
cmd.PersistentFlags().IntP(consts.FlagPartSize, "s", 512*1024, "part size for transfer")
cmd.PersistentFlags().IntP(consts.FlagThreads, "t", 4, "max threads for transfer one item")
cmd.PersistentFlags().IntP(consts.FlagLimit, "l", 2, "max number of concurrent tasks")
2023-11-25 11:53:51 +08:00
cmd.PersistentFlags().Int(consts.FlagPoolSize, 8, "specify the size of the DC pool, zero means infinity")
cmd.PersistentFlags().Duration(consts.FlagDelay, 0, "delay between each task, zero means no delay")
2022-09-21 20:52:43 +08:00
cmd.PersistentFlags().String(consts.FlagNTP, "", "ntp server host, if not set, use system time")
cmd.PersistentFlags().Duration(consts.FlagReconnectTimeout, 5*time.Minute, "Telegram client reconnection backoff timeout, infinite if set to 0") // #158
2022-09-21 20:52:43 +08:00
// completion
_ = cmd.RegisterFlagCompletionFunc(consts.FlagNamespace, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
2023-12-04 10:26:39 +08:00
engine := kv.From(cmd.Context())
ns, err := engine.Namespaces()
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return ns, cobra.ShellCompDirectiveNoFileComp
})
_ = viper.BindPFlags(cmd.PersistentFlags())
2022-09-19 20:48:16 +08:00
viper.SetEnvPrefix("tdl")
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
2022-09-19 20:48:16 +08:00
viper.AutomaticEnv()
2022-09-21 20:52:43 +08:00
// extension command format: <global-flags> <extension-name> <extension-flags>,
// which means parse args layer by layer. But common command flags are flat.
// To keep compatibility, we only set TraverseChildren to true for extension
// command instead of other commands.
foundCmd, _, err := cmd.Find(os.Args[1:])
if err == nil && foundCmd.GroupID == groupExtensions.ID {
cmd.TraverseChildren = true // allow global config to be parsed before extension command is executed
}
2023-03-29 18:20:23 +08:00
return cmd
}
type completeFunc func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective)
func completeExtFiles(ext ...string) completeFunc {
2023-04-06 17:39:59 +08:00
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
files := make([]string, 0)
for _, e := range ext {
f, err := filepath.Glob(toComplete + "*." + e)
if err != nil {
return nil, cobra.ShellCompDirectiveDefault
}
files = append(files, f...)
}
2023-04-06 18:12:48 +08:00
return files, cobra.ShellCompDirectiveFilterDirs
2023-04-06 17:39:59 +08:00
}
}
func tOptions(ctx context.Context) (tclient.Options, error) {
// init tclient kv
kvd, err := kv.From(ctx).Open(viper.GetString(consts.FlagNamespace))
if err != nil {
return tclient.Options{}, errors.Wrap(err, "open kv storage")
}
o := tclient.Options{
KV: kvd,
Proxy: viper.GetString(consts.FlagProxy),
NTP: viper.GetString(consts.FlagNTP),
ReconnectTimeout: viper.GetDuration(consts.FlagReconnectTimeout),
UpdateHandler: nil,
}
return o, nil
}
func tRun(ctx context.Context, f func(ctx context.Context, c *telegram.Client, kvd kv.KV) error, middlewares ...telegram.Middleware) error {
o, err := tOptions(ctx)
if err != nil {
return errors.Wrap(err, "build telegram options")
}
2023-12-11 10:57:35 +08:00
client, err := tclient.New(ctx, o, false, middlewares...)
if err != nil {
return errors.Wrap(err, "create client")
}
return tclientcore.RunWithAuth(ctx, client, func(ctx context.Context) error {
return f(ctx, client, o.KV)
})
}
func migrateLegacyToBolt() (rerr error) {
legacy, err := kv.NewWithMap(DefaultLegacyStorage)
if err != nil {
return errors.Wrap(err, "create legacy kv storage")
}
defer multierr.AppendInvoke(&rerr, multierr.Close(legacy))
bolt, err := kv.NewWithMap(DefaultBoltStorage)
if err != nil {
return errors.Wrap(err, "create bolt kv storage")
}
defer multierr.AppendInvoke(&rerr, multierr.Close(bolt))
meta, err := legacy.MigrateTo()
if err != nil {
return errors.Wrap(err, "migrate legacy to bolt")
}
return bolt.MigrateFrom(meta)
}