Skip to content

Commit 7731afd

Browse files
authored
Merge branch 'main' into rs256-default
2 parents 761a842 + 732b4b6 commit 7731afd

File tree

4 files changed

+159
-69
lines changed

4 files changed

+159
-69
lines changed

internal/cli/apis.go

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,28 @@ import (
1313

1414
const (
1515
apiID = "id"
16-
apiName = "name"
17-
apiIdentifier = "identifier"
1816
apiScopes = "scopes"
1917
)
2018

19+
var (
20+
apiName = Flag{
21+
Name: "Name",
22+
LongForm: "name",
23+
ShortForm: "n",
24+
DefaultValue: "",
25+
Help: "Name of the API.",
26+
IsRequired: true,
27+
}
28+
apiIdentifier = Flag{
29+
Name: "Identifier",
30+
LongForm: "identifier",
31+
ShortForm: "i",
32+
DefaultValue: "",
33+
Help: "Identifier of the API. Cannot be changed once set.",
34+
IsRequired: true,
35+
}
36+
)
37+
2138
func apisCmd(cli *cli) *cobra.Command {
2239
cmd := &cobra.Command{
2340
Use: "apis",
@@ -147,26 +164,12 @@ auth0 apis create --name myapi --identifier http://my-api
147164
prepareInteractivity(cmd)
148165
},
149166
RunE: func(cmd *cobra.Command, args []string) error {
150-
if shouldPrompt(cmd, apiName) {
151-
input := prompt.TextInput(
152-
apiName, "Name:",
153-
"Name of the API. You can change the name later in the API settings.",
154-
true)
155-
156-
if err := prompt.AskOne(input, &flags); err != nil {
157-
return fmt.Errorf("An unexpected error occurred: %w", err)
158-
}
167+
if err := apiName.Ask(cmd, &flags.Name); err != nil {
168+
return err
159169
}
160170

161-
if shouldPrompt(cmd, apiIdentifier) {
162-
input := prompt.TextInput(
163-
apiIdentifier, "Identifier:",
164-
"Identifier of the API. Cannot be changed once set.",
165-
true)
166-
167-
if err := prompt.AskOne(input, &flags); err != nil {
168-
return fmt.Errorf("An unexpected error occurred: %w", err)
169-
}
171+
if err := apiIdentifier.Ask(cmd, &flags.Identifier); err != nil {
172+
return err
170173
}
171174

172175
if shouldPrompt(cmd, apiScopes) {
@@ -199,10 +202,9 @@ auth0 apis create --name myapi --identifier http://my-api
199202
},
200203
}
201204

202-
cmd.Flags().StringVarP(&flags.Name, apiName, "n", "", "Name of the API.")
203-
cmd.Flags().StringVarP(&flags.Identifier, apiIdentifier, "i", "", "Identifier of the API.")
205+
apiName.RegisterString(cmd, &flags.Name)
206+
apiIdentifier.RegisterString(cmd, &flags.Identifier)
204207
cmd.Flags().StringVarP(&flags.Scopes, apiScopes, "s", "", "Space-separated list of scopes.")
205-
mustRequireFlags(cmd, apiName, apiIdentifier)
206208

207209
return cmd
208210
}
@@ -240,12 +242,8 @@ auth0 apis update <id> --name myapi
240242
inputs.ID = args[0]
241243
}
242244

243-
if shouldPromptWhenFlagless(cmd, apiName) {
244-
input := prompt.TextInput(apiName, "Name:", "Name of the API.", true)
245-
246-
if err := prompt.AskOne(input, &inputs); err != nil {
247-
return fmt.Errorf("An unexpected error occurred: %w", err)
248-
}
245+
if err := apiName.AskU(cmd, &inputs.Name); err != nil {
246+
return err
249247
}
250248

251249
if shouldPromptWhenFlagless(cmd, apiScopes) {
@@ -289,7 +287,7 @@ auth0 apis update <id> --name myapi
289287
},
290288
}
291289

292-
cmd.Flags().StringVarP(&inputs.Name, apiName, "n", "", "Name of the API.")
290+
apiName.RegisterStringU(cmd, &inputs.Name)
293291
cmd.Flags().StringVarP(&inputs.Scopes, apiScopes, "s", "", "Space-separated list of scopes.")
294292

295293
return cmd

internal/cli/apps.go

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,35 @@ import (
1313
)
1414

1515
const (
16-
appID = "id"
17-
appName = "name"
18-
appType = "type"
19-
appDescription = "description"
16+
appID = "id"
17+
appType = "type"
18+
)
19+
20+
var (
21+
appName = Flag{
22+
Name: "Name",
23+
LongForm: "name",
24+
ShortForm: "n",
25+
DefaultValue: "",
26+
Help: "Name of the application.",
27+
IsRequired: true,
28+
}
29+
appDescription = Flag{
30+
Name: "Description",
31+
LongForm: "description",
32+
ShortForm: "d",
33+
DefaultValue: "",
34+
Help: "Description of the application. Max character count is 140.",
35+
IsRequired: false,
36+
}
37+
appAuthMethod = Flag{
38+
Name: "Auth Method",
39+
LongForm: "auth-method",
40+
ShortForm: "a",
41+
DefaultValue: "",
42+
Help: "Defines the requested authentication method for the token endpoint. Possible values are 'None' (public application without a client secret), 'Post' (application uses HTTP POST parameters) or 'Basic' (application uses HTTP Basic).",
43+
IsRequired: false,
44+
}
2045
)
2146

2247
func appsCmd(cli *cli) *cobra.Command {
@@ -189,15 +214,8 @@ auth0 apps create --name myapp --type [native|spa|regular|m2m]
189214
prepareInteractivity(cmd)
190215
},
191216
RunE: func(cmd *cobra.Command, args []string) error {
192-
if shouldPrompt(cmd, appName) {
193-
input := prompt.TextInput(
194-
appName, "Name:",
195-
"Name of the application. You can change the name later in the application settings.",
196-
true)
197-
198-
if err := prompt.AskOne(input, &flags); err != nil {
199-
return fmt.Errorf("An unexpected error occurred: %w", err)
200-
}
217+
if err := appName.Ask(cmd, &flags.Name); err != nil {
218+
return err
201219
}
202220

203221
if shouldPrompt(cmd, appType) {
@@ -216,12 +234,8 @@ auth0 apps create --name myapp --type [native|spa|regular|m2m]
216234
}
217235
}
218236

219-
if shouldPrompt(cmd, appDescription) {
220-
input := prompt.TextInput(appDescription, "Description:", "Description of the application.", false)
221-
222-
if err := prompt.AskOne(input, &flags); err != nil {
223-
return fmt.Errorf("An unexpected error occurred: %w", err)
224-
}
237+
if err := appDescription.Ask(cmd, &flags.Description); err != nil {
238+
return err
225239
}
226240

227241
a := &management.Client{
@@ -259,20 +273,20 @@ auth0 apps create --name myapp --type [native|spa|regular|m2m]
259273
},
260274
}
261275

262-
cmd.Flags().StringVarP(&flags.Name, "name", "n", "", "Name of the application.")
276+
appName.RegisterString(cmd, &flags.Name)
263277
cmd.Flags().StringVarP(&flags.Type, "type", "t", "", "Type of application:\n"+
264278
"- native: mobile, desktop, CLI and smart device apps running natively.\n"+
265279
"- spa (single page application): a JavaScript front-end app that uses an API.\n"+
266280
"- regular: Traditional web app using redirects.\n"+
267281
"- m2m (machine to machine): CLIs, daemons or services running on your backend.")
268-
cmd.Flags().StringVarP(&flags.Description, "description", "d", "", "Description of the application. Max character count is 140.")
282+
appDescription.RegisterString(cmd, &flags.Description)
269283
cmd.Flags().StringSliceVarP(&flags.Callbacks, "callbacks", "c", nil, "After the user authenticates we will only call back to any of these URLs. You can specify multiple valid URLs by comma-separating them (typically to handle different environments like QA or testing). Make sure to specify the protocol (https://) otherwise the callback may fail in some cases. With the exception of custom URI schemes for native apps, all callbacks should use protocol https://.")
270284
cmd.Flags().StringSliceVarP(&flags.AllowedOrigins, "origins", "o", nil, "Comma-separated list of URLs allowed to make requests from JavaScript to Auth0 API (typically used with CORS). By default, all your callback URLs will be allowed. This field allows you to enter other origins if necessary. You can also use wildcards at the subdomain level (e.g., https://*.contoso.com). Query strings and hash information are not taken into account when validating these URLs.")
271285
cmd.Flags().StringSliceVarP(&flags.AllowedWebOrigins, "web-origins", "w", nil, "Comma-separated list of allowed origins for use with Cross-Origin Authentication, Device Flow, and web message response mode.")
272286
cmd.Flags().StringSliceVarP(&flags.AllowedLogoutURLs, "logout-urls", "l", nil, "Comma-separated list of URLs that are valid to redirect to after logout from Auth0. Wildcards are allowed for subdomains.")
273-
cmd.Flags().StringVarP(&flags.AuthMethod, "auth-method", "a", "", "Defines the requested authentication method for the token endpoint. Possible values are 'None' (public application without a client secret), 'Post' (application uses HTTP POST parameters) or 'Basic' (application uses HTTP Basic).")
287+
appAuthMethod.RegisterString(cmd, &flags.AuthMethod)
274288
cmd.Flags().StringSliceVarP(&flags.Grants, "grants", "g", nil, "List of grant types supported for this application. Can include code, implicit, refresh-token, credentials, password, password-realm, mfa-oob, mfa-otp, mfa-recovery-code, and device-code.")
275-
mustRequireFlags(cmd, appName, appType)
289+
mustRequireFlags(cmd, appType)
276290

277291
return cmd
278292
}
@@ -318,12 +332,8 @@ auth0 apps update <id> --name myapp --type [native|spa|regular|m2m]
318332
inputs.ID = args[0]
319333
}
320334

321-
if shouldPromptWhenFlagless(cmd, appName) {
322-
input := prompt.TextInput(appName, "Name:", "Name of the application", true)
323-
324-
if err := prompt.AskOne(input, &inputs); err != nil {
325-
return fmt.Errorf("An unexpected error occurred: %w", err)
326-
}
335+
if err := appName.AskU(cmd, &inputs.Name); err != nil {
336+
return err
327337
}
328338

329339
if shouldPromptWhenFlagless(cmd, appType) {
@@ -342,12 +352,8 @@ auth0 apps update <id> --name myapp --type [native|spa|regular|m2m]
342352
}
343353
}
344354

345-
if shouldPromptWhenFlagless(cmd, appDescription) {
346-
input := prompt.TextInput(appDescription, "Description:", "Description of the application.", false)
347-
348-
if err := prompt.AskOne(input, &inputs); err != nil {
349-
return fmt.Errorf("An unexpected error occurred: %w", err)
350-
}
355+
if err := appDescription.AskU(cmd, &inputs.Description); err != nil {
356+
return err
351357
}
352358

353359
if shouldPromptWhenFlagless(cmd, "CallbacksString") {
@@ -439,18 +445,18 @@ auth0 apps update <id> --name myapp --type [native|spa|regular|m2m]
439445
},
440446
}
441447

442-
cmd.Flags().StringVarP(&inputs.Name, "name", "n", "", "Name of the application.")
448+
appName.RegisterStringU(cmd, &inputs.Name)
443449
cmd.Flags().StringVarP(&inputs.Type, "type", "t", "", "Type of application:\n"+
444450
"- native: mobile, desktop, CLI and smart device apps running natively.\n"+
445451
"- spa (single page application): a JavaScript front-end app that uses an API.\n"+
446452
"- regular: Traditional web app using redirects.\n"+
447453
"- m2m (machine to machine): CLIs, daemons or services running on your backend.")
448-
cmd.Flags().StringVarP(&inputs.Description, "description", "d", "", "Description of the application. Max character count is 140.")
454+
appDescription.RegisterStringU(cmd, &inputs.Description)
449455
cmd.Flags().StringSliceVarP(&inputs.Callbacks, "callbacks", "c", nil, "After the user authenticates we will only call back to any of these URLs. You can specify multiple valid URLs by comma-separating them (typically to handle different environments like QA or testing). Make sure to specify the protocol (https://) otherwise the callback may fail in some cases. With the exception of custom URI schemes for native apps, all callbacks should use protocol https://.")
450456
cmd.Flags().StringSliceVarP(&inputs.AllowedOrigins, "origins", "o", nil, "Comma-separated list of URLs allowed to make requests from JavaScript to Auth0 API (typically used with CORS). By default, all your callback URLs will be allowed. This field allows you to enter other origins if necessary. You can also use wildcards at the subdomain level (e.g., https://*.contoso.com). Query strings and hash information are not taken into account when validating these URLs.")
451457
cmd.Flags().StringSliceVarP(&inputs.AllowedWebOrigins, "web-origins", "w", nil, "Comma-separated list of allowed origins for use with Cross-Origin Authentication, Device Flow, and web message response mode.")
452458
cmd.Flags().StringSliceVarP(&inputs.AllowedLogoutURLs, "logout-urls", "l", nil, "Comma-separated list of URLs that are valid to redirect to after logout from Auth0. Wildcards are allowed for subdomains.")
453-
cmd.Flags().StringVarP(&inputs.AuthMethod, "auth-method", "a", "", "Defines the requested authentication method for the token endpoint. Possible values are 'None' (public application without a client secret), 'Post' (application uses HTTP POST parameters) or 'Basic' (application uses HTTP Basic).")
459+
appAuthMethod.RegisterStringU(cmd, &inputs.AuthMethod)
454460
cmd.Flags().StringSliceVarP(&inputs.Grants, "grants", "g", nil, "List of grant types supported for this application. Can include code, implicit, refresh-token, credentials, password, password-realm, mfa-oob, mfa-otp, mfa-recovery-code, and device-code.")
455461

456462
return cmd

internal/cli/flags.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/auth0/auth0-cli/internal/prompt"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
type Flag struct {
11+
Name string
12+
LongForm string
13+
ShortForm string
14+
DefaultValue string
15+
Help string
16+
IsRequired bool
17+
}
18+
19+
func (f *Flag) Ask(cmd *cobra.Command, value interface{}) error {
20+
return ask(cmd, f, value, false)
21+
}
22+
23+
func (f *Flag) AskU(cmd *cobra.Command, value interface{}) error {
24+
return ask(cmd, f, value, true)
25+
}
26+
27+
func (f *Flag) RegisterString(cmd *cobra.Command, value *string) {
28+
registerString(cmd, f, value, false)
29+
}
30+
31+
func (f *Flag) RegisterStringU(cmd *cobra.Command, value *string) {
32+
registerString(cmd, f, value, true)
33+
}
34+
35+
func ask(cmd *cobra.Command, f *Flag, value interface{}, isUpdate bool) error {
36+
var shouldAsk bool
37+
38+
if isUpdate {
39+
shouldAsk = shouldPromptWhenFlagless(cmd, f.LongForm)
40+
} else {
41+
shouldAsk = shouldPrompt(cmd, f.LongForm)
42+
}
43+
44+
if shouldAsk {
45+
input := prompt.TextInput("", fmt.Sprintf("%s:", f.Name), f.Help, f.IsRequired)
46+
47+
if err := prompt.AskOne(input, value); err != nil {
48+
return fmt.Errorf("An unexpected error occurred: %w", err)
49+
}
50+
}
51+
52+
return nil
53+
}
54+
55+
func registerString(cmd *cobra.Command, f *Flag, value *string, isUpdate bool) {
56+
cmd.Flags().StringVarP(value, f.LongForm, f.ShortForm, f.DefaultValue, f.Help)
57+
58+
if err := markFlagRequired(cmd, f, isUpdate); err != nil {
59+
panic(fmt.Errorf("An unexpected error occurred: %w", err)) // TODO: Handle
60+
}
61+
}
62+
63+
func markFlagRequired(cmd *cobra.Command, f *Flag, isUpdate bool) error {
64+
if f.IsRequired && !isUpdate {
65+
return cmd.MarkFlagRequired(f.LongForm)
66+
}
67+
68+
return nil
69+
}

internal/cli/login.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/auth0/auth0-cli/internal/ansi"
99
"github.com/auth0/auth0-cli/internal/auth"
1010
"github.com/auth0/auth0-cli/internal/open"
11+
"github.com/auth0/auth0-cli/internal/prompt"
1112
"github.com/spf13/cobra"
1213
)
1314

