Skip to content

Commit

Permalink
Pass query params through flags
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed Nov 29, 2022
1 parent 15bb5a9 commit dd53121
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions internal/cli/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ var apiFlags = apiCmdFlags{
IsRequired: false,
AlwaysPrompt: false,
},
QueryParams: Flag{
Name: "QueryParams",
LongForm: "query",
ShortForm: "q",
Help: "Query params to send with the request.",
IsRequired: false,
AlwaysPrompt: false,
},
}

var apiValidMethods = []string{
Expand All @@ -38,16 +46,18 @@ var apiValidMethods = []string{

type (
apiCmdFlags struct {
Data Flag
Data Flag
QueryParams Flag
}

apiCmdInputs struct {
RawMethod string
RawURI string
RawData string
Method string
URL *url.URL
Data io.Reader
RawMethod string
RawURI string
RawData string
RawQueryParams map[string]string
Method string
URL *url.URL
Data io.Reader
}
)

Expand All @@ -69,14 +79,15 @@ The method argument is optional, and when you don’t specify it, the command de
"Auth0 Management API Docs:\n", apiDocsURL,
"Available Methods:\n", strings.Join(apiValidMethods, ", "),
),
Example: `auth0 api "/stats/daily?from=20221101&to=20221118"
Example: `auth0 api "/stats/daily" -q "from=20221101" -q "to=20221118"
auth0 api get "/tenants/settings"
auth0 api clients --data "{\"name\":\"ssoTest\",\"app_type\":\"sso_integration\"}"
`,
RunE: apiCmdRun(cli, &inputs),
}

apiFlags.Data.RegisterString(cmd, &inputs.RawData, "")
apiFlags.QueryParams.RegisterStringMap(cmd, &inputs.RawQueryParams, nil)

return cmd
}
Expand Down Expand Up @@ -172,6 +183,12 @@ func (i *apiCmdInputs) validateAndSetEndpoint(domain string) error {
return fmt.Errorf("invalid uri given: %w", err)
}

params := endpoint.Query()
for key, value := range i.RawQueryParams {
params.Set(key, value)
}
endpoint.RawQuery = params.Encode()

i.URL = endpoint

return nil
Expand Down

0 comments on commit dd53121

Please sign in to comment.