-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroot.go
186 lines (156 loc) · 6.08 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package cmd
import (
"context"
"fmt"
"path/filepath"
"strings"
"time"
"github.com/go-faster/errors"
"github.com/gotd/td/telegram"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/multierr"
"go.uber.org/zap"
"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/pkg/consts"
"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,
}
)
func New() *cobra.Command {
cmd := &cobra.Command{
Use: "tdl",
Short: "Telegram Downloader, but more than a downloader",
SilenceErrors: true,
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// 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"))))
ns := viper.GetString(consts.FlagNamespace)
if ns != "" {
logctx.From(cmd.Context()).Info("Namespace",
zap.String("namespace", ns))
}
// 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))
if err != nil {
return errors.Wrap(err, "create kv storage")
}
cmd.SetContext(kv.With(cmd.Context(), storage))
return nil
},
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
return multierr.Combine(
kv.From(cmd.Context()).Close(),
logctx.From(cmd.Context()).Sync(),
)
},
}
cmd.AddCommand(NewVersion(), NewLogin(), NewDownload(), NewForward(),
NewChat(), NewUpload(), NewBackup(), NewRecover(), NewMigrate(), NewGen())
cmd.PersistentFlags().StringToString(consts.FlagStorage,
DefaultBoltStorage,
fmt.Sprintf("storage options, format: type=driver,key1=value1,key2=value2. Available drivers: [%s]",
strings.Join(kv.DriverNames(), ",")))
cmd.PersistentFlags().String(consts.FlagProxy, "", "proxy address, format: protocol://username:password@host:port")
cmd.PersistentFlags().StringP(consts.FlagNamespace, "n", "default", "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, 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")
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
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) {
engine := kv.From(cmd.Context())
ns, err := engine.Namespaces()
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
}
}
func tRun(ctx context.Context, f func(ctx context.Context, c *telegram.Client, kvd kv.KV) error, middlewares ...telegram.Middleware) error {
// init tclient kv
kvd, err := kv.From(ctx).Open(viper.GetString(consts.FlagNamespace))
if err != nil {
return 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),
Test: viper.GetString(consts.FlagTest) != "",
UpdateHandler: nil,
}
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, kvd)
})
}
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)
}