|
- package ls
-
- import (
- "fmt"
-
- "github.com/jedib0t/go-pretty/v6/table"
- cliapi "gitlink.org.cn/cloudream/jcs-pub/client/sdk/api/v1"
- clitypes "gitlink.org.cn/cloudream/jcs-pub/client/types"
- "gitlink.org.cn/cloudream/jcs-pub/jcsctl/cmd"
- )
-
- func lsObject(ctx *cmd.CommandContext, opt option, bktName string, pkgName string, objPath string) error {
- var pkgID clitypes.PackageID
- if opt.PackageID != 0 {
- pkgID = clitypes.PackageID(opt.PackageID)
- } else {
- resp, err := ctx.Client.Package().GetByFullName(cliapi.PackageGetByFullName{
- BucketName: bktName,
- PackageName: pkgName,
- })
- if err != nil {
- return fmt.Errorf("get package %v: %w", pkgName, err)
- }
- pkgID = resp.Package.PackageID
- }
-
- var objs []clitypes.Object
- var commonPrefixes []string
-
- req := cliapi.ObjectListByPath{
- PackageID: pkgID,
- Path: objPath,
- IsPrefix: true,
- }
- if !opt.Recursive {
- req.NoRecursive = true
- }
-
- var nextConToken string
- for {
- req.ContinuationToken = nextConToken
- resp, err := ctx.Client.Object().ListByPath(req)
- if err != nil {
- return fmt.Errorf("list objects: %w", err)
- }
-
- objs = append(objs, resp.Objects...)
- commonPrefixes = append(commonPrefixes, resp.CommonPrefixes...)
-
- if !resp.IsTruncated {
- break
- }
-
- nextConToken = resp.NextContinuationToken
- }
-
- if opt.Long {
- fmt.Printf("total %d objects, %d common prefixes in package %v\n", len(objs), len(commonPrefixes), pkgName)
- }
-
- if len(commonPrefixes) > 0 {
- for _, prefix := range commonPrefixes {
- fmt.Printf("%s\n", prefix)
- }
- fmt.Printf("\n")
- }
-
- if len(objs) > 0 {
- if opt.Long {
- tb := table.NewWriter()
- tb.AppendHeader(table.Row{"ID", "Path", "Size", "Hash", "Redundancy", "Create Time", "Update Time"})
- for _, obj := range objs {
- tb.AppendRow(table.Row{
- obj.ObjectID,
- obj.Path,
- obj.Size,
- obj.FileHash,
- obj.Redundancy.GetRedundancyType(),
- obj.CreateTime,
- obj.UpdateTime,
- })
- }
- fmt.Println(tb.Render())
- } else {
- for _, obj := range objs {
- fmt.Printf("%s\n", obj.Path)
- }
- }
- }
-
- return nil
- }
|