Skip to content

Commit 3ee28e3

Browse files
committed
feat: Add function to skip confirm prompts
1 parent 89e44da commit 3ee28e3

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

cli/cliui/prompt.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,26 @@ type PromptOptions struct {
2424
Validate func(string) error
2525
}
2626

27+
// AllowSkipConfirms allows the user to pass `-y` to skip all confirm prompts for
28+
// the command. This is helpful for scripts.
29+
// The reason this is not added to the root command is that some commands mix
30+
// confirm prompts with regular prompts. It would be strange to skip only some
31+
// of the prompts.
32+
func AllowSkipConfirms(cmd *cobra.Command) {
33+
cmd.PersistentFlags().BoolP("yes", "y", false, "Automatically yes all yes/no prompts. Other prompts will still show")
34+
}
35+
2736
// Prompt asks the user for input.
2837
func Prompt(cmd *cobra.Command, opts PromptOptions) (string, error) {
38+
if cmd.Flags().Lookup("yes") != nil {
39+
// If the cmd has the skip flag available and the user has it set, return "yes"
40+
// for all confirm prompts for them.
41+
skipConfirms, _ := cmd.Flags().GetBool("yes")
42+
if opts.IsConfirm && skipConfirms {
43+
return "yes", nil
44+
}
45+
}
46+
2947
_, _ = fmt.Fprint(cmd.OutOrStdout(), Styles.FocusedPrompt.String()+opts.Text+" ")
3048
if opts.IsConfirm {
3149
opts.Default = "yes"

0 commit comments

Comments
 (0)