-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroot.go
93 lines (76 loc) · 3.15 KB
/
root.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package cmd
import (
"path/filepath"
"strings"
"time"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"
"github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/pkg/logger"
)
func New() *cobra.Command {
cmd := &cobra.Command{
Use: "tdl",
Short: "Telegram Downloader, but more than a downloader",
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
}
cmd.SetContext(logger.With(cmd.Context(),
logger.New(level, filepath.Join(consts.LogPath, "latest.log"))))
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(), NewGen())
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")
cmd.PersistentFlags().Bool(consts.FlagDebug, false, "enable debug mode")
cmd.PersistentFlags().IntP(consts.FlagPartSize, "s", 512*1024, "part size for transfer, max is 512*1024")
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")
cmd.PersistentFlags().Int(consts.FlagPoolSize, 3, "specify the size of the DC pool")
cmd.PersistentFlags().String(consts.FlagNTP, "", "ntp server host, if not set, use system time")
cmd.PersistentFlags().Duration(consts.FlagReconnectTimeout, 2*time.Minute, "Telegram client reconnection backoff timeout, infinite if set to 0") // #158
cmd.PersistentFlags().String(consts.FlagTest, "", "use test Telegram client, only for developer")
// 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
})
_ = viper.BindPFlags(cmd.PersistentFlags())
viper.SetEnvPrefix("tdl")
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
viper.AutomaticEnv()
return cmd
}
type completeFunc func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective)
func completeExtFiles(ext ...string) completeFunc {
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...)
}
return files, cobra.ShellCompDirectiveFilterDirs
}
}