|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- 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 option
- 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().StringVar(&opt.Prefix, "prefix", "", "download objects with this prefix")
- // c.Flags().StringVar(&opt.NewPrefix, "new", "", "replace prefix specified by --prefix with this prefix")
- // c.Flags().BoolVar(&opt.Zip, "zip", false, "download as zip file")
- // c.Flags().StringVarP(&opt.Output, "output", "o", "", "output zip file name")
- UserSpaceCmd.AddCommand(c)
- }
-
- type option struct {
- // Prefix string
- // NewPrefix string
- // Zip bool
- // Output string
- }
-
- func getp(c *cobra.Command, ctx *cmd.CommandContext, opt option, 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.UserSpaceDownloadPackageReq{
- PackageID: getPkg.Package.PackageID,
- UserSpaceID: getSpace.UserSpace.UserSpaceID,
- RootPath: rootPath,
- })
- 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
- }
|