From dd53121ecec8e702658d45f4dfcb94e19c54f8e2 Mon Sep 17 00:00:00 2001 From: Sergiu Ghitea Date: Tue, 29 Nov 2022 12:20:25 +0100 Subject: [PATCH] Pass query params through flags --- internal/cli/api.go | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/internal/cli/api.go b/internal/cli/api.go index 822f08ab2..400499d7f 100644 --- a/internal/cli/api.go +++ b/internal/cli/api.go @@ -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{ @@ -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 } ) @@ -69,7 +79,7 @@ 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\"}" `, @@ -77,6 +87,7 @@ auth0 api clients --data "{\"name\":\"ssoTest\",\"app_type\":\"sso_integration\" } apiFlags.Data.RegisterString(cmd, &inputs.RawData, "") + apiFlags.QueryParams.RegisterStringMap(cmd, &inputs.RawQueryParams, nil) return cmd } @@ -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