From f15de22ce2da599ecffec78890b0c98938ff672e Mon Sep 17 00:00:00 2001 From: tickstep Date: Wed, 27 Oct 2021 22:13:29 +0800 Subject: [PATCH] add token command --- internal/command/token.go | 99 +++++++++++++++++++++++++++++++++++++++ main.go | 5 +- 2 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 internal/command/token.go diff --git a/internal/command/token.go b/internal/command/token.go new file mode 100644 index 0000000..0fd649a --- /dev/null +++ b/internal/command/token.go @@ -0,0 +1,99 @@ +// Copyright (c) 2020 tickstep. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package command + +import ( + "fmt" + "github.com/tickstep/aliyunpan-api/aliyunpan" + "github.com/tickstep/aliyunpan/cmder" + "github.com/tickstep/aliyunpan/internal/config" + "github.com/urfave/cli" +) + +func CmdToken() cli.Command { + return cli.Command{ + Name: "token", + Usage: "Token相关操作", + UsageText: cmder.App().Name + " token", + Category: "阿里云盘账号", + Before: cmder.ReloadConfigFunc, + Action: func(c *cli.Context) error { + cli.ShowCommandHelp(c, c.Command.Name) + return nil + }, + Subcommands: []cli.Command{ + { + Name: "update", + Usage: "更新Token", + UsageText: cmder.App().Name + " token update", + Description: ` +示例: + + 更新当前登录用户的Token + aliyunpan token update -mode 1 + + 更新全部登录用户的Token + aliyunpan token update -mode 2 +`, + Action: func(c *cli.Context) error { + if config.Config.ActiveUser() == nil { + fmt.Println("未登录账号,无需刷新Token") + return nil + } + + modeFlag := "1" + if c.IsSet("mode") { + modeFlag = c.String("mode") + } + RunTokenUpdate(modeFlag) + return nil + }, + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "mode", + Usage: "模式,1-登录用户,2-全部用户", + Value: "1", + }, + }, + }, + }, + } +} + +// RunTokenUpdate 执行Token更新 +func RunTokenUpdate(modeFlag string) { + cmder.ReloadConfigFunc(nil) + userList := config.Config.UserList + if userList == nil || len(userList) == 0 { + fmt.Printf("没有登录用户,无需刷新Token\n") + return + } + for _,user := range userList { + if modeFlag == "1" { + if user.UserId != config.Config.ActiveUID { + continue + } + } + newToken,e := aliyunpan.GetAccessTokenFromRefreshToken(user.RefreshToken) + if e != nil { + fmt.Printf("无法为%s用户获取新的RefreshToken,可能需要重新登录\n", user.Nickname) + continue + } + if newToken != nil && newToken.RefreshToken != "" { + user.RefreshToken = newToken.RefreshToken + fmt.Printf("成功刷新%s用户的RefreshToken\n", user.Nickname) + } + } + cmder.SaveConfigFunc(nil) +} diff --git a/main.go b/main.go index 64e5175..84893b5 100644 --- a/main.go +++ b/main.go @@ -47,7 +47,7 @@ const ( var ( // Version 版本号 - Version = "v0.0.3" + Version = "v0.0.4-dev" historyFilePath = filepath.Join(config.GetConfigDir(), "aliyunpan_command_history.txt") @@ -366,6 +366,9 @@ func main() { // 获取当前帐号空间配额 quota command.CmdQuota(), + // Token操作 + command.CmdToken(), + // 切换工作目录 cd command.CmdCd(),