mirror of
https://gitee.com/zhangsken/aliyunpan.git
synced 2026-10-16 10:29:42 +08:00
refractor login routine
This commit is contained in:
@@ -1,39 +1,11 @@
|
|||||||
package cmder
|
package cmder
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/tickstep/aliyunpan-api/aliyunpan"
|
|
||||||
"github.com/tickstep/aliyunpan-api/aliyunpan/apierror"
|
|
||||||
"github.com/tickstep/aliyunpan/cmder/cmdliner"
|
|
||||||
"github.com/tickstep/aliyunpan/internal/config"
|
|
||||||
"github.com/tickstep/aliyunpan/internal/functions/panlogin"
|
|
||||||
"github.com/tickstep/library-go/logger"
|
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
"sync"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
appInstance *cli.App
|
appInstance *cli.App
|
||||||
|
|
||||||
saveConfigMutex *sync.Mutex = new(sync.Mutex)
|
|
||||||
|
|
||||||
ReloadConfigFunc = func(c *cli.Context) error {
|
|
||||||
err := config.Config.Reload()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("重载配置错误: %s\n", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
SaveConfigFunc = func(c *cli.Context) error {
|
|
||||||
saveConfigMutex.Lock()
|
|
||||||
defer saveConfigMutex.Unlock()
|
|
||||||
err := config.Config.Save()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("保存配置错误: %s\n", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func SetApp(app *cli.App) {
|
func SetApp(app *cli.App) {
|
||||||
@@ -43,73 +15,3 @@ func SetApp(app *cli.App) {
|
|||||||
func App() *cli.App {
|
func App() *cli.App {
|
||||||
return appInstance
|
return appInstance
|
||||||
}
|
}
|
||||||
|
|
||||||
func DoLoginHelper(refreshToken string) (refreshTokenStr string, webToken aliyunpan.WebLoginToken, error error) {
|
|
||||||
line := cmdliner.NewLiner()
|
|
||||||
defer line.Close()
|
|
||||||
|
|
||||||
if refreshToken == "" {
|
|
||||||
refreshToken, error = line.State.Prompt("请输入RefreshToken, 回车键提交 > ")
|
|
||||||
if error != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// app login
|
|
||||||
atoken, apperr := aliyunpan.GetAccessTokenFromRefreshToken(refreshToken)
|
|
||||||
if apperr != nil {
|
|
||||||
if apperr.Code == apierror.ApiCodeTokenExpiredCode || apperr.Code == apierror.ApiCodeRefreshTokenExpiredCode {
|
|
||||||
fmt.Println("Token过期,需要重新登录")
|
|
||||||
} else {
|
|
||||||
fmt.Println("Token登录失败:", apperr)
|
|
||||||
}
|
|
||||||
return "", webToken, fmt.Errorf("登录失败")
|
|
||||||
}
|
|
||||||
refreshTokenStr = refreshToken
|
|
||||||
return refreshTokenStr, *atoken, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func TryLogin() *config.PanUser {
|
|
||||||
// can do automatically login?
|
|
||||||
for _, u := range config.Config.UserList {
|
|
||||||
if u.UserId == config.Config.ActiveUID {
|
|
||||||
// login
|
|
||||||
_, webToken, err := DoLoginHelper(u.RefreshToken)
|
|
||||||
if err != nil {
|
|
||||||
logger.Verboseln("automatically login use saved refresh token error ", err)
|
|
||||||
if u.TokenId != "" {
|
|
||||||
logger.Verboseln("try to login use tokenId")
|
|
||||||
h := panlogin.NewLoginHelper(config.DefaultTokenServiceWebHost)
|
|
||||||
r, e := h.GetRefreshToken(u.TokenId)
|
|
||||||
if e != nil {
|
|
||||||
logger.Verboseln("try to login use tokenId error", e)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
refreshToken, e := h.ParseSecureRefreshToken("", r.SecureRefreshToken)
|
|
||||||
if e != nil {
|
|
||||||
logger.Verboseln("try to parse refresh token error", e)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
_, webToken, err = DoLoginHelper(refreshToken)
|
|
||||||
if err != nil {
|
|
||||||
logger.Verboseln("try to use refresh token from tokenId error", e)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
fmt.Println("Token重新自动登录成功")
|
|
||||||
// save new refresh token
|
|
||||||
u.RefreshToken = refreshToken
|
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
// success
|
|
||||||
u.WebToken = webToken
|
|
||||||
|
|
||||||
// save
|
|
||||||
SaveConfigFunc(nil)
|
|
||||||
// reload
|
|
||||||
ReloadConfigFunc(nil)
|
|
||||||
return config.Config.ActiveUser()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ func CmdAlbum() cli.Command {
|
|||||||
Usage: "相簿(Beta)",
|
Usage: "相簿(Beta)",
|
||||||
UsageText: cmder.App().Name + " album",
|
UsageText: cmder.App().Name + " album",
|
||||||
Category: "阿里云盘",
|
Category: "阿里云盘",
|
||||||
Before: cmder.ReloadConfigFunc,
|
Before: ReloadConfigFunc,
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
cli.ShowCommandHelp(c, c.Command.Name)
|
cli.ShowCommandHelp(c, c.Command.Name)
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -43,8 +43,8 @@ func CmdCd() cli.Command {
|
|||||||
切换根目录:
|
切换根目录:
|
||||||
aliyunpan cd /
|
aliyunpan cd /
|
||||||
`,
|
`,
|
||||||
Before: cmder.ReloadConfigFunc,
|
Before: ReloadConfigFunc,
|
||||||
After: cmder.SaveConfigFunc,
|
After: SaveConfigFunc,
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
if c.NArg() == 0 {
|
if c.NArg() == 0 {
|
||||||
cli.ShowCommandHelp(c, c.Command.Name)
|
cli.ShowCommandHelp(c, c.Command.Name)
|
||||||
@@ -73,7 +73,7 @@ func CmdPwd() cli.Command {
|
|||||||
Usage: "输出工作目录",
|
Usage: "输出工作目录",
|
||||||
UsageText: cmder.App().Name + " pwd",
|
UsageText: cmder.App().Name + " pwd",
|
||||||
Category: "阿里云盘",
|
Category: "阿里云盘",
|
||||||
Before: cmder.ReloadConfigFunc,
|
Before: ReloadConfigFunc,
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
if config.Config.ActiveUser() == nil {
|
if config.Config.ActiveUser() == nil {
|
||||||
fmt.Println("未登录账号")
|
fmt.Println("未登录账号")
|
||||||
|
|||||||
@@ -131,7 +131,6 @@ func (r *RapidUploadItem) createRapidUploadLink(hideRelativePath bool) string {
|
|||||||
p := r.FilePath
|
p := r.FilePath
|
||||||
p = strings.ReplaceAll(p, "\\", "/")
|
p = strings.ReplaceAll(p, "\\", "/")
|
||||||
|
|
||||||
|
|
||||||
fileName := path.Base(p)
|
fileName := path.Base(p)
|
||||||
dirPath := path.Dir(p)
|
dirPath := path.Dir(p)
|
||||||
|
|
||||||
@@ -160,8 +159,8 @@ func CmdConfig() cli.Command {
|
|||||||
Usage: "显示和修改程序配置项",
|
Usage: "显示和修改程序配置项",
|
||||||
Description: "显示和修改程序配置项",
|
Description: "显示和修改程序配置项",
|
||||||
Category: "配置",
|
Category: "配置",
|
||||||
Before: cmder.ReloadConfigFunc,
|
Before: ReloadConfigFunc,
|
||||||
After: cmder.SaveConfigFunc,
|
After: SaveConfigFunc,
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
fmt.Printf("----\n运行 %s config set 可进行设置配置\n\n当前配置:\n", cmder.App().Name)
|
fmt.Printf("----\n运行 %s config set 可进行设置配置\n\n当前配置:\n", cmder.App().Name)
|
||||||
config.Config.PrintTable()
|
config.Config.PrintTable()
|
||||||
@@ -283,7 +282,6 @@ func CmdConfig() cli.Command {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func CmdTool() cli.Command {
|
func CmdTool() cli.Command {
|
||||||
return cli.Command{
|
return cli.Command{
|
||||||
Name: "tool",
|
Name: "tool",
|
||||||
|
|||||||
@@ -109,7 +109,7 @@ func CmdDownload() cli.Command {
|
|||||||
5)排除 myfile.txt 文件:-exn "^myfile.txt$"
|
5)排除 myfile.txt 文件:-exn "^myfile.txt$"
|
||||||
`,
|
`,
|
||||||
Category: "阿里云盘",
|
Category: "阿里云盘",
|
||||||
Before: cmder.ReloadConfigFunc,
|
Before: ReloadConfigFunc,
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
if c.NArg() == 0 {
|
if c.NArg() == 0 {
|
||||||
cli.ShowCommandHelp(c, c.Command.Name)
|
cli.ShowCommandHelp(c, c.Command.Name)
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/olekukonko/tablewriter"
|
"github.com/olekukonko/tablewriter"
|
||||||
"github.com/tickstep/aliyunpan-api/aliyunpan"
|
"github.com/tickstep/aliyunpan-api/aliyunpan"
|
||||||
"github.com/tickstep/aliyunpan/cmder"
|
|
||||||
"github.com/tickstep/aliyunpan/cmder/cmdtable"
|
"github.com/tickstep/aliyunpan/cmder/cmdtable"
|
||||||
"github.com/tickstep/aliyunpan/internal/config"
|
"github.com/tickstep/aliyunpan/internal/config"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
@@ -38,8 +37,8 @@ func CmdDrive() cli.Command {
|
|||||||
aliyunpan drive <driveId>
|
aliyunpan drive <driveId>
|
||||||
`,
|
`,
|
||||||
Category: "阿里云盘账号",
|
Category: "阿里云盘账号",
|
||||||
Before: cmder.ReloadConfigFunc,
|
Before: ReloadConfigFunc,
|
||||||
After: cmder.SaveConfigFunc,
|
After: SaveConfigFunc,
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
inputData := c.Args().Get(0)
|
inputData := c.Args().Get(0)
|
||||||
targetDriveId := strings.TrimSpace(inputData)
|
targetDriveId := strings.TrimSpace(inputData)
|
||||||
|
|||||||
@@ -28,7 +28,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func CmdExport() cli.Command {
|
func CmdExport() cli.Command {
|
||||||
return cli.Command{
|
return cli.Command{
|
||||||
Name: "export",
|
Name: "export",
|
||||||
@@ -50,7 +49,7 @@ func CmdExport() cli.Command {
|
|||||||
aliyunpan export / /Users/tickstep/Downloads/export_files.txt
|
aliyunpan export / /Users/tickstep/Downloads/export_files.txt
|
||||||
`,
|
`,
|
||||||
Category: "阿里云盘",
|
Category: "阿里云盘",
|
||||||
Before: cmder.ReloadConfigFunc,
|
Before: ReloadConfigFunc,
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
if c.NArg() < 2 {
|
if c.NArg() < 2 {
|
||||||
cli.ShowCommandHelp(c, c.Command.Name)
|
cli.ShowCommandHelp(c, c.Command.Name)
|
||||||
@@ -75,7 +74,6 @@ func CmdExport() cli.Command {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func RunExportFiles(driveId string, overwrite bool, panPaths []string, saveLocalFilePath string) {
|
func RunExportFiles(driveId string, overwrite bool, panPaths []string, saveLocalFilePath string) {
|
||||||
activeUser := config.Config.ActiveUser()
|
activeUser := config.Config.ActiveUser()
|
||||||
panClient := activeUser.PanClient()
|
panClient := activeUser.PanClient()
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ func CmdImport() cli.Command {
|
|||||||
aliyunpan import -saveto=/ /Users/tickstep/Downloads/export_files.txt
|
aliyunpan import -saveto=/ /Users/tickstep/Downloads/export_files.txt
|
||||||
`,
|
`,
|
||||||
Category: "阿里云盘",
|
Category: "阿里云盘",
|
||||||
Before: cmder.ReloadConfigFunc,
|
Before: ReloadConfigFunc,
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
if c.NArg() < 1 {
|
if c.NArg() < 1 {
|
||||||
cli.ShowCommandHelp(c, c.Command.Name)
|
cli.ShowCommandHelp(c, c.Command.Name)
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ func CmdLocateUrl() cli.Command {
|
|||||||
aliyunpan locate -saveto "/Volumes/Downloads/file_url.txt" /我的资源
|
aliyunpan locate -saveto "/Volumes/Downloads/file_url.txt" /我的资源
|
||||||
`,
|
`,
|
||||||
Category: "阿里云盘",
|
Category: "阿里云盘",
|
||||||
Before: cmder.ReloadConfigFunc,
|
Before: ReloadConfigFunc,
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
if c.NArg() == 0 {
|
if c.NArg() == 0 {
|
||||||
cli.ShowCommandHelp(c, c.Command.Name)
|
cli.ShowCommandHelp(c, c.Command.Name)
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ package command
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/tickstep/aliyunpan-api/aliyunpan"
|
"github.com/tickstep/aliyunpan-api/aliyunpan"
|
||||||
"github.com/tickstep/aliyunpan/cmder"
|
|
||||||
"github.com/tickstep/aliyunpan/cmder/cmdliner"
|
"github.com/tickstep/aliyunpan/cmder/cmdliner"
|
||||||
"github.com/tickstep/aliyunpan/internal/config"
|
"github.com/tickstep/aliyunpan/internal/config"
|
||||||
"github.com/tickstep/aliyunpan/internal/functions/panlogin"
|
"github.com/tickstep/aliyunpan/internal/functions/panlogin"
|
||||||
@@ -42,8 +41,8 @@ func CmdLogin() cli.Command {
|
|||||||
aliyunpan login -QrCode
|
aliyunpan login -QrCode
|
||||||
`,
|
`,
|
||||||
Category: "阿里云盘账号",
|
Category: "阿里云盘账号",
|
||||||
Before: cmder.ReloadConfigFunc, // 每次进行登录动作的时候需要调用刷新配置
|
Before: ReloadConfigFunc, // 每次进行登录动作的时候需要调用刷新配置
|
||||||
After: cmder.SaveConfigFunc, // 登录完成需要调用保存配置
|
After: SaveConfigFunc, // 登录完成需要调用保存配置
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
refreshTokenStr := ""
|
refreshTokenStr := ""
|
||||||
if refreshTokenStr == "" {
|
if refreshTokenStr == "" {
|
||||||
@@ -93,8 +92,8 @@ func CmdLogout() cli.Command {
|
|||||||
Usage: "退出阿里帐号",
|
Usage: "退出阿里帐号",
|
||||||
Description: "退出当前登录的帐号",
|
Description: "退出当前登录的帐号",
|
||||||
Category: "阿里云盘账号",
|
Category: "阿里云盘账号",
|
||||||
Before: cmder.ReloadConfigFunc,
|
Before: ReloadConfigFunc,
|
||||||
After: cmder.SaveConfigFunc,
|
After: SaveConfigFunc,
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
if config.Config.NumLogins() == 0 {
|
if config.Config.NumLogins() == 0 {
|
||||||
fmt.Println("未设置任何帐号, 不能退出")
|
fmt.Println("未设置任何帐号, 不能退出")
|
||||||
@@ -183,6 +182,6 @@ func RunLogin(useQrCodeLogin bool, refreshToken string) (tokenId, refreshTokenSt
|
|||||||
tokenId = qrCodeUrlResult.TokenId
|
tokenId = qrCodeUrlResult.TokenId
|
||||||
}
|
}
|
||||||
|
|
||||||
refreshTokenStr, webToken, error = cmder.DoLoginHelper(refreshToken)
|
refreshTokenStr, webToken, error = DoLoginHelper(refreshToken)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ func CmdLs() cli.Command {
|
|||||||
aliyunpan ll /我的资源
|
aliyunpan ll /我的资源
|
||||||
`,
|
`,
|
||||||
Category: "阿里云盘",
|
Category: "阿里云盘",
|
||||||
Before: cmder.ReloadConfigFunc,
|
Before: ReloadConfigFunc,
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
if config.Config.ActiveUser() == nil {
|
if config.Config.ActiveUser() == nil {
|
||||||
fmt.Println("未登录账号")
|
fmt.Println("未登录账号")
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ func CmdMkdir() cli.Command {
|
|||||||
Usage: "创建目录",
|
Usage: "创建目录",
|
||||||
UsageText: cmder.App().Name + " mkdir <目录>",
|
UsageText: cmder.App().Name + " mkdir <目录>",
|
||||||
Category: "阿里云盘",
|
Category: "阿里云盘",
|
||||||
Before: cmder.ReloadConfigFunc,
|
Before: ReloadConfigFunc,
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
if c.NArg() == 0 {
|
if c.NArg() == 0 {
|
||||||
cli.ShowCommandHelp(c, c.Command.Name)
|
cli.ShowCommandHelp(c, c.Command.Name)
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ package command
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/tickstep/aliyunpan-api/aliyunpan"
|
"github.com/tickstep/aliyunpan-api/aliyunpan"
|
||||||
"github.com/tickstep/aliyunpan/cmder"
|
|
||||||
"github.com/tickstep/aliyunpan/cmder/cmdtable"
|
"github.com/tickstep/aliyunpan/cmder/cmdtable"
|
||||||
"github.com/tickstep/aliyunpan/internal/config"
|
"github.com/tickstep/aliyunpan/internal/config"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
@@ -43,7 +42,7 @@ func CmdMv() cli.Command {
|
|||||||
aliyunpan mv /我的资源/*.png /我的图片
|
aliyunpan mv /我的资源/*.png /我的图片
|
||||||
`,
|
`,
|
||||||
Category: "阿里云盘",
|
Category: "阿里云盘",
|
||||||
Before: cmder.ReloadConfigFunc,
|
Before: ReloadConfigFunc,
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
if c.NArg() <= 1 {
|
if c.NArg() <= 1 {
|
||||||
cli.ShowCommandHelp(c, c.Command.Name)
|
cli.ShowCommandHelp(c, c.Command.Name)
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ package command
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/tickstep/aliyunpan/cmder"
|
|
||||||
"github.com/tickstep/aliyunpan/internal/config"
|
"github.com/tickstep/aliyunpan/internal/config"
|
||||||
"github.com/tickstep/library-go/converter"
|
"github.com/tickstep/library-go/converter"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
@@ -34,7 +33,7 @@ func CmdQuota() cli.Command {
|
|||||||
Usage: "获取当前帐号空间配额",
|
Usage: "获取当前帐号空间配额",
|
||||||
Description: "获取网盘的总储存空间, 和已使用的储存空间",
|
Description: "获取网盘的总储存空间, 和已使用的储存空间",
|
||||||
Category: "阿里云盘账号",
|
Category: "阿里云盘账号",
|
||||||
Before: cmder.ReloadConfigFunc,
|
Before: ReloadConfigFunc,
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
if config.Config.ActiveUser() == nil {
|
if config.Config.ActiveUser() == nil {
|
||||||
fmt.Println("未登录账号")
|
fmt.Println("未登录账号")
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ func CmdRecycle() cli.Command {
|
|||||||
aliyunpan recycle delete -all
|
aliyunpan recycle delete -all
|
||||||
`,
|
`,
|
||||||
Category: "阿里云盘",
|
Category: "阿里云盘",
|
||||||
Before: cmder.ReloadConfigFunc,
|
Before: ReloadConfigFunc,
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
if c.NumFlags() <= 0 || c.NArg() <= 0 {
|
if c.NumFlags() <= 0 || c.NArg() <= 0 {
|
||||||
cli.ShowCommandHelp(c, c.Command.Name)
|
cli.ShowCommandHelp(c, c.Command.Name)
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ package command
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/tickstep/aliyunpan-api/aliyunpan/apiutil"
|
"github.com/tickstep/aliyunpan-api/aliyunpan/apiutil"
|
||||||
"github.com/tickstep/aliyunpan/cmder"
|
|
||||||
"github.com/tickstep/aliyunpan/internal/config"
|
"github.com/tickstep/aliyunpan/internal/config"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
"path"
|
"path"
|
||||||
@@ -40,7 +39,7 @@ func CmdRename() cli.Command {
|
|||||||
aliyunpan rename /test/1.mp4 /test/2.mp4
|
aliyunpan rename /test/1.mp4 /test/2.mp4
|
||||||
`,
|
`,
|
||||||
Category: "阿里云盘",
|
Category: "阿里云盘",
|
||||||
Before: cmder.ReloadConfigFunc,
|
Before: ReloadConfigFunc,
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
if c.NArg() != 2 {
|
if c.NArg() != 2 {
|
||||||
cli.ShowCommandHelp(c, c.Command.Name)
|
cli.ShowCommandHelp(c, c.Command.Name)
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ func CmdRm() cli.Command {
|
|||||||
aliyunpan rm /我的资源/*.zip
|
aliyunpan rm /我的资源/*.zip
|
||||||
`,
|
`,
|
||||||
Category: "阿里云盘",
|
Category: "阿里云盘",
|
||||||
Before: cmder.ReloadConfigFunc,
|
Before: ReloadConfigFunc,
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
if c.NArg() == 0 {
|
if c.NArg() == 0 {
|
||||||
cli.ShowCommandHelp(c, c.Command.Name)
|
cli.ShowCommandHelp(c, c.Command.Name)
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ func CmdShare() cli.Command {
|
|||||||
Usage: "分享文件/目录",
|
Usage: "分享文件/目录",
|
||||||
UsageText: cmder.App().Name + " share",
|
UsageText: cmder.App().Name + " share",
|
||||||
Category: "阿里云盘",
|
Category: "阿里云盘",
|
||||||
Before: cmder.ReloadConfigFunc,
|
Before: ReloadConfigFunc,
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
cli.ShowCommandHelp(c, c.Command.Name)
|
cli.ShowCommandHelp(c, c.Command.Name)
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ func CmdSync() cli.Command {
|
|||||||
aliyunpan sync start -h
|
aliyunpan sync start -h
|
||||||
`,
|
`,
|
||||||
Category: "阿里云盘",
|
Category: "阿里云盘",
|
||||||
Before: cmder.ReloadConfigFunc,
|
Before: ReloadConfigFunc,
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
cli.ShowCommandHelp(c, c.Command.Name)
|
cli.ShowCommandHelp(c, c.Command.Name)
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ func CmdToken() cli.Command {
|
|||||||
Usage: "Token相关操作",
|
Usage: "Token相关操作",
|
||||||
UsageText: cmder.App().Name + " token",
|
UsageText: cmder.App().Name + " token",
|
||||||
Category: "阿里云盘账号",
|
Category: "阿里云盘账号",
|
||||||
Before: cmder.ReloadConfigFunc,
|
Before: ReloadConfigFunc,
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
cli.ShowCommandHelp(c, c.Command.Name)
|
cli.ShowCommandHelp(c, c.Command.Name)
|
||||||
return nil
|
return nil
|
||||||
@@ -76,7 +76,7 @@ func CmdToken() cli.Command {
|
|||||||
|
|
||||||
// RunTokenUpdate 执行Token更新
|
// RunTokenUpdate 执行Token更新
|
||||||
func RunTokenUpdate(modeFlag string) {
|
func RunTokenUpdate(modeFlag string) {
|
||||||
cmder.ReloadConfigFunc(nil)
|
ReloadConfigFunc(nil)
|
||||||
|
|
||||||
// 获取当前插件
|
// 获取当前插件
|
||||||
pluginManger := plugins.NewPluginManager(config.GetPluginDir())
|
pluginManger := plugins.NewPluginManager(config.GetPluginDir())
|
||||||
@@ -125,5 +125,5 @@ func RunTokenUpdate(modeFlag string) {
|
|||||||
logger.Verbosef("UserTokenRefreshFinishCallback error: " + er.Error())
|
logger.Verbosef("UserTokenRefreshFinishCallback error: " + er.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cmder.SaveConfigFunc(nil)
|
SaveConfigFunc(nil)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ func CmdTree() cli.Command {
|
|||||||
aliyunpan tree -fp /我的资源
|
aliyunpan tree -fp /我的资源
|
||||||
`,
|
`,
|
||||||
Category: "阿里云盘",
|
Category: "阿里云盘",
|
||||||
Before: cmder.ReloadConfigFunc,
|
Before: ReloadConfigFunc,
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
if config.Config.ActiveUser() == nil {
|
if config.Config.ActiveUser() == nil {
|
||||||
fmt.Println("未登录账号")
|
fmt.Println("未登录账号")
|
||||||
|
|||||||
@@ -155,7 +155,7 @@ func CmdUpload() cli.Command {
|
|||||||
5)排除 myfile.txt 文件:-exn "^myfile.txt$"
|
5)排除 myfile.txt 文件:-exn "^myfile.txt$"
|
||||||
`,
|
`,
|
||||||
Category: "阿里云盘",
|
Category: "阿里云盘",
|
||||||
Before: cmder.ReloadConfigFunc,
|
Before: ReloadConfigFunc,
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
if c.NArg() < 2 {
|
if c.NArg() < 2 {
|
||||||
cli.ShowCommandHelp(c, c.Command.Name)
|
cli.ShowCommandHelp(c, c.Command.Name)
|
||||||
@@ -214,7 +214,7 @@ func CmdRapidUpload() cli.Command {
|
|||||||
aliyunpan rapidupload "aliyunpan://file.dmg|752FCCBFB2436A6FFCA3B287831D4FAA5654B07E|7005440|pan_folder" "aliyunpan://file1.dmg|752FCCBFB2436A6FFCA3B287831D4FAA5654B07E|7005440|pan_folder"
|
aliyunpan rapidupload "aliyunpan://file.dmg|752FCCBFB2436A6FFCA3B287831D4FAA5654B07E|7005440|pan_folder" "aliyunpan://file1.dmg|752FCCBFB2436A6FFCA3B287831D4FAA5654B07E|7005440|pan_folder"
|
||||||
`,
|
`,
|
||||||
Category: "阿里云盘",
|
Category: "阿里云盘",
|
||||||
Before: cmder.ReloadConfigFunc,
|
Before: ReloadConfigFunc,
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
if c.NArg() <= 0 {
|
if c.NArg() <= 0 {
|
||||||
cli.ShowCommandHelp(c, c.Command.Name)
|
cli.ShowCommandHelp(c, c.Command.Name)
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ func CmdLoglist() cli.Command {
|
|||||||
Usage: "列出帐号列表",
|
Usage: "列出帐号列表",
|
||||||
Description: "列出所有已登录的阿里账号",
|
Description: "列出所有已登录的阿里账号",
|
||||||
Category: "阿里云盘账号",
|
Category: "阿里云盘账号",
|
||||||
Before: cmder.ReloadConfigFunc,
|
Before: ReloadConfigFunc,
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
fmt.Println(config.Config.UserList.String())
|
fmt.Println(config.Config.UserList.String())
|
||||||
return nil
|
return nil
|
||||||
@@ -50,8 +50,8 @@ func CmdSu() cli.Command {
|
|||||||
aliyunpan su <uid or name>
|
aliyunpan su <uid or name>
|
||||||
`,
|
`,
|
||||||
Category: "阿里云盘账号",
|
Category: "阿里云盘账号",
|
||||||
Before: cmder.ReloadConfigFunc,
|
Before: ReloadConfigFunc,
|
||||||
After: cmder.SaveConfigFunc,
|
After: SaveConfigFunc,
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
if c.NArg() >= 2 {
|
if c.NArg() >= 2 {
|
||||||
cli.ShowCommandHelp(c, c.Command.Name)
|
cli.ShowCommandHelp(c, c.Command.Name)
|
||||||
@@ -102,7 +102,7 @@ func CmdSu() cli.Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if switchedUser == nil {
|
if switchedUser == nil {
|
||||||
switchedUser = cmder.TryLogin()
|
switchedUser = TryLogin()
|
||||||
}
|
}
|
||||||
|
|
||||||
if switchedUser != nil {
|
if switchedUser != nil {
|
||||||
@@ -122,7 +122,7 @@ func CmdWho() cli.Command {
|
|||||||
Usage: "获取当前帐号",
|
Usage: "获取当前帐号",
|
||||||
Description: "获取当前帐号的信息",
|
Description: "获取当前帐号的信息",
|
||||||
Category: "阿里云盘账号",
|
Category: "阿里云盘账号",
|
||||||
Before: cmder.ReloadConfigFunc,
|
Before: ReloadConfigFunc,
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
if config.Config.ActiveUser() == nil {
|
if config.Config.ActiveUser() == nil {
|
||||||
fmt.Println("未登录账号")
|
fmt.Println("未登录账号")
|
||||||
|
|||||||
@@ -16,20 +16,45 @@ package command
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/tickstep/aliyunpan-api/aliyunpan"
|
"github.com/tickstep/aliyunpan-api/aliyunpan"
|
||||||
|
"github.com/tickstep/aliyunpan-api/aliyunpan/apierror"
|
||||||
|
"github.com/tickstep/aliyunpan/cmder/cmdliner"
|
||||||
"github.com/tickstep/aliyunpan/internal/config"
|
"github.com/tickstep/aliyunpan/internal/config"
|
||||||
|
"github.com/tickstep/aliyunpan/internal/functions/panlogin"
|
||||||
"github.com/tickstep/aliyunpan/internal/plugins"
|
"github.com/tickstep/aliyunpan/internal/plugins"
|
||||||
"github.com/tickstep/aliyunpan/internal/utils"
|
"github.com/tickstep/aliyunpan/internal/utils"
|
||||||
"github.com/tickstep/library-go/logger"
|
"github.com/tickstep/library-go/logger"
|
||||||
|
"github.com/urfave/cli"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/url"
|
"net/url"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
panCommandVerbose = logger.New("PANCOMMAND", config.EnvVerbose)
|
panCommandVerbose = logger.New("PANCOMMAND", config.EnvVerbose)
|
||||||
|
|
||||||
|
saveConfigMutex *sync.Mutex = new(sync.Mutex)
|
||||||
|
|
||||||
|
ReloadConfigFunc = func(c *cli.Context) error {
|
||||||
|
err := config.Config.Reload()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("重载配置错误: %s\n", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
SaveConfigFunc = func(c *cli.Context) error {
|
||||||
|
saveConfigMutex.Lock()
|
||||||
|
defer saveConfigMutex.Unlock()
|
||||||
|
err := config.Config.Save()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("保存配置错误: %s\n", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
// RunTestShellPattern 执行测试通配符
|
// RunTestShellPattern 执行测试通配符
|
||||||
@@ -160,3 +185,75 @@ func isIncludeFile(pattern string, fileName string) bool {
|
|||||||
func isMatchWildcardPattern(name string) bool {
|
func isMatchWildcardPattern(name string) bool {
|
||||||
return strings.ContainsAny(name, "*") || strings.ContainsAny(name, "?") || strings.ContainsAny(name, "[")
|
return strings.ContainsAny(name, "*") || strings.ContainsAny(name, "?") || strings.ContainsAny(name, "[")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DoLoginHelper 登录助手,使用token进行登录
|
||||||
|
func DoLoginHelper(refreshToken string) (refreshTokenStr string, webToken aliyunpan.WebLoginToken, error error) {
|
||||||
|
line := cmdliner.NewLiner()
|
||||||
|
defer line.Close()
|
||||||
|
|
||||||
|
if refreshToken == "" {
|
||||||
|
refreshToken, error = line.State.Prompt("请输入RefreshToken, 回车键提交 > ")
|
||||||
|
if error != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// app login
|
||||||
|
atoken, apperr := aliyunpan.GetAccessTokenFromRefreshToken(refreshToken)
|
||||||
|
if apperr != nil {
|
||||||
|
if apperr.Code == apierror.ApiCodeTokenExpiredCode || apperr.Code == apierror.ApiCodeRefreshTokenExpiredCode {
|
||||||
|
fmt.Println("Token过期,需要重新登录")
|
||||||
|
} else {
|
||||||
|
fmt.Println("Token登录失败:", apperr)
|
||||||
|
}
|
||||||
|
return "", webToken, fmt.Errorf("登录失败")
|
||||||
|
}
|
||||||
|
refreshTokenStr = refreshToken
|
||||||
|
return refreshTokenStr, *atoken, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// TryLogin 尝试登录,基础应用支持的各类登录方式进行尝试成功登录
|
||||||
|
func TryLogin() *config.PanUser {
|
||||||
|
// can do automatically login?
|
||||||
|
for _, u := range config.Config.UserList {
|
||||||
|
if u.UserId == config.Config.ActiveUID {
|
||||||
|
// login
|
||||||
|
_, webToken, err := DoLoginHelper(u.RefreshToken)
|
||||||
|
if err != nil {
|
||||||
|
logger.Verboseln("automatically login use saved refresh token error ", err)
|
||||||
|
if u.TokenId != "" {
|
||||||
|
logger.Verboseln("try to login use tokenId")
|
||||||
|
h := panlogin.NewLoginHelper(config.DefaultTokenServiceWebHost)
|
||||||
|
r, e := h.GetRefreshToken(u.TokenId)
|
||||||
|
if e != nil {
|
||||||
|
logger.Verboseln("try to login use tokenId error", e)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
refreshToken, e := h.ParseSecureRefreshToken("", r.SecureRefreshToken)
|
||||||
|
if e != nil {
|
||||||
|
logger.Verboseln("try to parse refresh token error", e)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
_, webToken, err = DoLoginHelper(refreshToken)
|
||||||
|
if err != nil {
|
||||||
|
logger.Verboseln("try to use refresh token from tokenId error", e)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
fmt.Println("Token重新自动登录成功")
|
||||||
|
// save new refresh token
|
||||||
|
u.RefreshToken = refreshToken
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
// success
|
||||||
|
u.WebToken = webToken
|
||||||
|
|
||||||
|
// save
|
||||||
|
SaveConfigFunc(nil)
|
||||||
|
// reload
|
||||||
|
ReloadConfigFunc(nil)
|
||||||
|
return config.Config.ActiveUser()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -30,8 +30,8 @@ func CmdWebdav() cli.Command {
|
|||||||
Usage: "在线网盘服务",
|
Usage: "在线网盘服务",
|
||||||
Description: "webdav在线网盘服务",
|
Description: "webdav在线网盘服务",
|
||||||
Category: "阿里云盘",
|
Category: "阿里云盘",
|
||||||
Before: cmder.ReloadConfigFunc,
|
Before: ReloadConfigFunc,
|
||||||
After: cmder.SaveConfigFunc,
|
After: SaveConfigFunc,
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
fmt.Print(`
|
fmt.Print(`
|
||||||
本文命令可以让阿里云盘变身为webdav协议的文件服务器。这样你可以把阿里云盘挂载为Windows、Linux、Mac系统的磁盘,可以通过NAS系统做文件管理或文件同步等等。
|
本文命令可以让阿里云盘变身为webdav协议的文件服务器。这样你可以把阿里云盘挂载为Windows、Linux、Mac系统的磁盘,可以通过NAS系统做文件管理或文件同步等等。
|
||||||
|
|||||||
@@ -75,16 +75,16 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func checkLoginExpiredAndRelogin() {
|
func checkLoginExpiredAndRelogin() {
|
||||||
cmder.ReloadConfigFunc(nil)
|
command.ReloadConfigFunc(nil)
|
||||||
activeUser := config.Config.ActiveUser()
|
activeUser := config.Config.ActiveUser()
|
||||||
if activeUser == nil || activeUser.UserId == "" {
|
if activeUser == nil || activeUser.UserId == "" {
|
||||||
// maybe expired, try to login
|
// maybe expired, try to login
|
||||||
cmder.TryLogin()
|
command.TryLogin()
|
||||||
} else {
|
} else {
|
||||||
// 刷新过期Token并保存到配置文件
|
// 刷新过期Token并保存到配置文件
|
||||||
command.RefreshTokenInNeed(activeUser)
|
command.RefreshTokenInNeed(activeUser)
|
||||||
}
|
}
|
||||||
cmder.SaveConfigFunc(nil)
|
command.SaveConfigFunc(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -301,7 +301,7 @@ func main() {
|
|||||||
fmt.Printf("提示: 输入 help 获取帮助.\n")
|
fmt.Printf("提示: 输入 help 获取帮助.\n")
|
||||||
|
|
||||||
// check update
|
// check update
|
||||||
cmder.ReloadConfigFunc(c)
|
command.ReloadConfigFunc(c)
|
||||||
if config.Config.UpdateCheckInfo.LatestVer != "" {
|
if config.Config.UpdateCheckInfo.LatestVer != "" {
|
||||||
if utils.ParseVersionNum(config.Config.UpdateCheckInfo.LatestVer) > utils.ParseVersionNum(config.AppVersion) {
|
if utils.ParseVersionNum(config.Config.UpdateCheckInfo.LatestVer) > utils.ParseVersionNum(config.AppVersion) {
|
||||||
fmt.Printf("\n当前的软件版本为:%s, 现在有新版本 %s 可供更新,强烈推荐进行更新!(可以输入 update 命令进行更新)\n\n",
|
fmt.Printf("\n当前的软件版本为:%s, 现在有新版本 %s 可供更新,强烈推荐进行更新!(可以输入 update 命令进行更新)\n\n",
|
||||||
@@ -322,7 +322,7 @@ func main() {
|
|||||||
config.Config.UpdateCheckInfo.CheckTime = nowTime
|
config.Config.UpdateCheckInfo.CheckTime = nowTime
|
||||||
|
|
||||||
// save
|
// save
|
||||||
cmder.SaveConfigFunc(c)
|
command.SaveConfigFunc(c)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@@ -333,7 +333,7 @@ func main() {
|
|||||||
)
|
)
|
||||||
|
|
||||||
if activeUser == nil {
|
if activeUser == nil {
|
||||||
activeUser = cmder.TryLogin()
|
activeUser = command.TryLogin()
|
||||||
}
|
}
|
||||||
|
|
||||||
if activeUser != nil && activeUser.Nickname != "" {
|
if activeUser != nil && activeUser.Nickname != "" {
|
||||||
|
|||||||
Reference in New Issue
Block a user