From eb8d2fa3db11455f18b8a0a4f78d29417281e5d3 Mon Sep 17 00:00:00 2001 From: Will Vedder Date: Tue, 14 Nov 2023 15:31:01 -0500 Subject: [PATCH 1/3] Adding interactive prompts selection to show and update command --- internal/cli/prompts_custom_text.go | 87 ++++++++++++++++++----------- internal/cli/terraform_fetcher.go | 5 +- 2 files changed, 58 insertions(+), 34 deletions(-) diff --git a/internal/cli/prompts_custom_text.go b/internal/cli/prompts_custom_text.go index cdfbafa2d..eb5a1698e 100644 --- a/internal/cli/prompts_custom_text.go +++ b/internal/cli/prompts_custom_text.go @@ -35,6 +35,11 @@ var ( Help: "Text contents for the branding.", IsRequired: true, } + + customTextPrompt = Argument{ + Name: "Prompt", + Help: "ID of custom text prompt.", + } ) type promptsTextInput struct { @@ -59,19 +64,59 @@ func universalLoginPromptsTextCmd(cli *cli) *cobra.Command { return cmd } +func customTextPromptOptions(_ context.Context) (pickerOptions, error) { + var opts pickerOptions + for _, promptType := range customTextPromptTypes { + opts = append(opts, pickerOption{value: promptType, label: promptType}) + } + return opts, nil +} + func showPromptsTextCmd(cli *cli) *cobra.Command { var inputs promptsTextInput cmd := &cobra.Command{ Use: "show", - Args: cobra.ExactArgs(1), + Args: cobra.MaximumNArgs(1), Short: "Show the custom text for a prompt", Long: "Show the custom text for a prompt.", Example: ` auth0 universal-login prompts show auth0 universal-login prompts show --language auth0 ul prompts show -l auth0 ul prompts show signup -l es`, - RunE: showPromptsText(cli, &inputs), + + RunE: func(cmd *cobra.Command, args []string) error { + brandingText := make(map[string]interface{}) + + if len(args) == 0 { + if err := customTextPrompt.Pick(cmd, &inputs.Prompt, customTextPromptOptions); err != nil { + return err + } + } else { + inputs.Prompt = args[0] + } + + if err := ansi.Waiting(func() (err error) { + brandingText, err = cli.api.Prompt.CustomText(cmd.Context(), inputs.Prompt, inputs.Language) + return err + }); err != nil { + return fmt.Errorf( + "unable to fetch custom text for prompt %s and language %s: %w", + inputs.Prompt, + inputs.Language, + err, + ) + } + + brandingTextJSON, err := json.MarshalIndent(brandingText, "", " ") + if err != nil { + return fmt.Errorf("failed to serialize the prompt custom text to JSON: %w", err) + } + + cli.renderer.BrandingTextShow(string(brandingTextJSON), inputs.Prompt, inputs.Language) + + return nil + }, } textLanguage.RegisterString(cmd, &inputs.Language, textLanguageDefault) @@ -84,7 +129,7 @@ func updatePromptsTextCmd(cli *cli) *cobra.Command { cmd := &cobra.Command{ Use: "update", - Args: cobra.ExactArgs(1), + Args: cobra.MaximumNArgs(1), Short: "Update the custom text for a prompt", Long: "Update the custom text for a prompt.", Example: ` auth0 universal-login prompts update @@ -98,37 +143,15 @@ func updatePromptsTextCmd(cli *cli) *cobra.Command { return cmd } -func showPromptsText(cli *cli, inputs *promptsTextInput) func(cmd *cobra.Command, args []string) error { - return func(cmd *cobra.Command, args []string) error { - inputs.Prompt = args[0] - brandingText := make(map[string]interface{}) - - if err := ansi.Waiting(func() (err error) { - brandingText, err = cli.api.Prompt.CustomText(cmd.Context(), inputs.Prompt, inputs.Language) - return err - }); err != nil { - return fmt.Errorf( - "unable to fetch custom text for prompt %s and language %s: %w", - inputs.Prompt, - inputs.Language, - err, - ) - } - - brandingTextJSON, err := json.MarshalIndent(brandingText, "", " ") - if err != nil { - return fmt.Errorf("failed to serialize the prompt custom text to JSON: %w", err) - } - - cli.renderer.BrandingTextShow(string(brandingTextJSON), inputs.Prompt, inputs.Language) - - return nil - } -} - func updateBrandingText(cli *cli, inputs *promptsTextInput) func(cmd *cobra.Command, args []string) error { return func(cmd *cobra.Command, args []string) error { - inputs.Prompt = args[0] + if len(args) == 0 { + if err := customTextPrompt.Pick(cmd, &inputs.Prompt, customTextPromptOptions); err != nil { + return err + } + } else { + inputs.Prompt = args[0] + } inputs.Body = string(iostream.PipedInput()) brandingTextToEdit, err := fetchBrandingTextContentToEdit(cmd.Context(), cli, inputs) diff --git a/internal/cli/terraform_fetcher.go b/internal/cli/terraform_fetcher.go index 3ea07f60b..ee8e96271 100644 --- a/internal/cli/terraform_fetcher.go +++ b/internal/cli/terraform_fetcher.go @@ -329,16 +329,17 @@ func (f *promptResourceFetcher) FetchData(_ context.Context) (importDataList, er }, nil } +var customTextPromptTypes = []string{"login", "login-id", "login-password", "login-email-verification", "signup", "signup-id", "signup-password", "reset-password", "consent", "mfa-push", "mfa-otp", "mfa-voice", "mfa-phone", "mfa-webauthn", "mfa-sms", "mfa-email", "mfa-recovery-code", "mfa", "status", "device-flow", "email-verification", "email-otp-challenge", "organizations", "invitation", "common"} + func (f *promptCustomTextResourceFetcherResourceFetcher) FetchData(ctx context.Context) (importDataList, error) { tenant, err := f.api.Tenant.Read(ctx) if err != nil { return nil, err } - promptTypes := []string{"login", "login-id", "login-password", "login-email-verification", "signup", "signup-id", "signup-password", "reset-password", "consent", "mfa-push", "mfa-otp", "mfa-voice", "mfa-phone", "mfa-webauthn", "mfa-sms", "mfa-email", "mfa-recovery-code", "mfa", "status", "device-flow", "email-verification", "email-otp-challenge", "organizations", "invitation", "common"} var data importDataList for _, language := range tenant.GetEnabledLocales() { - for _, promptType := range promptTypes { + for _, promptType := range customTextPromptTypes { data = append(data, importDataItem{ ResourceName: "auth0_prompt_custom_text." + sanitizeResourceName(language+"_"+promptType), ImportID: promptType + "::" + language, From 3e9632069419c018c8eeed85d004e59fd3cd3468 Mon Sep 17 00:00:00 2001 From: Will Vedder Date: Tue, 14 Nov 2023 15:40:55 -0500 Subject: [PATCH 2/3] brining back showPromptsText function --- internal/cli/prompts_custom_text.go | 68 +++++++++++++++-------------- 1 file changed, 36 insertions(+), 32 deletions(-) diff --git a/internal/cli/prompts_custom_text.go b/internal/cli/prompts_custom_text.go index eb5a1698e..5c2e70652 100644 --- a/internal/cli/prompts_custom_text.go +++ b/internal/cli/prompts_custom_text.go @@ -85,38 +85,7 @@ func showPromptsTextCmd(cli *cli) *cobra.Command { auth0 ul prompts show -l auth0 ul prompts show signup -l es`, - RunE: func(cmd *cobra.Command, args []string) error { - brandingText := make(map[string]interface{}) - - if len(args) == 0 { - if err := customTextPrompt.Pick(cmd, &inputs.Prompt, customTextPromptOptions); err != nil { - return err - } - } else { - inputs.Prompt = args[0] - } - - if err := ansi.Waiting(func() (err error) { - brandingText, err = cli.api.Prompt.CustomText(cmd.Context(), inputs.Prompt, inputs.Language) - return err - }); err != nil { - return fmt.Errorf( - "unable to fetch custom text for prompt %s and language %s: %w", - inputs.Prompt, - inputs.Language, - err, - ) - } - - brandingTextJSON, err := json.MarshalIndent(brandingText, "", " ") - if err != nil { - return fmt.Errorf("failed to serialize the prompt custom text to JSON: %w", err) - } - - cli.renderer.BrandingTextShow(string(brandingTextJSON), inputs.Prompt, inputs.Language) - - return nil - }, + RunE: showPromptsText(cli, &inputs), } textLanguage.RegisterString(cmd, &inputs.Language, textLanguageDefault) @@ -143,6 +112,41 @@ func updatePromptsTextCmd(cli *cli) *cobra.Command { return cmd } +func showPromptsText(cli *cli, inputs *promptsTextInput) func(cmd *cobra.Command, args []string) error { + return func(cmd *cobra.Command, args []string) error { + + if len(args) == 0 { + if err := customTextPrompt.Pick(cmd, &inputs.Prompt, customTextPromptOptions); err != nil { + return err + } + } else { + inputs.Prompt = args[0] + } + + brandingText := make(map[string]interface{}) + if err := ansi.Waiting(func() (err error) { + brandingText, err = cli.api.Prompt.CustomText(cmd.Context(), inputs.Prompt, inputs.Language) + return err + }); err != nil { + return fmt.Errorf( + "unable to fetch custom text for prompt %s and language %s: %w", + inputs.Prompt, + inputs.Language, + err, + ) + } + + brandingTextJSON, err := json.MarshalIndent(brandingText, "", " ") + if err != nil { + return fmt.Errorf("failed to serialize the prompt custom text to JSON: %w", err) + } + + cli.renderer.BrandingTextShow(string(brandingTextJSON), inputs.Prompt, inputs.Language) + + return nil + } +} + func updateBrandingText(cli *cli, inputs *promptsTextInput) func(cmd *cobra.Command, args []string) error { return func(cmd *cobra.Command, args []string) error { if len(args) == 0 { From 94d31758ec97679325e6eb6f21268b076e802043 Mon Sep 17 00:00:00 2001 From: Will Vedder Date: Tue, 14 Nov 2023 16:13:12 -0500 Subject: [PATCH 3/3] Fixing linting error --- internal/cli/prompts_custom_text.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/cli/prompts_custom_text.go b/internal/cli/prompts_custom_text.go index 5c2e70652..484c2011f 100644 --- a/internal/cli/prompts_custom_text.go +++ b/internal/cli/prompts_custom_text.go @@ -114,7 +114,6 @@ func updatePromptsTextCmd(cli *cli) *cobra.Command { func showPromptsText(cli *cli, inputs *promptsTextInput) func(cmd *cobra.Command, args []string) error { return func(cmd *cobra.Command, args []string) error { - if len(args) == 0 { if err := customTextPrompt.Pick(cmd, &inputs.Prompt, customTextPromptOptions); err != nil { return err