You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ls.go 2.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package userspace
  2. import (
  3. "fmt"
  4. "strconv"
  5. "github.com/jedib0t/go-pretty/v6/table"
  6. "github.com/spf13/cobra"
  7. cliapi "gitlink.org.cn/cloudream/jcs-pub/client/sdk/api/v1"
  8. jcstypes "gitlink.org.cn/cloudream/jcs-pub/common/types"
  9. "gitlink.org.cn/cloudream/jcs-pub/jcsctl/cmd"
  10. )
  11. func init() {
  12. var opt lsOpt
  13. cmd := cobra.Command{
  14. Use: "ls",
  15. Args: cobra.MaximumNArgs(1),
  16. RunE: func(c *cobra.Command, args []string) error {
  17. ctx := cmd.GetCmdCtx(c)
  18. return ls(c, ctx, opt, args)
  19. },
  20. }
  21. cmd.Flags().BoolVarP(&opt.ByID, "id", "", false, "按id查询")
  22. cmd.Flags().BoolVarP(&opt.ShowPassword, "password", "p", false, "显示密码信息,请在安全环境下使用")
  23. UserSpaceCmd.AddCommand(&cmd)
  24. }
  25. type lsOpt struct {
  26. ByID bool
  27. ShowPassword bool
  28. }
  29. func ls(c *cobra.Command, ctx *cmd.CommandContext, opt lsOpt, args []string) error {
  30. // 仅ls无参数
  31. if len(args) == 0 {
  32. resp, err := ctx.Client.UserSpace().GetAll()
  33. if err != nil {
  34. return err
  35. }
  36. fmt.Printf("total: %d\n", len(resp.UserSpaces))
  37. tb := table.NewWriter()
  38. tb.AppendHeader(table.Row{"ID", "Name", "StorageType"})
  39. for _, userSpace := range resp.UserSpaces {
  40. tb.AppendRow(table.Row{userSpace.UserSpaceID, userSpace.Name, userSpace.Storage.GetStorageType()})
  41. }
  42. fmt.Println(tb.Render())
  43. return nil
  44. }
  45. searchKey := args[0]
  46. var userSpace *jcstypes.UserSpace
  47. if opt.ByID {
  48. id, err := strconv.Atoi(searchKey)
  49. if err != nil {
  50. return fmt.Errorf("ID必须是数字")
  51. }
  52. result, err := ctx.Client.UserSpace().Get(cliapi.UserSpaceGet{
  53. UserSpaceID: jcstypes.UserSpaceID(id),
  54. })
  55. if err != nil {
  56. return err
  57. }
  58. userSpace = &result.UserSpace
  59. } else {
  60. result, err := ctx.Client.UserSpace().GetByName(cliapi.UserSpaceGetByName{
  61. Name: searchKey,
  62. })
  63. if err != nil {
  64. return err
  65. }
  66. userSpace = &result.UserSpace
  67. }
  68. if userSpace == nil {
  69. return fmt.Errorf("未找到匹配的云存储: %s", searchKey)
  70. }
  71. fmt.Println("\n\033[1;36m云存储详情\033[0m")
  72. fmt.Println("----------------------------------")
  73. fmt.Printf("\033[1m%-8s\033[0m %d\n", "ID:", userSpace.UserSpaceID)
  74. fmt.Printf("\033[1m%-8s\033[0m %s\n", "名称:", userSpace.Name)
  75. fmt.Printf("\033[1m%-8s\033[0m %s\n", "类型:", userSpace.Storage.GetStorageType())
  76. fmt.Printf("\033[1m%-8s\033[0m %s\n", "Location:", userSpace.Storage.GetLocation().Location)
  77. if opt.ShowPassword {
  78. fmt.Printf("\033[1m%-8s\033[0m %s\n", "Credential:", userSpace.Credential.String(true))
  79. } else {
  80. fmt.Printf("\033[1m%-8s\033[0m %s\n", "Credential:", userSpace.Credential.String(false))
  81. }
  82. if len(userSpace.Features) > 0 {
  83. fmt.Printf("\033[1m%-8s\033[0m %s\n", "Features:", userSpace.Features[0].GetFeatureType())
  84. }
  85. fmt.Printf("\033[1m%-8s\033[0m %s\n", "WorkingDir:", userSpace.WorkingDir)
  86. fmt.Println("----------------------------------")
  87. return nil
  88. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。