2022-09-01 15:16:59 +08:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-09-21 20:52:43 +08:00
|
|
|
"github.com/fatih/color"
|
2022-09-01 15:16:59 +08:00
|
|
|
"github.com/iyear/tdl/pkg/consts"
|
2023-04-06 14:43:17 +08:00
|
|
|
"github.com/iyear/tdl/pkg/kv"
|
2022-09-19 20:48:16 +08:00
|
|
|
"github.com/iyear/tdl/pkg/logger"
|
2022-09-04 18:56:11 +08:00
|
|
|
"github.com/iyear/tdl/pkg/utils"
|
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"
|
2022-09-01 15:16:59 +08:00
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
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 != "" {
|
|
|
|
color.Cyan("Namespace: %s", 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
|
|
|
|
2022-09-25 13:37:48 +08:00
|
|
|
// The default parameters are consistent with the official client to reduce the probability of blocking
|
|
|
|
// https://github.com/iyear/tdl/issues/30
|
|
|
|
cmd.PersistentFlags().IntP(consts.FlagPartSize, "s", 128*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")
|
|
|
|
|
2022-09-21 20:52:43 +08:00
|
|
|
cmd.PersistentFlags().String(consts.FlagNTP, "", "ntp server host, if not set, use system time")
|
|
|
|
|
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")
|
|
|
|
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
|
|
|
}
|