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.

import.go 2.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package pubshards
  2. import (
  3. "fmt"
  4. "os"
  5. "strconv"
  6. "strings"
  7. "github.com/spf13/cobra"
  8. "gitlink.org.cn/cloudream/common/sdks"
  9. cliapi "gitlink.org.cn/cloudream/jcs-pub/client/sdk/api/v1"
  10. "gitlink.org.cn/cloudream/jcs-pub/common/ecode"
  11. jcstypes "gitlink.org.cn/cloudream/jcs-pub/common/types"
  12. "gitlink.org.cn/cloudream/jcs-pub/jcsctl/cmd"
  13. )
  14. func init() {
  15. var opt import2Option
  16. c := &cobra.Command{
  17. Use: "import <local_file> <package_path>",
  18. Short: "import package object metadata from local file",
  19. Args: cobra.ExactArgs(2),
  20. RunE: func(c *cobra.Command, args []string) error {
  21. ctx := cmd.GetCmdCtx(c)
  22. return import2(c, ctx, opt, args)
  23. },
  24. }
  25. PubShardsCmd.AddCommand(c)
  26. c.Flags().BoolVarP(&opt.UseID, "id", "i", false, "treat first argumnet as package ID instead of package path")
  27. c.Flags().BoolVar(&opt.Create, "create", false, "create package if not exists")
  28. }
  29. type import2Option struct {
  30. UseID bool
  31. Create bool
  32. }
  33. func import2(c *cobra.Command, ctx *cmd.CommandContext, opt import2Option, args []string) error {
  34. var pkgID jcstypes.PackageID
  35. if opt.UseID {
  36. id, err := strconv.ParseInt(args[1], 10, 64)
  37. if err != nil {
  38. return fmt.Errorf("invalid package ID %v: %w", args[1], err)
  39. }
  40. pkgID = jcstypes.PackageID(id)
  41. } else {
  42. comps := strings.Split(args[1], "/")
  43. resp, err := ctx.Client.Package().GetByFullName(cliapi.PackageGetByFullName{
  44. BucketName: comps[0],
  45. PackageName: comps[1],
  46. })
  47. if err != nil {
  48. if !sdks.IsErrorCode(err, string(ecode.DataNotFound)) {
  49. return err
  50. }
  51. if !opt.Create {
  52. return fmt.Errorf("package not found")
  53. }
  54. bkt, err := ctx.Client.Bucket().GetByName(cliapi.BucketGetByName{
  55. Name: comps[0],
  56. })
  57. if err != nil {
  58. return fmt.Errorf("get bucket %v: %w", comps[0], err)
  59. }
  60. cpkg, err := ctx.Client.Package().Create(cliapi.PackageCreate{
  61. BucketID: bkt.Bucket.BucketID,
  62. Name: comps[1],
  63. })
  64. if err != nil {
  65. return fmt.Errorf("create package %v: %w", args[1], err)
  66. }
  67. pkgID = cpkg.Package.PackageID
  68. } else {
  69. pkgID = resp.Package.PackageID
  70. }
  71. }
  72. file, err := os.Open(args[0])
  73. if err != nil {
  74. return fmt.Errorf("open file %v: %w", args[0], err)
  75. }
  76. defer file.Close()
  77. resp, err := ctx.Client.PubShards().ImportPackage(cliapi.PubShardsImportPackage{
  78. PackageID: pkgID,
  79. PackFile: file,
  80. })
  81. if err != nil {
  82. return fmt.Errorf("import package: %w", err)
  83. }
  84. if len(resp.InvalidObjects) > 0 {
  85. fmt.Printf("below objects are invalid and will not be imported:\n")
  86. for _, obj := range resp.InvalidObjects {
  87. fmt.Printf("%v\n", obj.Path)
  88. }
  89. }
  90. return nil
  91. }

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