package ls import ( "fmt" "strings" "github.com/spf13/cobra" "gitlink.org.cn/cloudream/jcs-pub/jcsctl/cmd" ) func init() { var opt option c := &cobra.Command{ Use: "ls [bucket_name]/[package_name]:[object_path]", Short: "download package all files to local disk", Args: cobra.MaximumNArgs(1), RunE: func(c *cobra.Command, args []string) error { ctx := cmd.GetCmdCtx(c) return ls(c, ctx, opt, args) }, } c.Flags().Int64Var(&opt.BucketID, "bid", 0, "bucket id, if set, you should not set any path") c.Flags().Int64Var(&opt.PackageID, "pid", 0, "package id, if set, you should not set /") c.Flags().BoolVarP(&opt.Recursive, "recursive", "r", false, "list all files in package recursively, only valid when list in a package") c.Flags().BoolVarP(&opt.Long, "long", "l", false, "show more details") cmd.RootCmd.AddCommand(c) } type option struct { Long bool BucketID int64 PackageID int64 Recursive bool } func ls(c *cobra.Command, ctx *cmd.CommandContext, opt option, args []string) error { if opt.PackageID != 0 { objPath := "" if len(args) > 0 { objPath = args[0] } return lsObject(ctx, opt, "", "", objPath) } if opt.BucketID != 0 { if len(args) > 0 { return fmt.Errorf("list package objects is not supported when use bucket id") } return lsPackage(ctx, opt, "") } if len(args) == 0 { return lsBucket(ctx, opt) } comps := strings.SplitN(args[0], ":", 2) if len(comps) > 1 { objPath := comps[1] comps = strings.SplitN(comps[0], "/", 2) if len(comps) != 2 { return fmt.Errorf("invalid path format, should be /:") } return lsObject(ctx, opt, comps[0], comps[1], objPath) } comps = strings.SplitN(args[0], "/", 2) if len(comps) == 1 { return lsPackage(ctx, opt, comps[0]) } return lsObject(ctx, opt, comps[0], comps[1], "") }