From a9752d92cfc244bdae008b434bce3ffc321dbaa5 Mon Sep 17 00:00:00 2001 From: dddddai Date: Thu, 14 Jan 2021 23:28:01 +0800 Subject: [PATCH 1/2] Add `config reset` --- README.md | 9 +++++++-- internal/pcsconfig/pcsconfig.go | 4 ++-- main.go | 23 ++++++++++++++++++++--- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 9e73b0c..04cb0ca 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,8 @@ iikira/BaiduPCS-Go was largely inspired by [GangZhuo/BaiduPCS](https://github.co * [4. 网盘内列出文件和目录](#4-网盘内列出文件和目录) * [5. 下载文件](#5-下载文件) * [6. 设置下载最大并发量](#6-设置下载最大并发量) - * [7. 退出程序](#7-退出程序) + * [7. 恢复默认配置](#7-恢复默认配置) + * [8. 退出程序](#8-退出程序) - [已知问题](#已知问题) - [TODO](#todo) - [交流反馈](#交流反馈) @@ -980,7 +981,11 @@ cli交互模式下, 运行命令 `config set -max_parallel 2` 将下载最大并 注意:下载最大并发量的值不宜设置过高, 可能会导致百度帐号被限制下载 -## 7. 退出程序 +## 7. 恢复默认配置 + +cli交互模式下, 运行命令 `config reset` + +## 8. 退出程序 运行命令 `quit` 或 `exit` 或 组合键 `Ctrl+C` 或 组合键 `Ctrl+D` diff --git a/internal/pcsconfig/pcsconfig.go b/internal/pcsconfig/pcsconfig.go index fed2783..a721825 100644 --- a/internal/pcsconfig/pcsconfig.go +++ b/internal/pcsconfig/pcsconfig.go @@ -133,7 +133,7 @@ func (c *PCSConfig) init() error { return ErrConfigFileNotExist } - c.initDefaultConfig() + c.InitDefaultConfig() err := c.loadConfigFromFile() if err != nil { return err @@ -219,7 +219,7 @@ func (c *PCSConfig) loadConfigFromFile() (err error) { return nil } -func (c *PCSConfig) initDefaultConfig() { +func (c *PCSConfig) InitDefaultConfig() { c.AppID = 266719 c.CacheSize = 65536 c.MaxParallel = 1 diff --git a/main.go b/main.go index 6d7dba3..c1e4783 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,8 @@ import ( "strings" "unicode" + "github.com/olekukonko/tablewriter" + "github.com/peterh/liner" "github.com/qjfoidnh/BaiduPCS-Go/baidupcs" "github.com/qjfoidnh/BaiduPCS-Go/internal/pcscommand" "github.com/qjfoidnh/BaiduPCS-Go/internal/pcsconfig" @@ -29,8 +31,6 @@ import ( "github.com/qjfoidnh/BaiduPCS-Go/pcsutil/getip" "github.com/qjfoidnh/BaiduPCS-Go/pcsutil/pcstime" "github.com/qjfoidnh/BaiduPCS-Go/pcsverbose" - "github.com/olekukonko/tablewriter" - "github.com/peterh/liner" "github.com/urfave/cli" ) @@ -1346,7 +1346,7 @@ func main() { strLength, strMd5, strSliceMd5, strCrc32 := strconv.FormatInt(lp.Length, 10), hex.EncodeToString(lp.MD5), hex.EncodeToString(lp.SliceMD5), strconv.FormatUint(uint64(lp.CRC32), 10) fileName := filepath.Base(filePath) - regFileName := strings.Replace(fileName, " ", "_", -1 ) + regFileName := strings.Replace(fileName, " ", "_", -1) regFileName = strings.Replace(regFileName, "#", "_", -1) tb := pcstable.NewTable(os.Stdout) tb.SetColumnAlignment([]int{tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT}) @@ -1923,6 +1923,23 @@ func main() { }, }, }, + { + Name: "reset", + Usage: "恢复默认配置项", + UsageText: app.Name + " config reset", + Description: "", + Action: func(c *cli.Context) error { + pcsconfig.Config.InitDefaultConfig() + err := pcsconfig.Config.Save() + if err != nil { + fmt.Println(err) + return err + } + pcsconfig.Config.PrintTable() + fmt.Println("恢复默认配置成功") + return nil + }, + }, }, }, { From c68dce666d0443768fe0418f2860a866fce5da01 Mon Sep 17 00:00:00 2001 From: dddddai Date: Fri, 15 Jan 2021 22:23:17 +0800 Subject: [PATCH 2/2] Fix version comparison --- internal/pcsupdate/pcsupdate.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/internal/pcsupdate/pcsupdate.go b/internal/pcsupdate/pcsupdate.go index 6a6170a..d8a7b20 100644 --- a/internal/pcsupdate/pcsupdate.go +++ b/internal/pcsupdate/pcsupdate.go @@ -59,7 +59,7 @@ func CheckUpdate(version string, yes bool) { } // 没有更新, 或忽略 Beta 版本, 和版本前缀不符的 - if strings.Contains(releaseInfo.TagName, "Beta") || !strings.HasPrefix(releaseInfo.TagName, "v") || version >= releaseInfo.TagName { + if strings.Contains(releaseInfo.TagName, "Beta") || !strings.HasPrefix(releaseInfo.TagName, "v") || needUpdate(version, releaseInfo.TagName) { fmt.Printf("未检测到更新!\n") return } @@ -255,3 +255,17 @@ func CheckUpdate(version string, yes bool) { fmt.Printf("更新完毕, 请重启程序\n") } + +func needUpdate(current, latest string) bool { + // 去除第一个字符'v' + v1 := strings.Split(current[1:], ".") + v2 := strings.Split(latest[1:], ".") + for i := range v1 { + a, _ := strconv.Atoi(v1[i]) + b, _ := strconv.Atoi(v2[i]) + if a < b { + return true + } + } + return false +}