Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cmd/ls-main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ var (
Name: "zip",
Usage: "list files inside zip archive (MinIO servers only)",
},
cli.StringFlag{
Name: "sort",
Usage: "sort by field. Only possible value for now: size",
},
}
)

Expand Down Expand Up @@ -177,6 +181,11 @@ func checkListSyntax(cliCtx *cli.Context) ([]string, doListOptions) {

timeRef := parseRewindFlag(cliCtx.String("rewind"))

sortBy := cliCtx.String("sort")
if sortBy != "" && sortBy != "size" {
fatalIf(errInvalidArgument().Trace(args...), "Unsupported sort option '"+sortBy+"'. Only 'size' is supported.")
}
Comment thread
iTrooz marked this conversation as resolved.
Outdated

if listZip && (withVersions || !timeRef.IsZero()) {
fatalIf(errInvalidArgument().Trace(args...), "Zip file listing can only be performed on the latest version")
}
Expand All @@ -189,6 +198,7 @@ func checkListSyntax(cliCtx *cli.Context) ([]string, doListOptions) {
withVersions: withVersions,
listZip: listZip,
filter: storageClasss,
sortBy: sortBy,
}
return args, opts
}
Expand Down
26 changes: 20 additions & 6 deletions cmd/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"fmt"
"path/filepath"
"slices"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -198,12 +199,10 @@ func (s summaryMessage) JSON() string {
}

// Pretty print the list of versions belonging to one object
func printObjectVersions(clntURL ClientURL, ctntVersions []*ClientContent, printAllVersions bool) {
func getObjectVersions(clntURL ClientURL, ctntVersions []*ClientContent, printAllVersions bool) []contentMessage {
sortObjectVersions(ctntVersions)
msgs := generateContentMessages(clntURL, ctntVersions, printAllVersions)
for _, msg := range msgs {
printMsg(msg)
}
return msgs
}

type doListOptions struct {
Expand All @@ -214,6 +213,7 @@ type doListOptions struct {
withVersions bool
listZip bool
filter string
sortBy string
}

// doList - list all entities inside a folder.
Expand All @@ -226,6 +226,7 @@ func doList(ctx context.Context, clnt Client, o doListOptions) error {
totalObjects int64
)

objects := []contentMessage{}
for content := range clnt.List(ctx, ListOptions{
Recursive: o.isRecursive,
Incomplete: o.isIncomplete,
Expand All @@ -245,9 +246,10 @@ func doList(ctx context.Context, clnt Client, o doListOptions) error {
continue
}

// Check if we have moved to a new object (or if this is another version of the last iteration's object)
if lastPath != content.URL.Path {
// Print any object in the current list before reinitializing it
printObjectVersions(clnt.GetURL(), perObjectVersions, o.withVersions)
objects = append(objects, getObjectVersions(clnt.GetURL(), perObjectVersions, o.withVersions)...)
lastPath = content.URL.Path
perObjectVersions = []*ClientContent{}
}
Expand All @@ -257,7 +259,19 @@ func doList(ctx context.Context, clnt Client, o doListOptions) error {
totalObjects++
}

printObjectVersions(clnt.GetURL(), perObjectVersions, o.withVersions)
objects = append(objects, getObjectVersions(clnt.GetURL(), perObjectVersions, o.withVersions)...)
Comment thread
iTrooz marked this conversation as resolved.
Outdated

// Sort by size if requested
if o.sortBy == "size" {
slices.SortFunc(objects, func(a, b contentMessage) int {
return int(a.Size - b.Size)
Comment thread
iTrooz marked this conversation as resolved.
Outdated
})
}

// Print all objects
for _, obj := range objects {
printMsg(obj)
}

if o.isSummary {
printMsg(summaryMessage{
Expand Down