-
Notifications
You must be signed in to change notification settings - Fork 100
/
command.go
570 lines (501 loc) · 14.8 KB
/
command.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
package lib
import (
"fmt"
"net/http"
"strconv"
"strings"
"time"
oss "github.com/aliyun/aliyun-oss-go-sdk/oss"
)
// group spec text of all commands
const (
GroupTypeNormalCommand string = "\nCommands:\n"
GroupTypeAdditionalCommand string = "\nAdditional Commands:\n"
GroupTypeDeprecatedCommand string = "\nDeprecated Commands:\n"
)
// CommandGroups is the array of all group types
var CommandGroups = []string{
GroupTypeNormalCommand,
GroupTypeAdditionalCommand,
GroupTypeDeprecatedCommand,
}
// SpecText is the spec text of a command
type SpecText struct {
synopsisText string
paramText string
syntaxText string
detailHelpText string
sampleText string
}
// Command contains all elements of a command, it's the base class of all commands
type Command struct {
name string
nameAlias []string
minArgc int
maxArgc int
specChinese SpecText
specEnglish SpecText
validOptionNames []string
group string
args []string
options OptionMapType
configOptions OptionMapType
}
// Commander is the interface of all commands
type Commander interface {
RunCommand() error
Init(args []string, options OptionMapType) error
}
// RewriteLoadConfiger is the interface for those commands, which do not need to load config, or have other action
type RewriteLoadConfiger interface {
rewriteLoadConfig(string) error
}
// RewriteAssembleOptioner is the interface for those commands, which do not need to assemble options
type RewriteAssembleOptioner interface {
rewriteAssembleOptions()
}
// Init is the common functions for all commands, they use Init to initialize itself
func (cmd *Command) Init(args []string, options OptionMapType, cmder interface{}) error {
cmd.args = args
cmd.options = options
cmd.configOptions = OptionMapType{}
if err := cmd.checkArgs(); err != nil {
return err
}
val, _ := GetString(OptionConfigFile, cmd.options)
if err := cmd.loadConfig(val, cmder); err != nil {
return err
}
if err := cmd.checkOptions(); err != nil {
return err
}
cmd.assembleOptions(cmder)
return nil
}
func (cmd *Command) checkArgs() error {
str := ""
if cmd.minArgc > 1 {
str = "s"
}
if len(cmd.args) < cmd.minArgc {
msg := fmt.Sprintf("the command needs at least %d argument%s", cmd.minArgc, str)
return CommandError{cmd.name, msg}
}
str = ""
if cmd.minArgc > 1 {
str = "s"
}
if len(cmd.args) > cmd.maxArgc {
msg := fmt.Sprintf("the command needs at most %d argument%s", cmd.maxArgc, str)
return CommandError{cmd.name, msg}
}
return nil
}
func (cmd *Command) loadConfig(configFile string, cmder interface{}) error {
if cmdder, ok := cmder.(RewriteLoadConfiger); ok {
return cmdder.rewriteLoadConfig(configFile)
}
var err error
if cmd.configOptions, err = LoadConfig(configFile); err != nil && cmd.needConfigFile() {
return err
}
return nil
}
func (cmd *Command) needConfigFile() bool {
for _, name := range []string{OptionEndpoint, OptionAccessKeyID, OptionAccessKeySecret, OptionSTSToken} {
val, _ := GetString(name, cmd.options)
if val != "" {
return false
}
}
return true
}
func (cmd *Command) checkOptions() error {
for name := range cmd.options {
msg := fmt.Sprintf("the command does not support option: \"%s\"", name)
switch OptionMap[name].optionType {
case OptionTypeFlagTrue:
if val, _ := GetBool(name, cmd.options); val {
if FindPos(name, cmd.validOptionNames) == -1 {
return CommandError{cmd.name, msg}
}
}
default:
if val, _ := GetString(name, cmd.options); val != "" {
if FindPos(name, cmd.validOptionNames) == -1 {
return CommandError{cmd.name, msg}
}
}
}
}
return nil
}
func (cmd *Command) assembleOptions(cmder interface{}) {
if cmdder, ok := cmder.(RewriteAssembleOptioner); ok {
cmdder.rewriteAssembleOptions()
return
}
for name, option := range cmd.configOptions {
if _, ok := cmd.options[name]; ok {
if OptionMap[name].optionType != OptionTypeFlagTrue {
if val, _ := GetString(name, cmd.options); val == "" {
opval := option.(string)
cmd.options[name] = &opval
delete(cmd.configOptions, name)
} else if name == OptionEndpoint {
delete(cmd.configOptions, BucketCnameSection)
delete(cmd.configOptions, BucketEndpointSection)
}
}
}
}
for name := range cmd.options {
if OptionMap[name].def != "" {
switch OptionMap[name].optionType {
case OptionTypeInt64:
if val, _ := GetString(name, cmd.options); val == "" {
def, _ := strconv.ParseInt(OptionMap[name].def, 10, 64)
cmd.options[name] = &def
}
case OptionTypeAlternative:
fallthrough
case OptionTypeString:
if val, _ := GetString(name, cmd.options); val == "" {
def := OptionMap[name].def
cmd.options[name] = &def
}
}
}
}
}
// FormatHelper is the interface for all commands to format spec information
type FormatHelper interface {
formatHelpForWhole() string
formatIndependHelp() string
}
func (cmd *Command) formatHelpForWhole() string {
formatStr := " %-" + strconv.Itoa(MaxCommandNameLen) + "s%s\n%s%s%s\n"
spec := cmd.getSpecText()
return fmt.Sprintf(formatStr, cmd.name, spec.paramText, FormatTAB, FormatTAB, spec.synopsisText)
}
func (cmd *Command) getSpecText() SpecText {
val, _ := GetString(OptionLanguage, helpCommand.command.options)
switch strings.ToLower(val) {
case LEnglishLanguage:
return cmd.specEnglish
default:
return cmd.specChinese
}
}
func (cmd *Command) formatIndependHelp() string {
spec := cmd.getSpecText()
text := fmt.Sprintf("SYNOPSIS\n\n%s%s\n", FormatTAB, strings.TrimSpace(spec.synopsisText))
if spec.syntaxText != "" {
text += fmt.Sprintf("\nSYNTAX\n\n%s%s\n", FormatTAB, strings.TrimSpace(spec.syntaxText))
}
if spec.detailHelpText != "" {
text += fmt.Sprintf("\nDETAIL DESCRIPTION\n\n%s%s\n", FormatTAB, strings.TrimSpace(spec.detailHelpText))
}
if spec.sampleText != "" {
text += fmt.Sprintf("\nSAMPLE\n\n%s%s\n", FormatTAB, strings.TrimSpace(spec.sampleText))
}
if len(cmd.validOptionNames) != 0 {
text += fmt.Sprintf("\nOPTIONS\n\n%s\n", cmd.formatOptionsHelp(cmd.validOptionNames))
}
return text
}
func (cmd *Command) formatOptionsHelp(validOptionNames []string) string {
text := ""
for _, optionName := range validOptionNames {
if option, ok := OptionMap[optionName]; ok {
text += cmd.formatOption(option)
}
}
return text
}
func (cmd *Command) formatOption(option Option) string {
text := FormatTAB
if option.name != "" {
text += option.name
if option.def != "" {
text += " " + option.def
}
}
if option.name != "" && option.nameAlias != "" {
text += ", "
}
if option.nameAlias != "" {
text += option.nameAlias
if option.def != "" {
text += fmt.Sprintf("=%s", option.def)
}
}
val, _ := GetString(OptionLanguage, helpCommand.command.options)
val = strings.ToLower(val)
opHelp := option.getHelp(val)
if opHelp != "" {
text += fmt.Sprintf("\n%s%s%s\n\n", FormatTAB, FormatTAB, opHelp)
}
return text
}
// OSS common function
// get oss client according to bucket(if bucket not empty)
func (cmd *Command) ossClient(bucket string) (*oss.Client, error) {
endpoint, isCname := cmd.getEndpoint(bucket)
accessKeyID, _ := GetString(OptionAccessKeyID, cmd.options)
accessKeySecret, _ := GetString(OptionAccessKeySecret, cmd.options)
stsToken, _ := GetString(OptionSTSToken, cmd.options)
disableCRC64, _ := GetBool(OptionDisableCRC64, cmd.options)
if err := cmd.checkCredentials(endpoint, accessKeyID, accessKeySecret); err != nil {
return nil, err
}
options := []oss.ClientOption{oss.UseCname(isCname), oss.SecurityToken(stsToken), oss.UserAgent(getUserAgent()), oss.Timeout(120, 1200)}
if disableCRC64 {
options = append(options, oss.EnableCRC(false))
} else {
options = append(options, oss.EnableCRC(true))
}
if logLevel > oss.LogOff {
options = append(options, oss.SetLogLevel(logLevel))
options = append(options, oss.SetLogger(utilLogger))
}
client, err := oss.New(endpoint, accessKeyID, accessKeySecret, options...)
if err != nil {
return nil, err
}
maxUpSpeed, errUp := GetInt(OptionMaxUpSpeed, cmd.options)
if errUp == nil {
if maxUpSpeed >= 0 {
errUp = client.LimitUploadSpeed(int(maxUpSpeed))
if errUp != nil {
return nil, errUp
} else {
LogInfo("set maxupspeed success,value is %d(KB/s)\n", maxUpSpeed)
}
} else {
return nil, fmt.Errorf("invalid value,maxupspeed %d less than 0", maxUpSpeed)
}
}
return client, nil
}
func (cmd *Command) checkCredentials(endpoint, accessKeyID, accessKeySecret string) error {
if strings.TrimSpace(endpoint) == "" {
return fmt.Errorf("invalid endpoint, endpoint is empty, please check your config")
}
if strings.TrimSpace(accessKeyID) == "" {
return fmt.Errorf("invalid accessKeyID, accessKeyID is empty, please check your config")
}
if strings.TrimSpace(accessKeySecret) == "" {
return fmt.Errorf("invalid accessKeySecret, accessKeySecret is empty, please check your config")
}
return nil
}
func (cmd *Command) getEndpoint(bucket string) (string, bool) {
if cnameMap, ok := cmd.configOptions[BucketCnameSection]; ok {
if endpoint, ok := cnameMap.(map[string]string)[bucket]; ok {
return endpoint, true
}
}
if eMap, ok := cmd.configOptions[BucketEndpointSection]; ok {
if endpoint, ok := eMap.(map[string]string)[bucket]; ok {
return endpoint, false
}
}
endpoint, _ := GetString(OptionEndpoint, cmd.options)
return endpoint, false
}
// get oss operable bucket
func (cmd *Command) ossBucket(bucketName string) (*oss.Bucket, error) {
client, err := cmd.ossClient(bucketName)
if err != nil {
return nil, err
}
bucket, err := client.Bucket(bucketName)
if err != nil {
return nil, err
}
return bucket, nil
}
func (cmd *Command) ossListObjectsRetry(bucket *oss.Bucket, options ...oss.Option) (oss.ListObjectsResult, error) {
retryTimes, _ := GetInt(OptionRetryTimes, cmd.options)
for i := 1; ; i++ {
lor, err := bucket.ListObjects(options...)
if err == nil {
return lor, err
}
if int64(i) >= retryTimes {
return lor, BucketError{err, bucket.BucketName}
}
}
}
func (cmd *Command) ossListMultipartUploadsRetry(bucket *oss.Bucket, options ...oss.Option) (oss.ListMultipartUploadResult, error) {
retryTimes, _ := GetInt(OptionRetryTimes, cmd.options)
for i := 1; ; i++ {
lmr, err := bucket.ListMultipartUploads(options...)
if err == nil {
return lmr, err
}
if int64(i) >= retryTimes {
return lmr, BucketError{err, bucket.BucketName}
}
}
}
func (cmd *Command) ossGetObjectStatRetry(bucket *oss.Bucket, object string, options ...oss.Option) (http.Header, error) {
retryTimes, _ := GetInt(OptionRetryTimes, cmd.options)
for i := 1; ; i++ {
props, err := bucket.GetObjectDetailedMeta(object, options...)
if err == nil {
return props, err
}
// http 4XX 、5XX error no need to retry
// only network error need to retry
serviceError, noNeedRetry := err.(oss.ServiceError)
if int64(i) >= retryTimes || (noNeedRetry && serviceError.StatusCode < 500) {
return props, ObjectError{err, bucket.BucketName, object}
}
// wait 1 second
time.Sleep(time.Duration(1) * time.Second)
}
}
func (cmd *Command) ossGetObjectMetaRetry(bucket *oss.Bucket, object string, options ...oss.Option) (http.Header, error) {
retryTimes, _ := GetInt(OptionRetryTimes, cmd.options)
for i := 1; ; i++ {
props, err := bucket.GetObjectMeta(object, options...)
if err == nil {
return props, err
}
// http 4XX 、5XX error no need to retry
// only network error need to retry
serviceError, noNeedRetry := err.(oss.ServiceError)
if int64(i) >= retryTimes || (noNeedRetry && serviceError.StatusCode < 500) {
return props, ObjectError{err, bucket.BucketName, object}
}
// wait 1 second
time.Sleep(time.Duration(1) * time.Second)
}
}
func (cmd *Command) objectStatistic(bucket *oss.Bucket, cloudURL CloudURL, monitor Monitorer, filters []filterOptionType) {
if monitor == nil {
return
}
pre := oss.Prefix(cloudURL.object)
marker := oss.Marker("")
for {
lor, err := cmd.ossListObjectsRetry(bucket, marker, pre)
if err != nil {
monitor.setScanError(err)
return
}
for _, object := range lor.Objects {
if doesSingleObjectMatchPatterns(object.Key, filters) {
monitor.updateScanNum(1)
}
}
marker = oss.Marker(lor.NextMarker)
if !lor.IsTruncated {
break
}
}
monitor.setScanEnd()
}
func (cmd *Command) objectProducer(bucket *oss.Bucket, cloudURL CloudURL, chObjects chan<- string, chError chan<- error, filters []filterOptionType) {
pre := oss.Prefix(cloudURL.object)
marker := oss.Marker("")
for {
lor, err := cmd.ossListObjectsRetry(bucket, marker, pre)
if err != nil {
chError <- err
break
}
for _, object := range lor.Objects {
if doesSingleObjectMatchPatterns(object.Key, filters) {
chObjects <- object.Key
}
}
pre = oss.Prefix(lor.Prefix)
marker = oss.Marker(lor.NextMarker)
if !lor.IsTruncated {
break
}
}
defer close(chObjects)
chError <- nil
}
func (cmd *Command) updateMonitor(err error, monitor *Monitor) {
if monitor == nil {
return
}
if err == nil {
monitor.updateOKNum(1)
} else {
monitor.updateErrNum(1)
}
fmt.Printf(monitor.progressBar(false, normalExit))
}
func (cmd *Command) report(msg string, err error, option *batchOptionType) {
if cmd.filterError(err, option) {
option.reporter.ReportError(fmt.Sprintf("%s error, info: %s", msg, err.Error()))
option.reporter.Prompt(err)
}
}
func (cmd *Command) filterError(err error, option *batchOptionType) bool {
if err == nil {
return false
}
err = err.(ObjectError).err
switch err.(type) {
case oss.ServiceError:
code := err.(oss.ServiceError).Code
if code == "NoSuchBucket" || code == "InvalidAccessKeyId" || code == "SignatureDoesNotMatch" || code == "AccessDenied" || code == "RequestTimeTooSkewed" || code == "InvalidBucketName" {
option.ctnu = false
return false
}
}
return true
}
func (cmd *Command) getOSSOptions(hopMap map[string]interface{}, headers map[string]string) ([]oss.Option, error) {
options := []oss.Option{}
for name, value := range headers {
if strings.HasPrefix(strings.ToLower(name), strings.ToLower(oss.HTTPHeaderOssMetaPrefix)) {
options = append(options, oss.Meta(name[len(oss.HTTPHeaderOssMetaPrefix):], value))
} else {
option, err := getOSSOption(hopMap, name, value)
if err != nil {
return nil, err
}
options = append(options, option)
}
}
return options, nil
}
// GetAllCommands returns all commands list
func GetAllCommands() []interface{} {
return []interface{}{
&helpCommand,
&configCommand,
&makeBucketCommand,
&listCommand,
&removeCommand,
&statCommand,
&setACLCommand,
&setMetaCommand,
©Command,
&restoreCommand,
&createSymlinkCommand,
&readSymlinkCommand,
&signURLCommand,
&hashCommand,
&updateCommand,
&probeCommand,
&mkdirCommand,
&corsCommand,
&bucketLogCommand,
&bucketRefererCommand,
&listPartCommand,
&allPartSizeCommand,
&appendFileCommand,
&catCommand,
&bucketTagCommand,
&bucketEncryptionCommand,
}
}