aliyunpan/internal/command/user_info.go

142 lines
3.6 KiB
Go
Raw Normal View History

2021-10-10 10:48:53 +08:00
// 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
//
2024-03-02 00:55:46 +08:00
// http://www.apache.org/licenses/LICENSE-2.0
2021-10-10 10:48:53 +08:00
//
// 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/cmder"
"github.com/tickstep/aliyunpan/internal/config"
"github.com/urfave/cli"
2022-01-05 15:26:43 +08:00
"os"
2021-10-10 10:48:53 +08:00
"strconv"
)
func CmdLoglist() cli.Command {
return cli.Command{
Name: "loglist",
Usage: "列出帐号列表",
Description: "列出所有已登录的阿里账号",
Category: "阿里云盘账号",
2022-12-10 14:07:28 +08:00
Before: ReloadConfigFunc,
2021-10-10 10:48:53 +08:00
Action: func(c *cli.Context) error {
fmt.Println(config.Config.UserList.String())
return nil
},
}
}
func CmdSu() cli.Command {
return cli.Command{
Name: "su",
Usage: "切换阿里账号",
Description: `
切换已登录的阿里账号:
如果运行该条命令没有提供参数, 程序将会列出所有的帐号, 供选择切换.
示例:
aliyunpan su
2024-04-02 09:42:08 +08:00
aliyunpan su <uid>
2021-10-10 10:48:53 +08:00
`,
Category: "阿里云盘账号",
2022-12-10 14:07:28 +08:00
Before: ReloadConfigFunc,
After: SaveConfigFunc,
2021-10-10 10:48:53 +08:00
Action: func(c *cli.Context) error {
if c.NArg() >= 2 {
cli.ShowCommandHelp(c, c.Command.Name)
return nil
}
numLogins := config.Config.NumLogins()
if numLogins == 0 {
fmt.Printf("未设置任何帐号, 不能切换\n")
return nil
}
var (
inputData = c.Args().Get(0)
uid string
)
if c.NArg() == 1 {
// 直接切换
uid = inputData
} else if c.NArg() == 0 {
// 输出所有帐号供选择切换
cli.HandleAction(cmder.App().Command("loglist").Action, c)
// 提示输入 index
var index string
fmt.Printf("输入要切换帐号的 # 值 > ")
_, err := fmt.Scanln(&index)
if err != nil {
return nil
}
if n, err1 := strconv.Atoi(index); err1 == nil && (n-1) >= 0 && (n-1) < numLogins {
uid = config.Config.UserList[n-1].UserId
2021-10-10 10:48:53 +08:00
} else {
fmt.Printf("切换用户失败, 请检查 # 值是否正确\n")
return nil
}
} else {
cli.ShowCommandHelp(c, c.Command.Name)
}
2024-04-02 09:42:08 +08:00
switchedUser, err := config.Config.SwitchUser(uid)
2021-10-10 10:48:53 +08:00
if err != nil {
fmt.Printf("切换用户失败, %s\n", err)
return nil
}
if switchedUser == nil {
2022-12-10 14:07:28 +08:00
switchedUser = TryLogin()
2021-10-10 10:48:53 +08:00
}
if switchedUser != nil {
fmt.Printf("切换用户: %s\n", switchedUser.Nickname)
} else {
fmt.Printf("切换用户失败\n")
}
return nil
},
}
}
func CmdWho() cli.Command {
return cli.Command{
Name: "who",
Usage: "获取当前帐号",
Description: "获取当前帐号的信息",
Category: "阿里云盘账号",
2022-12-10 14:07:28 +08:00
Before: ReloadConfigFunc,
2021-10-10 10:48:53 +08:00
Action: func(c *cli.Context) error {
if config.Config.ActiveUser() == nil {
fmt.Println("未登录账号")
2022-01-05 15:26:43 +08:00
os.Exit(1)
2021-10-10 10:48:53 +08:00
}
activeUser := config.Config.ActiveUser()
cloudName := activeUser.GetDriveById(activeUser.ActiveDriveId).DriveName
user, _ := GetActivePanClient().OpenapiPanClient().GetUserInfo()
thirdParty := "未开通"
if user.ThirdPartyVip {
thirdParty = "已开通(" + user.ThirdPartyVipExpire + ")"
}
fmt.Printf("当前帐号UID: %s, 昵称: %s, 三方权益包: %s, 当前使用网盘:%s\n", activeUser.UserId, activeUser.Nickname, thirdParty, cloudName)
2021-10-10 10:48:53 +08:00
return nil
},
}
}