|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- package userspace
-
- import (
- "fmt"
- "strconv"
-
- "github.com/jedib0t/go-pretty/v6/table"
- "github.com/spf13/cobra"
- cliapi "gitlink.org.cn/cloudream/jcs-pub/client/sdk/api/v1"
- jcstypes "gitlink.org.cn/cloudream/jcs-pub/common/types"
- "gitlink.org.cn/cloudream/jcs-pub/jcsctl/cmd"
- )
-
- func init() {
- var opt lsOpt
- cmd := cobra.Command{
- Use: "ls",
- Args: cobra.MaximumNArgs(1),
- RunE: func(c *cobra.Command, args []string) error {
- ctx := cmd.GetCmdCtx(c)
- return ls(c, ctx, opt, args)
- },
- }
-
- cmd.Flags().BoolVarP(&opt.ByID, "id", "", false, "按id查询")
- cmd.Flags().BoolVarP(&opt.ShowPassword, "password", "p", false, "显示密码信息,请在安全环境下使用")
- UserSpaceCmd.AddCommand(&cmd)
- }
-
- type lsOpt struct {
- ByID bool
- ShowPassword bool
- }
-
- func ls(c *cobra.Command, ctx *cmd.CommandContext, opt lsOpt, args []string) error {
- // 仅ls无参数
- if len(args) == 0 {
- resp, err := ctx.Client.UserSpace().GetAll()
- if err != nil {
- return err
- }
-
- fmt.Printf("total: %d\n", len(resp.UserSpaces))
- tb := table.NewWriter()
- tb.AppendHeader(table.Row{"ID", "Name", "StorageType"})
- for _, userSpace := range resp.UserSpaces {
- tb.AppendRow(table.Row{userSpace.UserSpaceID, userSpace.Name, userSpace.Storage.GetStorageType()})
- }
- fmt.Println(tb.Render())
- return nil
- }
-
- searchKey := args[0]
- var userSpace *jcstypes.UserSpace
- if opt.ByID {
- id, err := strconv.Atoi(searchKey)
- if err != nil {
- return fmt.Errorf("ID必须是数字")
- }
-
- result, err := ctx.Client.UserSpace().Get(cliapi.UserSpaceGet{
- UserSpaceID: jcstypes.UserSpaceID(id),
- })
- if err != nil {
- return err
- }
- userSpace = &result.UserSpace
-
- } else {
- result, err := ctx.Client.UserSpace().GetByName(cliapi.UserSpaceGetByName{
- Name: searchKey,
- })
- if err != nil {
- return err
- }
- userSpace = &result.UserSpace
- }
-
- if userSpace == nil {
- return fmt.Errorf("未找到匹配的云存储: %s", searchKey)
- }
-
- fmt.Println("\n\033[1;36m云存储详情\033[0m")
- fmt.Println("----------------------------------")
- fmt.Printf("\033[1m%-8s\033[0m %d\n", "ID:", userSpace.UserSpaceID)
- fmt.Printf("\033[1m%-8s\033[0m %s\n", "名称:", userSpace.Name)
- fmt.Printf("\033[1m%-8s\033[0m %s\n", "类型:", userSpace.Storage.GetStorageType())
- fmt.Printf("\033[1m%-8s\033[0m %s\n", "Location:", userSpace.Storage.GetLocation().Location)
- if opt.ShowPassword {
- fmt.Printf("\033[1m%-8s\033[0m %s\n", "Credential:", userSpace.Credential.String(true))
- } else {
- fmt.Printf("\033[1m%-8s\033[0m %s\n", "Credential:", userSpace.Credential.String(false))
- }
-
- if len(userSpace.Features) > 0 {
- fmt.Printf("\033[1m%-8s\033[0m %s\n", "Features:", userSpace.Features[0].GetFeatureType())
- }
- fmt.Printf("\033[1m%-8s\033[0m %s\n", "WorkingDir:", userSpace.WorkingDir)
- fmt.Println("----------------------------------")
-
- return nil
- }
|