-
Notifications
You must be signed in to change notification settings - Fork 20
/
sos_list.go
233 lines (192 loc) · 5.58 KB
/
sos_list.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package cmd
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
"text/tabwriter"
humanize "github.com/dustin/go-humanize"
minio "github.com/minio/minio-go/v6"
"github.com/spf13/cobra"
)
const (
printDate = "2006-01-02 15:04:05 MST"
)
// sosListCmd represents the list command
var sosListCmd = &cobra.Command{
Use: "list [<bucket name>/path]",
Short: "List file and folder",
Aliases: gListAlias,
RunE: func(cmd *cobra.Command, args []string) error {
minioClient, err := newMinioClient(sosZone)
if err != nil {
log.Fatal(err)
}
isRecursive, err := cmd.Flags().GetBool("recursive")
if err != nil {
return err
}
isShort, err := cmd.Flags().GetBool("short")
if err != nil {
return err
}
if len(args) == 0 {
return displayBucket(minioClient, isRecursive, isShort)
}
path := filepath.ToSlash(args[0])
path = strings.Trim(path, "/")
p := splitPath(args[0])
if len(p) == 0 || p[0] == "" {
return displayBucket(minioClient, isRecursive, isShort)
}
var prefix string
if len(p) > 1 {
prefix = path[len(p[0]):]
prefix = strings.Trim(prefix, "/")
}
bucketName := p[0]
///XXX waiting for pithos 301 redirect
location, err := minioClient.GetBucketLocation(bucketName)
if err != nil {
return err
}
minioClient, err = newMinioClient(location)
if err != nil {
return err
}
///
table := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.TabIndent)
if isRecursive {
listRecursively(minioClient, bucketName, prefix, "", false, isShort, table)
return table.Flush()
}
recursive := true
last := ""
for message := range minioClient.ListObjectsV2(bucketName, prefix, recursive, gContext.Done()) {
if message.Err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", message.Err)
continue
}
sPrefix := splitPath(prefix)
sKey := splitPath(message.Key)
// dont display if prefix == object path or folder
if isPrefix(prefix, message.Key) {
continue
}
// skip if message key contain prefix and is not equal
if len(prefix) != len(strings.Join(sKey[:len(sPrefix)], "/")) {
continue
}
// detect all file in current work dir (keep folder using last var)
if len(sKey) > len(sPrefix)+1 && sKey[len(sPrefix)] == last {
last = sKey[len(sPrefix)]
continue
}
last = sKey[len(sPrefix)]
// if is a folder (format key)
if len(sKey) > len(sPrefix)+1 {
message.Key = strings.Join([]string{prefix, sKey[len(sPrefix)]}, "/") + "/"
message.Size = 0
}
lastModified := message.LastModified.Format(printDate)
key := filepath.ToSlash(message.Key)
key = strings.TrimLeft(key[len(prefix):], "/")
if isShort {
fmt.Fprintln(table, key) // nolint: errcheck
continue
}
fmt.Fprintf(table, "[%s]\t%6s \t%s\n", lastModified, humanize.IBytes(uint64(message.Size)), key) // nolint: errcheck
}
return table.Flush()
},
}
func listRecursively(c *minio.Client, bucketName, prefix, zone string, displayBucket, isShort bool, table io.Writer) {
for message := range c.ListObjectsV2(bucketName, prefix, true, gContext.Done()) {
if message.Err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", message.Err)
continue
}
sPrefix := splitPath(prefix)
sKey := splitPath(message.Key)
if len(prefix) != len(strings.Join(sKey[:len(sPrefix)], "/")) {
continue
}
lastModified := message.LastModified.Format(printDate)
var bucket string
var zoneFormat string
if displayBucket {
bucket = fmt.Sprintf("%s/", bucketName)
zoneFormat = fmt.Sprintf("[%s]\t", zone)
}
if isShort {
fmt.Fprintf(table, "%s%s\n", bucket, message.Key) // nolint: errcheck
continue
}
fmt.Fprintf(table,
"[%s]\t%s%6s \t%s%s\n", lastModified, zoneFormat, humanize.IBytes(uint64(message.Size)), bucket, message.Key) // nolint: errcheck
}
}
func displayBucket(minioClient *minio.Client, isRecursive, isShort bool) error {
allBuckets, err := listBucket(minioClient)
if err != nil {
return err
}
table := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.TabIndent)
for zoneName, buckets := range allBuckets {
for _, bucket := range buckets {
if isShort {
fmt.Fprintf(table, "%s/\n", bucket.Name) // nolint: errcheck
} else {
fmt.Fprintf(table,
"[%s]\t[%s]\t%6s \t%s/\n", bucket.CreationDate.Format(printDate), zoneName, humanize.IBytes(uint64(0)), bucket.Name) // nolint: errcheck
}
if isRecursive {
minioClient, err = newMinioClient(zoneName)
if err != nil {
return err
}
listRecursively(minioClient, bucket.Name, "", zoneName, true, isShort, table)
}
}
}
return table.Flush()
}
func listBucket(minioClient *minio.Client) (map[string][]minio.BucketInfo, error) {
bucketInfos, err := minioClient.ListBuckets()
if err != nil {
return nil, err
}
res := map[string][]minio.BucketInfo{}
for _, bucketInfo := range bucketInfos {
bucketLocation, err := minioClient.GetBucketLocation(bucketInfo.Name)
if err != nil {
return nil, err
}
if _, ok := res[bucketLocation]; !ok {
res[bucketLocation] = []minio.BucketInfo{bucketInfo}
continue
}
res[bucketLocation] = append(res[bucketLocation], bucketInfo)
}
return res, nil
}
func isPrefix(prefix, file string) bool {
prefix = strings.Trim(prefix, "/")
file = strings.Trim(file, "/")
return prefix == file
}
func splitPath(s string) []string {
path := filepath.ToSlash(s)
path = strings.Trim(path, "/")
if path == "" {
return nil
}
return strings.Split(path, "/")
}
func init() {
sosCmd.AddCommand(sosListCmd)
sosListCmd.Flags().BoolP("recursive", "r", false, "List recursively")
sosListCmd.Flags().BoolP("short", "S", false, "List in short format")
}