forked from Angey40/BaiduPCS-Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
2029 lines (1833 loc) · 54.4 KB
/
main.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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"encoding/hex"
"fmt"
"github.com/iikira/BaiduPCS-Go/baidupcs"
"github.com/iikira/BaiduPCS-Go/internal/pcscommand"
"github.com/iikira/BaiduPCS-Go/internal/pcsconfig"
"github.com/iikira/BaiduPCS-Go/internal/pcsfunctions/pcsdownload"
_ "github.com/iikira/BaiduPCS-Go/internal/pcsinit"
"github.com/iikira/BaiduPCS-Go/internal/pcsupdate"
"github.com/iikira/BaiduPCS-Go/pcsliner"
"github.com/iikira/BaiduPCS-Go/pcsliner/args"
"github.com/iikira/BaiduPCS-Go/pcstable"
"github.com/iikira/BaiduPCS-Go/pcsutil"
"github.com/iikira/BaiduPCS-Go/pcsutil/checksum"
"github.com/iikira/BaiduPCS-Go/pcsutil/converter"
"github.com/iikira/BaiduPCS-Go/pcsutil/escaper"
"github.com/iikira/BaiduPCS-Go/pcsutil/getip"
"github.com/iikira/BaiduPCS-Go/pcsutil/pcstime"
"github.com/iikira/BaiduPCS-Go/pcsverbose"
"github.com/olekukonko/tablewriter"
"github.com/peterh/liner"
"github.com/urfave/cli"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
"unicode"
)
const (
// NameShortDisplayNum 文件名缩略显示长度
NameShortDisplayNum = 16
cryptoDescription = `
可用的方法 <method>:
aes-128-ctr, aes-192-ctr, aes-256-ctr,
aes-128-cfb, aes-192-cfb, aes-256-cfb,
aes-128-ofb, aes-192-ofb, aes-256-ofb.
密钥 <key>:
aes-128 对应key长度为16, aes-192 对应key长度为24, aes-256 对应key长度为32,
如果key长度不符合, 则自动修剪key, 舍弃超出长度的部分, 长度不足的部分用'\0'填充.
GZIP <disable-gzip>:
在文件加密之前, 启用GZIP压缩文件; 文件解密之后启用GZIP解压缩文件, 默认启用,
如果不启用, 则无法检测文件是否解密成功, 解密文件时会保留源文件, 避免解密失败造成文件数据丢失.`
)
var (
// Version 版本号
Version = "v3.6.2-devel"
historyFilePath = filepath.Join(pcsconfig.GetConfigDir(), "pcs_command_history.txt")
reloadFn = func(c *cli.Context) error {
err := pcsconfig.Config.Reload()
if err != nil {
fmt.Printf("重载配置错误: %s\n", err)
}
return nil
}
saveFunc = func(c *cli.Context) error {
err := pcsconfig.Config.Save()
if err != nil {
fmt.Printf("保存配置错误: %s\n", err)
}
return nil
}
isCli bool
)
func init() {
pcsutil.ChWorkDir()
err := pcsconfig.Config.Init()
switch err {
case nil:
case pcsconfig.ErrConfigFileNoPermission, pcsconfig.ErrConfigContentsParseError:
fmt.Fprintf(os.Stderr, "FATAL ERROR: config file error: %s\n", err)
os.Exit(1)
default:
fmt.Printf("WARNING: config init error: %s\n", err)
}
}
func main() {
defer pcsconfig.Config.Close()
app := cli.NewApp()
app.Name = "BaiduPCS-Go"
app.Version = Version
app.Author = "iikira/BaiduPCS-Go: https://github.com/iikira/BaiduPCS-Go"
app.Copyright = "(c) 2016-2020 iikira."
app.Usage = "百度网盘客户端 for " + runtime.GOOS + "/" + runtime.GOARCH
app.Description = `BaiduPCS-Go 使用Go语言编写的百度网盘命令行客户端, 为操作百度网盘, 提供实用功能.
具体功能, 参见 COMMANDS 列表
特色:
网盘内列出文件和目录, 支持通配符匹配路径;
下载网盘内文件, 支持网盘内目录 (文件夹) 下载, 支持多个文件或目录下载, 支持断点续传和高并发高速下载.
---------------------------------------------------
前往 https://github.com/iikira/BaiduPCS-Go 以获取更多帮助信息!
前往 https://github.com/iikira/BaiduPCS-Go/releases 以获取程序更新信息!
---------------------------------------------------
交流反馈:
提交Issue: https://github.com/iikira/BaiduPCS-Go/issues
邮箱: [email protected]`
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "verbose",
Usage: "启用调试",
EnvVar: pcsverbose.EnvVerbose,
Destination: &pcsverbose.IsVerbose,
},
}
app.Action = func(c *cli.Context) {
if c.NArg() != 0 {
fmt.Printf("未找到命令: %s\n运行命令 %s help 获取帮助\n", c.Args().Get(0), app.Name)
return
}
isCli = true
pcsverbose.Verbosef("VERBOSE: 这是一条调试信息\n\n")
var (
line = pcsliner.NewLiner()
err error
)
line.History, err = pcsliner.NewLineHistory(historyFilePath)
if err != nil {
fmt.Printf("警告: 读取历史命令文件错误, %s\n", err)
}
line.ReadHistory()
defer func() {
line.DoWriteHistory()
line.Close()
}()
// tab 自动补全命令
line.State.SetCompleter(func(line string) (s []string) {
var (
lineArgs = args.Parse(line)
numArgs = len(lineArgs)
acceptCompleteFileCommands = []string{
"cd", "cp", "download", "export", "fixmd5", "locate", "ls", "meta", "mkdir", "mv", "rapidupload", "rm", "share", "tree", "upload",
}
closed = strings.LastIndex(line, " ") == len(line)-1
)
for _, cmd := range app.Commands {
for _, name := range cmd.Names() {
if !strings.HasPrefix(name, line) {
continue
}
s = append(s, name+" ")
}
}
switch numArgs {
case 0:
return
case 1:
if !closed {
return
}
}
thisCmd := app.Command(lineArgs[0])
if thisCmd == nil {
return
}
if !pcsutil.ContainsString(acceptCompleteFileCommands, thisCmd.FullName()) {
return
}
var (
activeUser = pcsconfig.Config.ActiveUser()
pcs = pcsconfig.Config.ActiveUserBaiduPCS()
runeFunc = unicode.IsSpace
pcsRuneFunc = func(r rune) bool {
switch r {
case '\'', '"':
return true
}
return unicode.IsSpace(r)
}
targetPath string
)
if !closed {
targetPath = lineArgs[numArgs-1]
escaper.EscapeStringsByRuneFunc(lineArgs[:numArgs-1], runeFunc) // 转义
} else {
escaper.EscapeStringsByRuneFunc(lineArgs, runeFunc)
}
switch {
case targetPath == "." || strings.HasSuffix(targetPath, "/."):
s = append(s, line+"/")
return
case targetPath == ".." || strings.HasSuffix(targetPath, "/.."):
s = append(s, line+"/")
return
}
var (
targetDir string
isAbs = path.IsAbs(targetPath)
isDir = strings.LastIndex(targetPath, "/") == len(targetPath)-1
)
if isAbs {
targetDir = path.Dir(targetPath)
} else {
targetDir = path.Join(activeUser.Workdir, targetPath)
if !isDir {
targetDir = path.Dir(targetDir)
}
}
files, err := pcs.CacheFilesDirectoriesList(targetDir, baidupcs.DefaultOrderOptions)
if err != nil {
return
}
// fmt.Println("-", targetDir, targetPath, "-")
for _, file := range files {
if file == nil {
continue
}
var (
appendLine string
)
// 已经有的情况
if !closed {
if !strings.HasPrefix(file.Path, path.Clean(path.Join(targetDir, path.Base(targetPath)))) {
if path.Base(targetDir) == path.Base(targetPath) {
appendLine = strings.Join(append(lineArgs[:numArgs-1], escaper.EscapeByRuneFunc(path.Join(targetPath, file.Filename), pcsRuneFunc)), " ")
goto handle
}
// fmt.Println(file.Path, targetDir, targetPath)
continue
}
// fmt.Println(path.Clean(path.Join(path.Dir(targetPath), file.Filename)), targetPath, file.Filename)
appendLine = strings.Join(append(lineArgs[:numArgs-1], escaper.EscapeByRuneFunc(path.Clean(path.Join(path.Dir(targetPath), file.Filename)), pcsRuneFunc)), " ")
goto handle
}
// 没有的情况
appendLine = strings.Join(append(lineArgs, escaper.EscapeByRuneFunc(file.Filename, pcsRuneFunc)), " ")
goto handle
handle:
if file.Isdir {
s = append(s, appendLine+"/")
continue
}
s = append(s, appendLine+" ")
continue
}
return
})
fmt.Printf("提示: 方向键上下可切换历史命令.\n")
fmt.Printf("提示: Ctrl + A / E 跳转命令 首 / 尾.\n")
fmt.Printf("提示: 输入 help 获取帮助.\n")
for {
var (
prompt string
activeUser = pcsconfig.Config.ActiveUser()
)
if activeUser.Name != "" {
// 格式: BaiduPCS-Go:<工作目录> <百度ID>$
// 工作目录太长时, 会自动缩略
prompt = app.Name + ":" + converter.ShortDisplay(path.Base(activeUser.Workdir), NameShortDisplayNum) + " " + activeUser.Name + "$ "
} else {
// BaiduPCS-Go >
prompt = app.Name + " > "
}
commandLine, err := line.State.Prompt(prompt)
switch err {
case liner.ErrPromptAborted:
return
case nil:
// continue
default:
fmt.Println(err)
return
}
line.State.AppendHistory(commandLine)
cmdArgs := args.Parse(commandLine)
if len(cmdArgs) == 0 {
continue
}
s := []string{os.Args[0]}
s = append(s, cmdArgs...)
// 恢复原始终端状态
// 防止运行命令时程序被结束, 终端出现异常
line.Pause()
c.App.Run(s)
line.Resume()
}
}
app.Commands = []cli.Command{
{
Name: "run",
Usage: "执行系统命令",
Category: "其他",
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
cli.ShowCommandHelp(c, c.Command.Name)
return nil
}
cmd := exec.Command(c.Args().First(), c.Args().Tail()...)
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fmt.Println(err)
}
return nil
},
},
{
Name: "env",
Usage: "显示程序环境变量",
Description: `
BAIDUPCS_GO_CONFIG_DIR: 配置文件路径,
BAIDUPCS_GO_VERBOSE: 是否启用调试.
`,
Category: "其他",
Action: func(c *cli.Context) error {
envStr := "%s=\"%s\"\n"
envVar, ok := os.LookupEnv(pcsverbose.EnvVerbose)
if ok {
fmt.Printf(envStr, pcsverbose.EnvVerbose, envVar)
} else {
fmt.Printf(envStr, pcsverbose.EnvVerbose, "0")
}
envVar, ok = os.LookupEnv(pcsconfig.EnvConfigDir)
if ok {
fmt.Printf(envStr, pcsconfig.EnvConfigDir, envVar)
} else {
fmt.Printf(envStr, pcsconfig.EnvConfigDir, pcsconfig.GetConfigDir())
}
return nil
},
},
{
Name: "update",
Usage: "检测程序更新",
Category: "其他",
Action: func(c *cli.Context) error {
if c.IsSet("y") {
if !c.Bool("y") {
return nil
}
}
pcsupdate.CheckUpdate(app.Version, c.Bool("y"))
return nil
},
Flags: []cli.Flag{
cli.BoolFlag{
Name: "y",
Usage: "确认更新",
},
},
},
{
Name: "login",
Usage: "登录百度账号",
Description: `
示例:
BaiduPCS-Go login
BaiduPCS-Go login -username=liuhua
BaiduPCS-Go login -bduss=123456789
常规登录:
按提示一步一步来即可.
百度BDUSS获取方法:
参考这篇 Wiki: https://github.com/iikira/BaiduPCS-Go/wiki/关于-获取百度-BDUSS
或者百度搜索: 获取百度BDUSS`,
Category: "百度帐号",
Before: reloadFn,
After: saveFunc,
Action: func(c *cli.Context) error {
var bduss, ptoken, stoken string
if c.IsSet("bduss") {
bduss = c.String("bduss")
ptoken = c.String("ptoken")
stoken = c.String("stoken")
} else if c.NArg() == 0 {
var err error
bduss, ptoken, stoken, err = pcscommand.RunLogin(c.String("username"), c.String("password"))
if err != nil {
fmt.Println(err)
return err
}
} else {
cli.ShowCommandHelp(c, c.Command.Name)
return nil
}
baidu, err := pcsconfig.Config.SetupUserByBDUSS(bduss, ptoken, stoken)
if err != nil {
fmt.Println(err)
return nil
}
fmt.Println("百度帐号登录成功:", baidu.Name)
return nil
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "username",
Usage: "登录百度帐号的用户名(手机号/邮箱/用户名)",
},
cli.StringFlag{
Name: "password",
Usage: "登录百度帐号的用户名的密码",
},
cli.StringFlag{
Name: "bduss",
Usage: "使用百度 BDUSS 来登录百度帐号",
},
cli.StringFlag{
Name: "ptoken",
Usage: "百度 PTOKEN, 配合 -bduss 参数使用 (可选)",
},
cli.StringFlag{
Name: "stoken",
Usage: "百度 STOKEN, 配合 -bduss 参数使用 (可选)",
},
},
},
{
Name: "su",
Usage: "切换百度帐号",
Description: `
切换已登录的百度帐号:
如果运行该条命令没有提供参数, 程序将会列出所有的百度帐号, 供选择切换.
示例:
BaiduPCS-Go su
BaiduPCS-Go su <uid or name>
`,
Category: "百度帐号",
Before: reloadFn,
After: saveFunc,
Action: func(c *cli.Context) error {
if c.NArg() >= 2 {
cli.ShowCommandHelp(c, c.Command.Name)
return nil
}
numLogins := pcsconfig.Config.NumLogins()
if numLogins == 0 {
fmt.Printf("未设置任何百度帐号, 不能切换\n")
return nil
}
var (
inputData = c.Args().Get(0)
uid uint64
)
if c.NArg() == 1 {
// 直接切换
uid, _ = strconv.ParseUint(inputData, 10, 64)
} else if c.NArg() == 0 {
// 输出所有帐号供选择切换
cli.HandleAction(app.Command("loglist").Action, c)
// 提示输入 index
var index string
fmt.Printf("输入要切换帐号的 # 值 > ")
_, err := fmt.Scanln(&index)
if err != nil {
return nil
}
if n, err := strconv.Atoi(index); err == nil && n >= 0 && n < numLogins {
uid = pcsconfig.Config.BaiduUserList[n].UID
} else {
fmt.Printf("切换用户失败, 请检查 # 值是否正确\n")
return nil
}
} else {
cli.ShowCommandHelp(c, c.Command.Name)
}
switchedUser, err := pcsconfig.Config.SwitchUser(&pcsconfig.BaiduBase{
Name: inputData,
})
if err != nil {
switchedUser, err = pcsconfig.Config.SwitchUser(&pcsconfig.BaiduBase{
UID: uid,
})
if err != nil {
fmt.Printf("切换用户失败, %s\n", err)
return nil
}
}
fmt.Printf("切换用户: %s\n", switchedUser.Name)
return nil
},
},
{
Name: "logout",
Usage: "退出百度帐号",
Description: "退出当前登录的百度帐号",
Category: "百度帐号",
Before: reloadFn,
After: saveFunc,
Action: func(c *cli.Context) error {
if pcsconfig.Config.NumLogins() == 0 {
fmt.Println("未设置任何百度帐号, 不能退出")
return nil
}
var (
confirm string
activeUser = pcsconfig.Config.ActiveUser()
)
if !c.Bool("y") {
fmt.Printf("确认退出百度帐号: %s ? (y/n) > ", activeUser.Name)
_, err := fmt.Scanln(&confirm)
if err != nil || (confirm != "y" && confirm != "Y") {
return err
}
}
deletedUser, err := pcsconfig.Config.DeleteUser(&pcsconfig.BaiduBase{
UID: activeUser.UID,
})
if err != nil {
fmt.Printf("退出用户 %s, 失败, 错误: %s\n", activeUser.Name, err)
}
fmt.Printf("退出用户成功, %s\n", deletedUser.Name)
return nil
},
Flags: []cli.Flag{
cli.BoolFlag{
Name: "y",
Usage: "确认退出帐号",
},
},
},
{
Name: "loglist",
Usage: "列出帐号列表",
Description: "列出所有已登录的百度帐号",
Category: "百度帐号",
Before: reloadFn,
Action: func(c *cli.Context) error {
fmt.Println(pcsconfig.Config.BaiduUserList.String())
return nil
},
},
{
Name: "who",
Usage: "获取当前帐号",
Description: "获取当前帐号的信息",
Category: "百度帐号",
Before: reloadFn,
Action: func(c *cli.Context) error {
activeUser := pcsconfig.Config.ActiveUser()
fmt.Printf("当前帐号 uid: %d, 用户名: %s, 性别: %s, 年龄: %.1f\n", activeUser.UID, activeUser.Name, activeUser.Sex, activeUser.Age)
return nil
},
},
{
Name: "quota",
Usage: "获取网盘配额",
Description: "获取网盘的总储存空间, 和已使用的储存空间",
Category: "百度网盘",
Before: reloadFn,
Action: func(c *cli.Context) error {
pcscommand.RunGetQuota()
return nil
},
},
{
Name: "cd",
Category: "百度网盘",
Usage: "切换工作目录",
Description: `
BaiduPCS-Go cd <目录, 绝对路径或相对路径>
示例:
切换 /我的资源 工作目录:
BaiduPCS-Go cd /我的资源
切换上级目录:
BaiduPCS-Go cd ..
切换根目录:
BaiduPCS-Go cd /
切换 /我的资源 工作目录, 并自动列出 /我的资源 下的文件和目录
BaiduPCS-Go cd -l 我的资源
使用通配符:
BaiduPCS-Go cd /我的*
`,
Before: reloadFn,
After: saveFunc,
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
cli.ShowCommandHelp(c, c.Command.Name)
return nil
}
pcscommand.RunChangeDirectory(c.Args().Get(0), c.Bool("l"))
return nil
},
Flags: []cli.Flag{
cli.BoolFlag{
Name: "l",
Usage: "切换工作目录后自动列出工作目录下的文件和目录",
},
},
},
{
Name: "ls",
Aliases: []string{"l", "ll"},
Usage: "列出目录",
UsageText: app.Name + " ls <目录>",
Description: `
列出当前工作目录内的文件和目录, 或指定目录内的文件和目录
示例:
列出 我的资源 内的文件和目录
BaiduPCS-Go ls 我的资源
绝对路径
BaiduPCS-Go ls /我的资源
降序排序
BaiduPCS-Go ls -desc 我的资源
按文件大小降序排序
BaiduPCS-Go ls -size -desc 我的资源
使用通配符
BaiduPCS-Go ls /我的*
`,
Category: "百度网盘",
Before: reloadFn,
Action: func(c *cli.Context) error {
orderOptions := &baidupcs.OrderOptions{}
switch {
case c.IsSet("asc"):
orderOptions.Order = baidupcs.OrderAsc
case c.IsSet("desc"):
orderOptions.Order = baidupcs.OrderDesc
default:
orderOptions.Order = baidupcs.OrderAsc
}
switch {
case c.IsSet("time"):
orderOptions.By = baidupcs.OrderByTime
case c.IsSet("name"):
orderOptions.By = baidupcs.OrderByName
case c.IsSet("size"):
orderOptions.By = baidupcs.OrderBySize
default:
orderOptions.By = baidupcs.OrderByName
}
pcscommand.RunLs(c.Args().Get(0), &pcscommand.LsOptions{
Total: c.Bool("l") || c.Parent().Args().Get(0) == "ll",
}, orderOptions)
return nil
},
Flags: []cli.Flag{
cli.BoolFlag{
Name: "l",
Usage: "详细显示",
},
cli.BoolFlag{
Name: "asc",
Usage: "升序排序",
},
cli.BoolFlag{
Name: "desc",
Usage: "降序排序",
},
cli.BoolFlag{
Name: "time",
Usage: "根据时间排序",
},
cli.BoolFlag{
Name: "name",
Usage: "根据文件名排序",
},
cli.BoolFlag{
Name: "size",
Usage: "根据大小排序",
},
},
},
{
Name: "search",
Aliases: []string{"s"},
Usage: "搜索文件",
UsageText: app.Name + " search [-path=<需要检索的目录>] [-r] 关键字",
Description: `
按文件名搜索文件(不支持查找目录)。
默认在当前工作目录搜索.
示例:
搜索根目录的文件
BaiduPCS-Go search -path=/ 关键字
搜索当前工作目录的文件
BaiduPCS-Go search 关键字
递归搜索当前工作目录的文件
BaiduPCS-Go search -r 关键字
`,
Category: "百度网盘",
Before: reloadFn,
Action: func(c *cli.Context) error {
if c.NArg() < 1 {
cli.ShowCommandHelp(c, c.Command.Name)
return nil
}
pcscommand.RunSearch(c.String("path"), c.Args().Get(0), &pcscommand.SearchOptions{
Total: c.Bool("l"),
Recurse: c.Bool("r"),
})
return nil
},
Flags: []cli.Flag{
cli.BoolFlag{
Name: "l",
Usage: "详细显示",
},
cli.BoolFlag{
Name: "r",
Usage: "递归搜索",
},
cli.StringFlag{
Name: "path",
Usage: "需要检索的目录",
Value: ".",
},
},
},
{
Name: "tree",
Aliases: []string{"t"},
Usage: "列出目录的树形图",
UsageText: app.Name + " tree <目录>",
Category: "百度网盘",
Before: reloadFn,
Action: func(c *cli.Context) error {
pcscommand.RunTree(c.Args().Get(0))
return nil
},
},
{
Name: "pwd",
Usage: "输出工作目录",
UsageText: app.Name + " pwd",
Category: "百度网盘",
Before: reloadFn,
Action: func(c *cli.Context) error {
fmt.Println(pcsconfig.Config.ActiveUser().Workdir)
return nil
},
},
{
Name: "meta",
Usage: "获取文件/目录的元信息",
UsageText: app.Name + " meta <文件/目录1> <文件/目录2> <文件/目录3> ...",
Description: "默认获取工作目录元信息",
Category: "百度网盘",
Before: reloadFn,
Action: func(c *cli.Context) error {
var (
ca = c.Args()
as []string
)
if len(ca) == 0 {
as = []string{""}
} else {
as = ca
}
pcscommand.RunGetMeta(as...)
return nil
},
},
{
Name: "rm",
Usage: "删除文件/目录",
UsageText: app.Name + " rm <文件/目录的路径1> <文件/目录2> <文件/目录3> ...",
Description: `
注意: 删除多个文件和目录时, 请确保每一个文件和目录都存在, 否则删除操作会失败.
被删除的文件或目录可在网盘文件回收站找回.
示例:
删除 /我的资源/1.mp4
BaiduPCS-Go rm /我的资源/1.mp4
删除 /我的资源/1.mp4 和 /我的资源/2.mp4
BaiduPCS-Go rm /我的资源/1.mp4 /我的资源/2.mp4
删除 /我的资源 内的所有文件和目录, 但不删除该目录
BaiduPCS-Go rm /我的资源/*
删除 /我的资源 整个目录 !!
BaiduPCS-Go rm /我的资源
`,
Category: "百度网盘",
Before: reloadFn,
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
cli.ShowCommandHelp(c, c.Command.Name)
return nil
}
pcscommand.RunRemove(c.Args()...)
return nil
},
},
{
Name: "mkdir",
Usage: "创建目录",
UsageText: app.Name + " mkdir <目录>",
Category: "百度网盘",
Before: reloadFn,
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
cli.ShowCommandHelp(c, c.Command.Name)
return nil
}
pcscommand.RunMkdir(c.Args().Get(0))
return nil
},
},
{
Name: "cp",
Usage: "拷贝文件/目录",
UsageText: `BaiduPCS-Go cp <文件/目录> <目标文件/目录>
BaiduPCS-Go cp <文件/目录1> <文件/目录2> <文件/目录3> ... <目标目录>`,
Description: `
注意: 拷贝多个文件和目录时, 请确保每一个文件和目录都存在, 否则拷贝操作会失败.
示例:
将 /我的资源/1.mp4 复制到 根目录 /
BaiduPCS-Go cp /我的资源/1.mp4 /
将 /我的资源/1.mp4 和 /我的资源/2.mp4 复制到 根目录 /
BaiduPCS-Go cp /我的资源/1.mp4 /我的资源/2.mp4 /
`,
Category: "百度网盘",
Before: reloadFn,
Action: func(c *cli.Context) error {
if c.NArg() <= 1 {
cli.ShowCommandHelp(c, c.Command.Name)
return nil
}
pcscommand.RunCopy(c.Args()...)
return nil
},
},
{
Name: "mv",
Usage: "移动/重命名文件/目录",
UsageText: `移动:
BaiduPCS-Go mv <文件/目录1> <文件/目录2> <文件/目录3> ... <目标目录>
重命名:
BaiduPCS-Go mv <文件/目录> <重命名的文件/目录>`,
Description: `
注意: 移动多个文件和目录时, 请确保每一个文件和目录都存在, 否则移动操作会失败.
示例:
将 /我的资源/1.mp4 移动到 根目录 /
BaiduPCS-Go mv /我的资源/1.mp4 /
将 /我的资源/1.mp4 重命名为 /我的资源/3.mp4
BaiduPCS-Go mv /我的资源/1.mp4 /我的资源/3.mp4
`,
Category: "百度网盘",
Before: reloadFn,
Action: func(c *cli.Context) error {
if c.NArg() <= 1 {
cli.ShowCommandHelp(c, c.Command.Name)
return nil
}
pcscommand.RunMove(c.Args()...)
return nil
},
},
{
Name: "download",
Aliases: []string{"d"},
Usage: "下载文件/目录",
UsageText: app.Name + " download <文件/目录路径1> <文件/目录2> <文件/目录3> ...",
Description: `
下载的文件默认保存到, 程序所在目录的 download/ 目录.
通过 BaiduPCS-Go config set -savedir <savedir>, 自定义保存的目录.
支持多个文件或目录下载.
支持下载完成后自动校验文件, 但并不是所有的文件都支持校验!
自动跳过下载重名的文件!
下载模式说明:
pcs: 通过百度网盘的 PCS API 下载
stream: 通过百度网盘的 PCS API, 以流式文件的方式下载, 效果同 pcs
locate: 默认的下载模式。从百度网盘 Android 客户端, 获取下载链接的方式来下载
示例:
设置保存目录, 保存到 D:\Downloads
注意区别反斜杠 "\" 和 斜杠 "/" !!!
BaiduPCS-Go config set -savedir D:\\Downloads
或者
BaiduPCS-Go config set -savedir D:/Downloads
下载 /我的资源/1.mp4
BaiduPCS-Go d /我的资源/1.mp4
下载 /我的资源 整个目录!!
BaiduPCS-Go d /我的资源
下载网盘内的全部文件!!
BaiduPCS-Go d /
BaiduPCS-Go d *
`,
Category: "百度网盘",
Before: reloadFn,
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
cli.ShowCommandHelp(c, c.Command.Name)
return nil
}
// 处理saveTo
var (
saveTo string
)
if c.Bool("save") {
saveTo = "."
} else if c.String("saveto") != "" {
saveTo = filepath.Clean(c.String("saveto"))
}
// 处理解析downloadMode