From ea044f288d9284d6ef820a8f1111e141ffd18aed Mon Sep 17 00:00:00 2001 From: Radoslav Dimitrov Date: Mon, 20 Nov 2023 19:40:24 +0200 Subject: [PATCH] Add option to set default selected choice for minder prompts (#1698) --- cmd/cli/app/auth/auth_delete.go | 3 ++- cmd/cli/app/auth/auth_revoke_provider.go | 3 ++- cmd/cli/app/provider/provider_enroll.go | 3 ++- cmd/cli/app/quickstart/quickstart.go | 15 ++++++++++----- cmd/cli/app/rule_type/rule_type_delete.go | 3 ++- internal/util/cli/cli.go | 10 ++++++++-- 6 files changed, 26 insertions(+), 11 deletions(-) diff --git a/cmd/cli/app/auth/auth_delete.go b/cmd/cli/app/auth/auth_delete.go index 4a9bdc9b5b..cf651786c8 100644 --- a/cmd/cli/app/auth/auth_delete.go +++ b/cmd/cli/app/auth/auth_delete.go @@ -63,7 +63,8 @@ var auth_deleteCmd = &cobra.Command{ userDetails.Email, ), "Are you sure?", - "Delete account operation cancelled.") + "Delete account operation cancelled.", + false) if !yes { return } diff --git a/cmd/cli/app/auth/auth_revoke_provider.go b/cmd/cli/app/auth/auth_revoke_provider.go index a867df4afa..323923fdf2 100644 --- a/cmd/cli/app/auth/auth_revoke_provider.go +++ b/cmd/cli/app/auth/auth_revoke_provider.go @@ -58,7 +58,8 @@ var Auth_revokeproviderCmd = &cobra.Command{ yes := cli.PrintYesNoPrompt(cmd, "You are about to revoke the access tokens for your provider.", "Are you sure?", - "Delete provider access tokens cancelled.") + "Delete provider access tokens cancelled.", + false) if !yes { return } diff --git a/cmd/cli/app/provider/provider_enroll.go b/cmd/cli/app/provider/provider_enroll.go index b63dfa96db..4ff98b23c4 100644 --- a/cmd/cli/app/provider/provider_enroll.go +++ b/cmd/cli/app/provider/provider_enroll.go @@ -144,7 +144,8 @@ func EnrollProviderCmd(cmd *cobra.Command, _ []string) (string, error) { yes := cli.PrintYesNoPrompt(cmd, fmt.Sprintf("You are about to enroll repositories from %s.", ownerPromptStr), "Do you confirm?", - "Enroll operation cancelled.") + "Enroll operation cancelled.", + true) if !yes { return "", nil } diff --git a/cmd/cli/app/quickstart/quickstart.go b/cmd/cli/app/quickstart/quickstart.go index c433f1eeab..8192d252be 100644 --- a/cmd/cli/app/quickstart/quickstart.go +++ b/cmd/cli/app/quickstart/quickstart.go @@ -142,7 +142,8 @@ var cmd = &cobra.Command{ yes := cli.PrintYesNoPrompt(cmd, stepPromptMsgWelcome, "Proceed?", - "Quickstart operation cancelled.") + "Quickstart operation cancelled.", + true) if !yes { return nil } @@ -151,7 +152,8 @@ var cmd = &cobra.Command{ yes = cli.PrintYesNoPrompt(cmd, stepPromptMsgEnroll, "Proceed?", - "Quickstart operation cancelled.") + "Quickstart operation cancelled.", + true) if !yes { return nil } @@ -173,7 +175,8 @@ var cmd = &cobra.Command{ yes = cli.PrintYesNoPrompt(cmd, stepPromptMsgRegister, "Proceed?", - "Quickstart operation cancelled.") + "Quickstart operation cancelled.", + true) if !yes { return nil } @@ -192,7 +195,8 @@ var cmd = &cobra.Command{ yes = cli.PrintYesNoPrompt(cmd, stepPromptMsgRuleType, "Proceed?", - "Quickstart operation cancelled.") + "Quickstart operation cancelled.", + true) if !yes { return nil } @@ -244,7 +248,8 @@ var cmd = &cobra.Command{ yes = cli.PrintYesNoPrompt(cmd, fmt.Sprintf(stepPromptMsgProfile, strings.Join(registeredRepos[:], "\n")), "Proceed?", - "Quickstart operation cancelled.") + "Quickstart operation cancelled.", + true) if !yes { return nil } diff --git a/cmd/cli/app/rule_type/rule_type_delete.go b/cmd/cli/app/rule_type/rule_type_delete.go index d6638fc2f2..58ebfbc534 100644 --- a/cmd/cli/app/rule_type/rule_type_delete.go +++ b/cmd/cli/app/rule_type/rule_type_delete.go @@ -58,7 +58,8 @@ minder control plane.`, yes := cli.PrintYesNoPrompt(cmd, "You are about to permanently delete all of your rule types.", "Are you sure?", - "Delete all rule types operation cancelled.") + "Delete all rule types operation cancelled.", + false) if !yes { return } diff --git a/internal/util/cli/cli.go b/internal/util/cli/cli.go index f7607f833c..925709ef11 100644 --- a/internal/util/cli/cli.go +++ b/internal/util/cli/cli.go @@ -41,12 +41,18 @@ func Print(out io.Writer, msg string, args ...interface{}) { } // PrintYesNoPrompt prints a yes/no prompt to the user and returns false if the user did not respond with yes or y -func PrintYesNoPrompt(cmd *cobra.Command, promptMsg, confirmMsg, fallbackMsg string) bool { +func PrintYesNoPrompt(cmd *cobra.Command, promptMsg, confirmMsg, fallbackMsg string, defaultYes bool) bool { // Print the warning banner with the prompt message PrintCmd(cmd, WarningBanner.Render(promptMsg)) + // Determine the default confirmation value + defConf := confirmation.No + if defaultYes { + defConf = confirmation.Yes + } + // Prompt the user for confirmation - input := confirmation.New(confirmMsg, confirmation.No) + input := confirmation.New(confirmMsg, defConf) ok, err := input.RunPrompt() if err != nil { PrintCmd(cmd, WarningBanner.Render(fmt.Sprintf("Error reading input: %v", err)))