2022-09-01 15:16:59 +08:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2023-08-20 21:00:06 +08:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-09-21 20:52:43 +08:00
|
|
|
"github.com/fatih/color"
|
2022-09-01 15:16:59 +08:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/cobra/doc"
|
2022-09-19 19:35:38 +08:00
|
|
|
"github.com/spf13/viper"
|
2023-01-30 19:56:08 +08:00
|
|
|
"go.uber.org/zap"
|
2023-08-20 21:00:06 +08:00
|
|
|
|
|
|
|
"github.com/iyear/tdl/pkg/consts"
|
|
|
|
"github.com/iyear/tdl/pkg/kv"
|
|
|
|
"github.com/iyear/tdl/pkg/logger"
|
|
|
|
"github.com/iyear/tdl/pkg/utils"
|
2022-09-01 15:16:59 +08:00
|
|
|
)
|
|
|
|
|
2023-03-29 18:20:23 +08:00
|
|
|
func New() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "tdl",
|
|
|
|
Short: "Telegram Downloader, but more than a downloader",
|
|
|
|
DisableAutoGenTag: true,
|
|
|
|
SilenceErrors: true,
|
|
|
|
SilenceUsage: true,
|
|
|
|
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
|
|
|
// init logger
|
|
|
|
debug, level := viper.GetBool(consts.FlagDebug), zap.InfoLevel
|
|
|
|
if debug {
|
|
|
|
level = zap.DebugLevel
|
|
|
|
}
|
2023-03-29 18:24:41 +08:00
|
|
|
cmd.SetContext(logger.With(cmd.Context(),
|
|
|
|
logger.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 != "" {
|
|
|
|
logger.From(cmd.Context()).Info("Namespace",
|
|
|
|
zap.String("namespace", ns))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return logger.From(cmd.Context()).Sync()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.AddCommand(NewVersion(), NewLogin(), NewDownload(),
|
|
|
|
NewChat(), NewUpload(), NewBackup(), NewRecover())
|
2022-09-01 15:16:59 +08:00
|
|
|
|
2022-09-19 18:39:56 +08:00
|
|
|
cmd.PersistentFlags().String(consts.FlagProxy, "", "proxy address, only socks5 is supported, format: protocol://username:password@host:port")
|
|
|
|
cmd.PersistentFlags().StringP(consts.FlagNamespace, "n", "", "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
|
|
|
|
2023-04-06 18:39:02 +08:00
|
|
|
cmd.PersistentFlags().IntP(consts.FlagPartSize, "s", 512*1024, "part size for transfer, max is 512*1024")
|
2023-01-31 18:54:57 +08:00
|
|
|
cmd.PersistentFlags().IntP(consts.FlagThreads, "t", 4, "max threads for transfer one item")
|
2022-09-19 19:35:38 +08:00
|
|
|
cmd.PersistentFlags().IntP(consts.FlagLimit, "l", 2, "max number of concurrent tasks")
|
2023-09-09 17:13:15 +08:00
|
|
|
cmd.PersistentFlags().Int(consts.FlagPoolSize, 3, "specify the size of the DC pool")
|
2022-09-19 19:35:38 +08:00
|
|
|
|
2022-09-21 20:52:43 +08:00
|
|
|
cmd.PersistentFlags().String(consts.FlagNTP, "", "ntp server host, if not set, use system time")
|
2023-05-31 11:25:50 +08:00
|
|
|
cmd.PersistentFlags().Duration(consts.FlagReconnectTimeout, 2*time.Minute, "Telegram client reconnection backoff timeout, infinite if set to 0") // #158
|
2022-09-21 20:52:43 +08:00
|
|
|
|
2023-05-26 23:45:00 +08:00
|
|
|
cmd.PersistentFlags().String(consts.FlagTest, "", "use test Telegram client, only for developer")
|
|
|
|
|
2023-04-06 14:43:17 +08:00
|
|
|
// completion
|
|
|
|
_ = cmd.RegisterFlagCompletionFunc(consts.FlagNamespace, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
|
|
ns, err := kv.Namespaces(consts.KVPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, cobra.ShellCompDirectiveNoFileComp
|
|
|
|
}
|
|
|
|
return ns, cobra.ShellCompDirectiveNoFileComp
|
|
|
|
})
|
|
|
|
|
2022-09-19 19:35:38 +08:00
|
|
|
_ = viper.BindPFlags(cmd.PersistentFlags())
|
|
|
|
|
2022-09-19 20:48:16 +08:00
|
|
|
viper.SetEnvPrefix("tdl")
|
2023-05-17 16:57:20 +08:00
|
|
|
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
|
2022-09-19 20:48:16 +08:00
|
|
|
viper.AutomaticEnv()
|
2022-09-21 20:52:43 +08:00
|
|
|
|
2023-03-29 18:20:23 +08:00
|
|
|
generateCommandDocs(cmd)
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateCommandDocs(cmd *cobra.Command) {
|
2022-09-21 20:52:43 +08:00
|
|
|
docs := filepath.Join(consts.DocsPath, "command")
|
|
|
|
if utils.FS.PathExists(docs) {
|
|
|
|
if err := doc.GenMarkdownTree(cmd, docs); err != nil {
|
|
|
|
color.Red("generate cmd docs failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2022-09-01 15:16:59 +08:00
|
|
|
}
|
2023-04-06 17:39:59 +08:00
|
|
|
|
2023-04-06 18:01:35 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|