@@ -62,12 +63,28 @@ func RunLogin(ctx context.Context, cli *cli, expired bool) error {
6263
cli.renderer.Infof("Successfully logged in.")
6364
cli.renderer.Infof("Tenant: %s\n", res.Tenant)
6465

65-
return cli.addTenant(tenant{
66+
err = cli.addTenant(tenant{
6667
Name: res.Tenant,
6768
Domain: res.Domain,
6869
AccessToken: res.AccessToken,
6970
ExpiresAt: time.Now().Add(
7071
time.Duration(res.ExpiresIn) * time.Second,
7172
),
7273
})
74+
if err != nil {
75+
return fmt.Errorf("Unexpected error adding tenant to config: %w", err)
76+
}
77+
78+
if cli.config.DefaultTenant != res.Tenant {
79+
promptText := fmt.Sprintf("Your default tenant is %s. Do you want to change it to %s?", cli.config.DefaultTenant, res.Tenant)
80+
if confirmed := prompt.Confirm(promptText); !confirmed {
81+
return nil
82+
}
83+
cli.config.DefaultTenant = res.Tenant
84+
if err := cli.persistConfig(); err != nil {
85+
return fmt.Errorf("An error occurred while setting the default tenant: %w", err)
86+
}
87+
}
88+
89+
return nil
7390
}

0 commit comments

Comments
 (0)