forked from tencentyun/coscli
/
ls.go
130 lines (115 loc) · 3.72 KB
/
ls.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package cmd
import (
"coscli/util"
"fmt"
"os"
"github.com/olekukonko/tablewriter"
logger "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/tencentyun/cos-go-sdk-v5"
)
var lsCmd = &cobra.Command{
Use: "ls",
Short: "List buckets or objects",
Long: `List buckets or objects
Format:
./coscli ls cos://<bucket-name>[/prefix/] [flags]
Example:
./coscli ls cos://examplebucket/test/ -r`,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
limit, _ := cmd.Flags().GetInt("limit")
recursive, _ := cmd.Flags().GetBool("recursive")
include, _ := cmd.Flags().GetString("include")
exclude, _ := cmd.Flags().GetString("exclude")
if limit < 0 || limit > 1000 {
logger.Fatalln("Flag --limit should in range 0~1000")
os.Exit(1)
}
// 无参数,则列出当前账号下的所有存储桶
if len(args) == 0 {
listBuckets(limit, include, exclude)
} else if util.IsCosPath(args[0]) {
listObjects(args[0], limit, recursive, include, exclude)
} else {
logger.Fatalln("cospath needs to contain cos://")
}
},
}
func init() {
rootCmd.AddCommand(lsCmd)
lsCmd.Flags().Int("limit", 0, "Limit the number of objects listed(0~1000)")
lsCmd.Flags().BoolP("recursive", "r", false, "List objects recursively")
lsCmd.Flags().String("include", "", "List files that meet the specified criteria")
lsCmd.Flags().String("exclude", "", "Exclude files that meet the specified criteria")
}
func listBuckets(limit int, include string, exclude string) {
c := util.NewClient(&config, ¶m, "")
buckets := util.GetBucketsList(c, limit, include, exclude)
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Bucket Name", "Region", "Create Date"})
for _, b := range buckets {
table.Append([]string{b.Name, b.Region, b.CreationDate})
}
table.SetFooter([]string{"", "Total Buckets: ", fmt.Sprintf("%d", len(buckets))})
table.SetBorder(false)
table.Render()
}
func listObjects(cosPath string, limit int, recursive bool, include string, exclude string) {
bucketName, path := util.ParsePath(cosPath)
c := util.NewClient(&config, ¶m, bucketName)
var dirs []string
var objects []cos.Object
var marker = ""
var isTruncated bool
var nextMarker string
var total int64
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Key", "Type", "Last Modified", "Size"})
table.SetBorder(false)
table.SetAlignment(tablewriter.ALIGN_RIGHT)
if recursive {
for {
table.ClearRows()
objects, isTruncated, nextMarker = util.GetObjectsListRecursiveForLs(c, path, limit, include, exclude, marker)
for _, d := range dirs {
table.Append([]string{d, "DIR", "", ""})
}
for _, v := range objects {
table.Append([]string{v.Key, v.StorageClass, v.LastModified, util.FormatSize(v.Size)})
}
total = total + int64(len(dirs)) + int64(len(objects))
if !isTruncated {
break
}
table.Render()
marker = nextMarker
}
table.ClearRows()
table.SetFooter([]string{"", "", "Total Objects: ", fmt.Sprintf("%d", total)})
table.SetBorder(false)
table.SetAlignment(tablewriter.ALIGN_RIGHT)
table.Render()
} else {
for {
table.ClearRows()
dirs, objects, isTruncated, nextMarker = util.GetObjectsListForLs(c, path, limit, include, exclude, marker)
for _, d := range dirs {
table.Append([]string{d, "DIR", "", ""})
}
for _, v := range objects {
table.Append([]string{v.Key, v.StorageClass, v.LastModified, util.FormatSize(v.Size)})
}
total = total + int64(len(dirs)) + int64(len(objects))
if !isTruncated {
break
}
table.Render()
marker = nextMarker
}
table.SetFooter([]string{"", "", "Total Objects: ", fmt.Sprintf("%d", total)})
table.SetBorder(false)
table.SetAlignment(tablewriter.ALIGN_RIGHT)
table.Render()
}
}