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.

load.go 2.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package cmdline
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "time"
  7. "github.com/spf13/cobra"
  8. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  9. )
  10. func init() {
  11. var useID bool
  12. cmd := cobra.Command{
  13. Use: "load",
  14. Short: "Load data from CDS to a storage service",
  15. Args: cobra.ExactArgs(3),
  16. Run: func(cmd *cobra.Command, args []string) {
  17. cmdCtx := GetCmdCtx(cmd)
  18. if useID {
  19. pkgID, err := strconv.ParseInt(args[0], 10, 64)
  20. if err != nil {
  21. fmt.Printf("Invalid package ID: %s\n", args[0])
  22. }
  23. stgID, err := strconv.ParseInt(args[1], 10, 64)
  24. if err != nil {
  25. fmt.Printf("Invalid storage ID: %s\n", args[1])
  26. }
  27. loadByID(cmdCtx, cdssdk.PackageID(pkgID), cdssdk.StorageID(stgID), args[2])
  28. } else {
  29. loadByPath(cmdCtx, args[0], args[1], args[2])
  30. }
  31. },
  32. }
  33. cmd.Flags().BoolVarP(&useID, "id", "i", false, "Use ID for both package and storage service instead of their name or path")
  34. rootCmd.AddCommand(&cmd)
  35. }
  36. func loadByPath(cmdCtx *CommandContext, pkgPath string, stgName string, rootPath string) {
  37. userID := cdssdk.UserID(1)
  38. comps := strings.Split(strings.Trim(pkgPath, cdssdk.ObjectPathSeparator), cdssdk.ObjectPathSeparator)
  39. if len(comps) != 2 {
  40. fmt.Printf("Package path must be in format of <bucket>/<package>")
  41. return
  42. }
  43. pkg, err := cmdCtx.Cmdline.Svc.PackageSvc().GetByFullName(userID, comps[0], comps[1])
  44. if err != nil {
  45. fmt.Println(err)
  46. return
  47. }
  48. stg, err := cmdCtx.Cmdline.Svc.StorageSvc().GetByName(userID, stgName)
  49. if err != nil {
  50. fmt.Println(err)
  51. return
  52. }
  53. loadByID(cmdCtx, pkg.PackageID, stg.StorageID, rootPath)
  54. }
  55. func loadByID(cmdCtx *CommandContext, pkgID cdssdk.PackageID, stgID cdssdk.StorageID, rootPath string) {
  56. userID := cdssdk.UserID(1)
  57. startTime := time.Now()
  58. err := cmdCtx.Cmdline.Svc.StorageSvc().LoadPackage(userID, pkgID, stgID, rootPath)
  59. if err != nil {
  60. fmt.Println(err)
  61. return
  62. }
  63. fmt.Printf("Package loaded to: %v:%v in %v\n", stgID, rootPath, time.Since(startTime))
  64. }

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