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.

export.go 2.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package pubshards
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "strconv"
  7. "strings"
  8. "github.com/spf13/cobra"
  9. cliapi "gitlink.org.cn/cloudream/jcs-pub/client/sdk/api/v1"
  10. jcstypes "gitlink.org.cn/cloudream/jcs-pub/common/types"
  11. "gitlink.org.cn/cloudream/jcs-pub/jcsctl/cmd"
  12. )
  13. func init() {
  14. var opt exportOption
  15. c := &cobra.Command{
  16. Use: "export <package_path>",
  17. Short: "export package object metadata as a file",
  18. Args: cobra.ExactArgs(1),
  19. RunE: func(c *cobra.Command, args []string) error {
  20. ctx := cmd.GetCmdCtx(c)
  21. return export(c, ctx, opt, args)
  22. },
  23. }
  24. PubShardsCmd.AddCommand(c)
  25. c.Flags().BoolVarP(&opt.UseID, "id", "i", false, "treat first argumnet as package ID instead of package path")
  26. c.Flags().StringArrayVarP(&opt.PubShardsNames, "pubshards", "p", nil, "pub shards name")
  27. c.Flags().StringVarP(&opt.OutputPath, "output", "o", "", "output file path")
  28. }
  29. type exportOption struct {
  30. UseID bool
  31. PubShardsNames []string
  32. OutputPath string
  33. }
  34. func export(c *cobra.Command, ctx *cmd.CommandContext, opt exportOption, args []string) error {
  35. if len(opt.PubShardsNames) == 0 {
  36. return fmt.Errorf("you must specify at least one userspace name")
  37. }
  38. var pkgID jcstypes.PackageID
  39. if opt.UseID {
  40. id, err := strconv.ParseInt(args[0], 10, 64)
  41. if err != nil {
  42. return fmt.Errorf("invalid package ID: %s", args[0])
  43. }
  44. pkgID = jcstypes.PackageID(id)
  45. } else {
  46. comps := strings.Split(args[0], "/")
  47. if len(comps) != 2 {
  48. return fmt.Errorf("invalid package path: %s", args[0])
  49. }
  50. resp, err := ctx.Client.Package().GetByFullName(cliapi.PackageGetByFullName{
  51. BucketName: comps[0],
  52. PackageName: comps[1],
  53. })
  54. if err != nil {
  55. return fmt.Errorf("get package by full name: %w", err)
  56. }
  57. pkgID = resp.Package.PackageID
  58. }
  59. _, err := ctx.Client.Package().Pin(cliapi.PackagePin{
  60. PackageID: pkgID,
  61. Pin: true,
  62. })
  63. if err != nil {
  64. return fmt.Errorf("pin package: %w", err)
  65. }
  66. var pubIDs []jcstypes.PubShardsID
  67. for _, name := range opt.PubShardsNames {
  68. resp, err := ctx.Client.PubShards().Get(cliapi.PubShardsGet{
  69. Name: name,
  70. })
  71. if err != nil {
  72. return fmt.Errorf("get user space %v by name: %w", name, err)
  73. }
  74. pubIDs = append(pubIDs, resp.PubShards.PubShardsID)
  75. }
  76. resp, err := ctx.Client.PubShards().ExportPackage(cliapi.PubShardsExportPackage{
  77. PackageID: pkgID,
  78. AvailablePubShards: pubIDs,
  79. })
  80. if err != nil {
  81. return fmt.Errorf("export package: %w", err)
  82. }
  83. outputPath := opt.OutputPath
  84. if outputPath == "" {
  85. outputPath = resp.FileName
  86. }
  87. outputFile, err := os.Create(outputPath)
  88. if err != nil {
  89. return fmt.Errorf("create output file: %w", err)
  90. }
  91. defer outputFile.Close()
  92. _, err = io.Copy(outputFile, resp.PackFile)
  93. if err != nil {
  94. return fmt.Errorf("write output file: %w", err)
  95. }
  96. fmt.Printf("Package %v exported to %v, which also set pinned to true\n", pkgID, outputPath)
  97. return nil
  98. }

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