|
- package userspace
-
- import (
- "fmt"
- "strings"
- "time"
-
- "github.com/spf13/cobra"
- cliapi "gitlink.org.cn/cloudream/jcs-pub/client/sdk/api/v1"
- "gitlink.org.cn/cloudream/jcs-pub/jcsctl/cmd"
- )
-
- func init() {
- var opt getpOpt
- c := &cobra.Command{
- Use: "getp <bucket_name>/<package_name> <space_name>:<root_path>",
- Short: "download package all files to user space",
- Args: cobra.ExactArgs(2),
- RunE: func(c *cobra.Command, args []string) error {
- ctx := cmd.GetCmdCtx(c)
- return getp(c, ctx, opt, args)
- },
- }
- c.Flags().IntVarP(&opt.Concurrency, "concurrency", "c", 5, "concurrency of download files")
- UserSpaceCmd.AddCommand(c)
- }
-
- type getpOpt struct {
- Concurrency int
- }
-
- func getp(c *cobra.Command, ctx *cmd.CommandContext, opt getpOpt, args []string) error {
- comps := strings.Split(args[0], "/")
- if len(comps) != 2 {
- return fmt.Errorf("invalid package name: %s", args[0])
- }
-
- bucketName, packageName := comps[0], comps[1]
-
- comps = strings.Split(args[1], ":")
- if len(comps) != 2 {
- return fmt.Errorf("invalid space name and root path: %s", args[1])
- }
-
- spaceName, rootPath := comps[0], comps[1]
-
- getPkg, err := ctx.Client.Package().GetByFullName(cliapi.PackageGetByFullName{
- BucketName: bucketName,
- PackageName: packageName,
- })
- if err != nil {
- return fmt.Errorf("get package %v: %w", args[0], err)
- }
-
- getSpace, err := ctx.Client.UserSpace().GetByName(cliapi.UserSpaceGetByName{
- Name: spaceName,
- })
- if err != nil {
- return fmt.Errorf("get user space %v: %w", spaceName, err)
- }
-
- startTime := time.Now()
-
- _, err = ctx.Client.UserSpace().DownloadPackage(cliapi.UserSpaceDownloadPackage{
- PackageID: getPkg.Package.PackageID,
- UserSpaceID: getSpace.UserSpace.UserSpaceID,
- RootPath: rootPath,
- Concurrency: opt.Concurrency,
- })
- if err != nil {
- return fmt.Errorf("download package %v to user space %v: %w", args[0], spaceName, err)
- }
-
- dt := time.Since(startTime)
-
- fmt.Printf("download package %v to user space %v success, time: %v\n", args[0], spaceName, dt)
- return nil
- }
|