forked from minio/mc
/
ls.go
270 lines (230 loc) · 7.69 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright (c) 2015-2022 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"context"
"fmt"
"path/filepath"
"sort"
"strings"
"time"
"github.com/dustin/go-humanize"
json "github.com/minio/colorjson"
"github.com/trinet2005/oss-mc/pkg/probe"
"github.com/trinet2005/oss-pkg/console"
)
// printDate - human friendly formatted date.
const (
printDate = "2006-01-02 15:04:05 MST"
)
// contentMessage container for content message structure.
type contentMessage struct {
Status string `json:"status"`
Filetype string `json:"type"`
Time time.Time `json:"lastModified"`
Size int64 `json:"size"`
Key string `json:"key"`
ETag string `json:"etag"`
URL string `json:"url,omitempty"`
VersionID string `json:"versionId,omitempty"`
VersionOrd int `json:"versionOrdinal,omitempty"`
VersionIndex int `json:"versionIndex,omitempty"`
IsDeleteMarker bool `json:"isDeleteMarker,omitempty"`
StorageClass string `json:"storageClass,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
Tags map[string]string `json:"tags,omitempty"`
}
// String colorized string message.
func (c contentMessage) String() string {
message := console.Colorize("Time", fmt.Sprintf("[%s]", c.Time.Format(printDate)))
message += console.Colorize("Size", fmt.Sprintf("%7s", strings.Join(strings.Fields(humanize.IBytes(uint64(c.Size))), "")))
fileDesc := ""
if c.StorageClass != "" {
message += " " + console.Colorize("SC", c.StorageClass)
}
if c.VersionID != "" {
fileDesc += console.Colorize("VersionID", " "+c.VersionID) + console.Colorize("VersionOrd", fmt.Sprintf(" v%d", c.VersionOrd))
if c.IsDeleteMarker {
fileDesc += console.Colorize("DEL", " DEL")
} else {
fileDesc += console.Colorize("PUT", " PUT")
}
}
fileDesc += " " + c.Key
if c.Filetype == "folder" {
message += console.Colorize("Dir", fileDesc)
} else {
message += console.Colorize("File", fileDesc)
}
return message
}
// JSON jsonified content message.
func (c contentMessage) JSON() string {
c.Status = "success"
jsonMessageBytes, e := json.MarshalIndent(c, "", " ")
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
return string(jsonMessageBytes)
}
// Use OS separator and adds a trailing separator if it is a dir
func getOSDependantKey(path string, isDir bool) string {
sep := "/"
if isDir && !strings.HasSuffix(path, sep) {
return fmt.Sprintf("%s%s", path, sep)
}
return path
}
// get content key
func getKey(c *ClientContent) string {
return getOSDependantKey(c.URL.Path, c.Type.IsDir())
}
// Generate printable listing from a list of sorted client
// contents, the latest created content comes first.
func generateContentMessages(clntURL ClientURL, ctnts []*ClientContent, printAllVersions bool) (msgs []contentMessage) {
prefixPath := clntURL.Path
prefixPath = filepath.ToSlash(prefixPath)
if !strings.HasSuffix(prefixPath, "/") {
prefixPath = prefixPath[:strings.LastIndex(prefixPath, "/")+1]
}
prefixPath = strings.TrimPrefix(prefixPath, "./")
nrVersions := len(ctnts)
for i, c := range ctnts {
// Convert any os specific delimiters to "/".
contentURL := filepath.ToSlash(c.URL.Path)
// Trim prefix path from the content path.
c.URL.Path = strings.TrimPrefix(contentURL, prefixPath)
contentMsg := contentMessage{}
contentMsg.Time = c.Time.Local()
// guess file type.
contentMsg.Filetype = func() string {
if c.Type.IsDir() {
return "folder"
}
return "file"
}()
contentMsg.Size = c.Size
contentMsg.StorageClass = c.StorageClass
contentMsg.Metadata = c.Metadata
contentMsg.Tags = c.Tags
md5sum := strings.TrimPrefix(c.ETag, "\"")
md5sum = strings.TrimSuffix(md5sum, "\"")
contentMsg.ETag = md5sum
// Convert OS Type to match console file printing style.
contentMsg.Key = getKey(c)
contentMsg.VersionID = c.VersionID
contentMsg.IsDeleteMarker = c.IsDeleteMarker
contentMsg.VersionOrd = nrVersions - i
// URL is empty by default
// Set it to either relative dir (host) or public url (remote)
contentMsg.URL = clntURL.String()
msgs = append(msgs, contentMsg)
if !printAllVersions {
break
}
}
return
}
func sortObjectVersions(ctntVersions []*ClientContent) {
// Sort versions
sort.Slice(ctntVersions, func(i, j int) bool {
if ctntVersions[i].IsLatest {
return true
}
if ctntVersions[j].IsLatest {
return false
}
return ctntVersions[i].Time.After(ctntVersions[j].Time)
})
}
// summaryMessage container for summary message structure
type summaryMessage struct {
TotalObjects int64 `json:"totalObjects"`
TotalSize int64 `json:"totalSize"`
}
// String colorized string message
func (s summaryMessage) String() string {
msg := console.Colorize("Summarize", fmt.Sprintf("\nTotal Size: %s", humanize.IBytes(uint64(s.TotalSize))))
msg += "\n" + console.Colorize("Summarize", fmt.Sprintf("Total Objects: %d", s.TotalObjects))
return msg
}
// JSON jsonified summary message
func (s summaryMessage) JSON() string {
jsonMessageBytes, e := json.MarshalIndent(s, "", "")
fatalIf(probe.NewError(e), "Unable to marshal into JSON")
return string(jsonMessageBytes)
}
// Pretty print the list of versions belonging to one object
func printObjectVersions(clntURL ClientURL, ctntVersions []*ClientContent, printAllVersions bool) {
sortObjectVersions(ctntVersions)
msgs := generateContentMessages(clntURL, ctntVersions, printAllVersions)
for _, msg := range msgs {
printMsg(msg)
}
}
type doListOptions struct {
timeRef time.Time
isRecursive bool
isIncomplete bool
isSummary bool
withOlderVersions bool
listZip bool
filter string
}
// doList - list all entities inside a folder.
func doList(ctx context.Context, clnt Client, o doListOptions) error {
var (
lastPath string
perObjectVersions []*ClientContent
cErr error
totalSize int64
totalObjects int64
)
for content := range clnt.List(ctx, ListOptions{
Recursive: o.isRecursive,
Incomplete: o.isIncomplete,
TimeRef: o.timeRef,
WithOlderVersions: o.withOlderVersions || !o.timeRef.IsZero(),
WithDeleteMarkers: true,
ShowDir: DirNone,
ListZip: o.listZip,
}) {
if content.Err != nil {
errorIf(content.Err.Trace(clnt.GetURL().String()), "Unable to list folder.")
cErr = exitStatus(globalErrorExitStatus) // Set the exit status.
continue
}
if content.StorageClass != "" && o.filter != "" && o.filter != "*" && content.StorageClass != o.filter {
continue
}
if lastPath != content.URL.Path {
// Print any object in the current list before reinitializing it
printObjectVersions(clnt.GetURL(), perObjectVersions, o.withOlderVersions)
lastPath = content.URL.Path
perObjectVersions = []*ClientContent{}
}
perObjectVersions = append(perObjectVersions, content)
totalSize += content.Size
totalObjects++
}
printObjectVersions(clnt.GetURL(), perObjectVersions, o.withOlderVersions)
if o.isSummary {
printMsg(summaryMessage{
TotalObjects: totalObjects,
TotalSize: totalSize,
})
}
return cErr
}