Skip to content

Commit 8cb357b

Browse files
richardltyesnault
authored andcommitted
fix(cdsctl): fix fields filter for list (#3872)
1 parent abc8a3c commit 8cb357b

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

cli/cobra.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ func newCommand(c Command, run interface{}, subCommands []*cobra.Command, mods .
299299
quiet, _ := cmd.Flags().GetBool("quiet")
300300
verbose, _ := cmd.Flags().GetBool("verbose")
301301
filter, _ := cmd.Flags().GetString("filter")
302+
fields, _ := cmd.Flags().GetString("fields")
302303
var filters = make(map[string]string)
303304
if filter != "" {
304305
t := strings.Split(filter, " ")
@@ -320,7 +321,11 @@ func newCommand(c Command, run interface{}, subCommands []*cobra.Command, mods .
320321
allResult := []map[string]string{}
321322

322323
for _, i := range s {
323-
item := listItem(i, filters, quiet, nil, verbose, map[string]string{})
324+
var fs []string
325+
if fields != "" {
326+
fs = strings.Split(fields, ",")
327+
}
328+
item := listItem(i, filters, quiet, fs, verbose, map[string]string{})
324329
if len(item) == 0 {
325330
continue
326331
}
@@ -491,7 +496,7 @@ func listItem(i interface{}, filters map[string]string, quiet bool, fields []str
491496

492497
if len(fields) > 0 {
493498
for _, ff := range fields {
494-
if ff == tag {
499+
if ff == tag || strings.ToUpper(ff) == tag || strings.ToLower(ff) == tag {
495500
res[tag] = fmt.Sprintf("%v", f.Interface())
496501
continue
497502
}

cli/cobra_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ func TestListItem(t *testing.T) {
1919
ProjectID: 1,
2020
}
2121

22-
myResult := listItem(keyProject, nil, false, nil, false, map[string]string{})
23-
assert.Equal(t, len(myResult), 3)
22+
result := listItem(keyProject, nil, false, nil, false, map[string]string{})
23+
assert.Equal(t, 3, len(result))
24+
25+
result = listItem(keyProject, nil, false, []string{"name"}, false, map[string]string{})
26+
assert.Equal(t, map[string]string{"name": "myKey"}, result)
27+
28+
result = listItem(keyProject, nil, false, []string{"NAME"}, false, map[string]string{})
29+
assert.Equal(t, map[string]string{"name": "myKey"}, result)
2430
}

0 commit comments

Comments
 (0)