Skip to content

Commit c6c8794

Browse files
committed
do not use an accumulating list when we just want to print results
1 parent 5a3706f commit c6c8794

File tree

1 file changed

+49
-17
lines changed

1 file changed

+49
-17
lines changed

cmd/ls.go

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ type doListOptions struct {
216216
sortBy string
217217
}
218218

219-
// doList - list all entities inside a folder.
220-
func doList(ctx context.Context, clnt Client, o doListOptions) error {
219+
// Will loop over entities listed and call fn() for every object with all their versions
220+
func loopOverObjects(ctx context.Context, clnt Client, fn func(content *[]contentMessage) error, o doListOptions) (int64, int64, error) {
221221
var (
222222
lastPath string
223223
perObjectVersions []*ClientContent
@@ -226,7 +226,6 @@ func doList(ctx context.Context, clnt Client, o doListOptions) error {
226226
totalObjects int64
227227
)
228228

229-
objects := []contentMessage{}
230229
for content := range clnt.List(ctx, ListOptions{
231230
Recursive: o.isRecursive,
232231
Incomplete: o.isIncomplete,
@@ -249,7 +248,8 @@ func doList(ctx context.Context, clnt Client, o doListOptions) error {
249248
// Check if we have moved to a new object (or if this is another version of the last iteration's object)
250249
if lastPath != content.URL.Path {
251250
// Print any object in the current list before reinitializing it
252-
objects = append(objects, getObjectVersions(clnt.GetURL(), perObjectVersions, o.withVersions)...)
251+
objects := getObjectVersions(clnt.GetURL(), perObjectVersions, o.withVersions)
252+
fn(&objects)
253253
lastPath = content.URL.Path
254254
perObjectVersions = []*ClientContent{}
255255
}
@@ -259,23 +259,55 @@ func doList(ctx context.Context, clnt Client, o doListOptions) error {
259259
totalObjects++
260260
}
261261

262-
objects = append(objects, getObjectVersions(clnt.GetURL(), perObjectVersions, o.withVersions)...)
262+
objects := getObjectVersions(clnt.GetURL(), perObjectVersions, o.withVersions)
263+
fn(&objects)
264+
return totalObjects, totalSize, cErr
265+
}
263266

264-
// Sort by size if requested
265-
if o.sortBy == "size" {
266-
slices.SortFunc(objects, func(a, b contentMessage) int {
267-
if a.Size < b.Size {
268-
return -1
269-
} else if a.Size > b.Size {
270-
return 1
267+
// doList - list all entities inside a folder.
268+
func doList(ctx context.Context, clnt Client, o doListOptions) error {
269+
var cErr error
270+
isAccumulating := o.sortBy == "size" // should objects be accumulated instead of printed directly
271+
var accumulatedObjects []contentMessage
272+
273+
var processFn func(objects *[]contentMessage) error
274+
if isAccumulating {
275+
processFn = func(newObjects *[]contentMessage) error {
276+
// Accumulate all objects for sorting later
277+
accumulatedObjects = append(accumulatedObjects, *newObjects...)
278+
return nil
279+
}
280+
} else {
281+
processFn = func(objects *[]contentMessage) error {
282+
// Print objects as they come
283+
for _, obj := range *objects {
284+
printMsg(obj)
271285
}
272-
return 0
273-
})
286+
return nil
287+
}
274288
}
275289

276-
// Print all objects
277-
for _, obj := range objects {
278-
printMsg(obj)
290+
totalObjects, totalSize, cErr := loopOverObjects(ctx, clnt, processFn, o)
291+
292+
// if isAccumulating is true, objects have been accumulated instead of printed
293+
if isAccumulating {
294+
// Sort if requested
295+
switch o.sortBy {
296+
case "size":
297+
slices.SortFunc(accumulatedObjects, func(a, b contentMessage) int {
298+
if a.Size < b.Size {
299+
return -1
300+
} else if a.Size > b.Size {
301+
return 1
302+
}
303+
return 0
304+
})
305+
}
306+
307+
// Print all objects
308+
for _, obj := range accumulatedObjects {
309+
printMsg(obj)
310+
}
279311
}
280312

281313
if o.isSummary {

0 commit comments

Comments
 (0)