Skip to content

Commit

Permalink
feat: allow duplicate keys when using customQueryParam global flag
Browse files Browse the repository at this point in the history
  • Loading branch information
reubenmiller committed Dec 10, 2024
1 parent 6da3dab commit 53ff499
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
20 changes: 18 additions & 2 deletions pkg/flags/getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ func WithQueryParameters(cmd *cobra.Command, query *QueryTemplate, inputIterator
query.SetVariable(key, url.EscapeQueryString(val))
}
}
case map[string][]string:
// Collect values
for key, values := range v {
encodedValues := make([]string, 0, len(values))
for _, value := range values {
encodedValues = append(encodedValues, url.EscapeQueryString(value))
}
query.SetVariable(key, encodedValues)
}
case AnyString:
query.SetVariable(name, string(v))
default:
Expand Down Expand Up @@ -483,7 +492,9 @@ func WithCustomStringSlice(valuesFunc func() ([]string, error), opts ...string)
dst = ""
}

outputValues := make(map[string]string)
// Support setting multiple values for query parameters
// e.g. foo=bar1&foo=bar2
outputValues := make(map[string][]string)
for _, v := range values {
parts := strings.Split(v, ":")
if len(parts) != 2 {
Expand All @@ -492,7 +503,12 @@ func WithCustomStringSlice(valuesFunc func() ([]string, error), opts ...string)
continue
}
}
outputValues[strings.TrimSpace(parts[0])] = strings.TrimSpace(applyFormatter(format, parts[1]))
key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(applyFormatter(format, parts[1]))
if _, ok := outputValues[key]; !ok {
outputValues[key] = make([]string, 0, 1)
}
outputValues[key] = append(outputValues[key], value)
}

return dst, outputValues, err
Expand Down
10 changes: 10 additions & 0 deletions tests/manual/api/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ tests:
path: /inventory/managedObjects
query: myValue=2&pageSize=1

It adds multiple custom query parameters to an outgoing request with the same key:
command: |
echo "/inventory/managedObjects?pageSize=1" |
c8y api --customQueryParam "myValue=2" --customQueryParam "myValue=3" --customQueryParam other=value
stdout:
json:
method: GET
path: /inventory/managedObjects
query: myValue=2&myValue=3&other=value&pageSize=1

It accepts positional arguments for method and path (not using pipeline):
command: |
c8y api GET "/alarm/alarms?pageSize=10&status=ACTIVE"
Expand Down

0 comments on commit 53ff499

Please sign in to comment.