Skip to content

Commit 0389813

Browse files
committed
sort: fixed sorters to be split by comma
Fixes #714. Sort attributes passed in from the commandline are csv values e.g ```shell $ drive list -sort name_r,modtime ``` The bug previously was that these values were actually never getting split by comma values, in cmd/drive/main. This CL ensures that we do the necessary splitting and whitespace trimming before we make the sorters. It also updates the CLI doc on sort: * Before ```shell $ drive list -sort -sort string sort items in the order * md5. * name. * size. * type. * version ``` * After ```shell $ drive list -sort -sort string sort items by a combination of attributes * modtime. * md5. * name. * size. * type. * version ```
1 parent ad7a6b3 commit 0389813

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/help.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ const (
163163
"\n\t* Are on a low power device"
164164
DescIgnoreConflict = "turns off the conflict resolution safety"
165165
DescIgnoreNameClashes = "ignore name clashes"
166-
DescSort = "sort items in the order\n\t* md5.\n\t* name.\n\t* size.\n\t* type.\n\t* version"
166+
DescSort = "sort items by a combination of attributes\n\t* modtime.\n\t* md5.\n\t* name.\n\t* size.\n\t* type.\n\t* version\ncomma separated e.g modtime,md5_r,name"
167167
DescSkipMime = "skip elements with mimeTypes derived from these extensions"
168168
DescMatchMime = "get elements with the exact mimeTypes derived from extensions"
169169
DescMatchTitle = "elements with matching titles"

src/list.go

+17-4
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,30 @@ type traversalSt struct {
3939
matchQuery *matchQuery
4040
}
4141

42-
func sorters(opts *Options) (sortKeys []string) {
42+
func sorters(opts *Options) []string {
4343
if opts == nil || opts.Meta == nil {
44-
return
44+
return nil
4545
}
4646

4747
meta := *(opts.Meta)
4848
retr, ok := meta[SortKey]
4949
if !ok {
50-
return
50+
return nil
51+
}
52+
53+
// Keys sent it via meta need to be comma split
54+
// first, space trimmed then added.
55+
// See Issue https://github.com/odeke-em/drive/issues/714.
56+
var sortKeys []string
57+
for _, attr := range retr {
58+
splits := strings.Split(attr, ",")
59+
for _, split := range splits {
60+
trimmedAttr := strings.TrimSpace(split)
61+
sortKeys = append(sortKeys, trimmedAttr)
62+
}
5163
}
52-
return retr
64+
65+
return sortKeys
5366
}
5467

5568
func (g *Commands) ListMatches() error {

0 commit comments

Comments
 (0)