Skip to content

Commit

Permalink
Adding interactive prompts selection to show and update command
Browse files Browse the repository at this point in the history
  • Loading branch information
willvedd committed Nov 14, 2023
1 parent 2314fe8 commit eb8d2fa
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 34 deletions.
87 changes: 55 additions & 32 deletions internal/cli/prompts_custom_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 <prompt>
auth0 universal-login prompts show <prompt> --language <language>
auth0 ul prompts show <prompt> -l <language>
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)
Expand All @@ -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 <prompt>
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions internal/cli/terraform_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit eb8d2fa

Please sign in to comment.