From 4005c639370c3d3fb29dce0ecf77020ad6f9b8c9 Mon Sep 17 00:00:00 2001 From: "Jorge L. Fatta" Date: Fri, 19 Mar 2021 16:26:03 -0300 Subject: [PATCH] refactor: quickstarts flags (#169) * refactor: quickstarts flags * Update internal/cli/quickstarts.go Co-authored-by: Rita Zerrizuela * Update internal/cli/quickstarts.go Co-authored-by: Rita Zerrizuela * Update internal/cli/quickstarts.go Co-authored-by: Rita Zerrizuela * refactor: rename flags * fix: quickstart stack is mandatory Co-authored-by: Rita Zerrizuela --- internal/cli/apps.go | 48 ++++++++++++------------- internal/cli/quickstarts.go | 70 +++++++++++++++++++++---------------- 2 files changed, 63 insertions(+), 55 deletions(-) diff --git a/internal/cli/apps.go b/internal/cli/apps.go index 08a0b4f05..7bbbed06c 100644 --- a/internal/cli/apps.go +++ b/internal/cli/apps.go @@ -238,7 +238,7 @@ auth0 apps delete } func createAppCmd(cli *cli) *cobra.Command { - var flags struct { + var inputs struct { Name string Type string Description string @@ -263,35 +263,35 @@ auth0 apps create --name myapp --type [native|spa|regular|m2m] prepareInteractivity(cmd) }, RunE: func(cmd *cobra.Command, args []string) error { - if err := appName.Ask(cmd, &flags.Name); err != nil { + if err := appName.Ask(cmd, &inputs.Name); err != nil { return err } - if err := appType.Select(cmd, &flags.Type, appTypeOptions); err != nil { + if err := appType.Select(cmd, &inputs.Type, appTypeOptions); err != nil { return err } - if err := appDescription.Ask(cmd, &flags.Description); err != nil { + if err := appDescription.Ask(cmd, &inputs.Description); err != nil { return err } a := &management.Client{ - Name: &flags.Name, - Description: &flags.Description, - AppType: auth0.String(apiTypeFor(flags.Type)), - Callbacks: stringToInterfaceSlice(flags.Callbacks), - AllowedOrigins: stringToInterfaceSlice(flags.AllowedOrigins), - WebOrigins: stringToInterfaceSlice(flags.AllowedWebOrigins), - AllowedLogoutURLs: stringToInterfaceSlice(flags.AllowedLogoutURLs), - TokenEndpointAuthMethod: apiAuthMethodFor(flags.AuthMethod), + Name: &inputs.Name, + Description: &inputs.Description, + AppType: auth0.String(apiTypeFor(inputs.Type)), + Callbacks: stringToInterfaceSlice(inputs.Callbacks), + AllowedOrigins: stringToInterfaceSlice(inputs.AllowedOrigins), + WebOrigins: stringToInterfaceSlice(inputs.AllowedWebOrigins), + AllowedLogoutURLs: stringToInterfaceSlice(inputs.AllowedLogoutURLs), + TokenEndpointAuthMethod: apiAuthMethodFor(inputs.AuthMethod), OIDCConformant: &oidcConformant, JWTConfiguration: &management.ClientJWTConfiguration{Algorithm: &algorithm}, } - if len(flags.Grants) == 0 { - a.GrantTypes = apiDefaultGrantsFor(flags.Type) + if len(inputs.Grants) == 0 { + a.GrantTypes = apiDefaultGrantsFor(inputs.Type) } else { - a.GrantTypes = apiGrantsFor(flags.Grants) + a.GrantTypes = apiGrantsFor(inputs.Grants) } err := ansi.Spinner("Creating application", func() error { @@ -310,15 +310,15 @@ auth0 apps create --name myapp --type [native|spa|regular|m2m] }, } - appName.RegisterString(cmd, &flags.Name, "") - appType.RegisterString(cmd, &flags.Type, "") - appDescription.RegisterString(cmd, &flags.Description, "") - appCallbacks.RegisterStringSlice(cmd, &flags.Callbacks, nil) - appOrigins.RegisterStringSlice(cmd, &flags.AllowedOrigins, nil) - appWebOrigins.RegisterStringSlice(cmd, &flags.AllowedWebOrigins, nil) - appLogoutURLs.RegisterStringSlice(cmd, &flags.AllowedLogoutURLs, nil) - appAuthMethod.RegisterString(cmd, &flags.AuthMethod, "") - appGrants.RegisterStringSlice(cmd, &flags.Grants, nil) + appName.RegisterString(cmd, &inputs.Name, "") + appType.RegisterString(cmd, &inputs.Type, "") + appDescription.RegisterString(cmd, &inputs.Description, "") + appCallbacks.RegisterStringSlice(cmd, &inputs.Callbacks, nil) + appOrigins.RegisterStringSlice(cmd, &inputs.AllowedOrigins, nil) + appWebOrigins.RegisterStringSlice(cmd, &inputs.AllowedWebOrigins, nil) + appLogoutURLs.RegisterStringSlice(cmd, &inputs.AllowedLogoutURLs, nil) + appAuthMethod.RegisterString(cmd, &inputs.AuthMethod, "") + appGrants.RegisterStringSlice(cmd, &inputs.Grants, nil) return cmd } diff --git a/internal/cli/quickstarts.go b/internal/cli/quickstarts.go index 63c6952de..1b118bb89 100644 --- a/internal/cli/quickstarts.go +++ b/internal/cli/quickstarts.go @@ -22,6 +22,15 @@ import ( "gopkg.in/auth0.v5/management" ) +// QuickStart app types and defaults +const ( + qsNative = "native" + qsSpa = "spa" + qsWebApp = "webapp" + qsBackend = "backend" + _defaultURL = "http://localhost:3000" +) + var ( //go:embed data/quickstarts.json qsBuf []byte @@ -31,15 +40,22 @@ var ( } return }() -) -// QuickStart app types and defaults -const ( - qsNative = "native" - qsSpa = "spa" - qsWebApp = "webapp" - qsBackend = "backend" - _defaultURL = "http://localhost:3000" + qsClientID = Flag{ + Name: "Client ID", + LongForm: "client-id", + ShortForm: "c", + Help: "Client Id of an Auth0 application.", + IsRequired: true, + } + + qsStack = Flag{ + Name: "Stack", + LongForm: "stack", + ShortForm: "s", + Help: "Tech/Language of the quickstart sample to download.", + IsRequired: true, + } ) type quickstart struct { @@ -64,16 +80,15 @@ func quickstartsCmd(cli *cli) *cobra.Command { } func downloadQuickstart(cli *cli) *cobra.Command { - var flags struct { + var inputs struct { ClientID string - Type string Stack string } cmd := &cobra.Command{ Use: "download", Short: "Download a quickstart sample app for a specific tech stack", - Long: `auth0 quickstarts download --type --client-id --stack `, + Long: `auth0 quickstarts download --client-id --stack `, PreRun: func(cmd *cobra.Command, args []string) { prepareInteractivity(cmd) }, @@ -82,30 +97,24 @@ func downloadQuickstart(cli *cli) *cobra.Command { return errors.New("This command can only be run on interactive mode") } - selectedClientID := flags.ClientID - - if selectedClientID == "" { - input := prompt.TextInput("client-id", "Client Id:", "Client Id of an Auth0 application.", true) - if err := prompt.AskOne(input, &selectedClientID); err != nil { - return fmt.Errorf("An unexpected error occurred: %v", err) - } + if err := qsClientID.Ask(cmd, &inputs.ClientID); err != nil { + return err } - client, err := cli.api.Client.Read(selectedClientID) + client, err := cli.api.Client.Read(inputs.ClientID) if err != nil { return fmt.Errorf("An unexpected error occurred, please verify your client id: %v", err.Error()) } - selectedStack := flags.Stack - - if selectedStack == "" { + if inputs.Stack == "" { + // get the valid types for this App: stacks, err := quickstartStacksFromType(client.GetAppType()) if err != nil { return fmt.Errorf("An unexpected error occurred: %v", err) } - input := prompt.SelectInput("stack", "Stack:", "Tech/Language of the quickstart sample to download", stacks, true) - if err := prompt.AskOne(input, &selectedStack); err != nil { - return fmt.Errorf("An unexpected error occurred: %v", err) + // ask for input using the valid types only: + if err := qsStack.Select(cmd, &inputs.Stack, stacks); err != nil { + return err } } @@ -120,13 +129,13 @@ func downloadQuickstart(cli *cli) *cobra.Command { } } - q, err := getQuickstart(client.GetAppType(), selectedStack) + q, err := getQuickstart(client.GetAppType(), inputs.Stack) if err != nil { - return fmt.Errorf("An unexpected error occurred with the specified stack %v: %v", selectedStack, err) + return fmt.Errorf("An unexpected error occurred with the specified stack %v: %v", inputs.Stack, err) } err = ansi.Spinner("Downloading quickstart sample", func() error { - return downloadQuickStart(context.TODO(), cli, client, target, q) + return downloadQuickStart(cmd.Context(), cli, client, target, q) }) if err != nil { @@ -144,10 +153,9 @@ func downloadQuickstart(cli *cli) *cobra.Command { } cmd.SetUsageTemplate(resourceUsageTemplate()) - cmd.Flags().StringVarP(&flags.ClientID, "client-id", "c", "", "Client Id of an Auth0 application.") - cmd.Flags().StringVarP(&flags.Stack, "stack", "s", "", "Tech/Language of the quickstart sample to download.") - mustRequireFlags(cmd, "client-id") + qsClientID.RegisterString(cmd, &inputs.ClientID, "") + qsStack.RegisterString(cmd, &inputs.Stack, "") return cmd }