mirror of https://github.com/coder/coder.git
feat: default confirm to no for cli delete (#2919)
This commit is contained in:
parent
b0bab3e432
commit
61c52b3090
|
@ -24,25 +24,41 @@ type PromptOptions struct {
|
|||
Validate func(string) error
|
||||
}
|
||||
|
||||
const skipPromptFlag = "yes"
|
||||
|
||||
func AllowSkipPrompt(cmd *cobra.Command) {
|
||||
cmd.Flags().BoolP("yes", "y", false, "Bypass prompts")
|
||||
cmd.Flags().BoolP(skipPromptFlag, "y", false, "Bypass prompts")
|
||||
}
|
||||
|
||||
const (
|
||||
ConfirmYes = "yes"
|
||||
ConfirmNo = "no"
|
||||
)
|
||||
|
||||
// Prompt asks the user for input.
|
||||
func Prompt(cmd *cobra.Command, opts PromptOptions) (string, error) {
|
||||
// If the cmd has a "yes" flag for skipping confirm prompts, honor it.
|
||||
// If it's not a "Confirm" prompt, then don't skip. As the default value of
|
||||
// "yes" makes no sense.
|
||||
if opts.IsConfirm && cmd.Flags().Lookup("yes") != nil {
|
||||
if skip, _ := cmd.Flags().GetBool("yes"); skip {
|
||||
return "yes", nil
|
||||
if opts.IsConfirm && cmd.Flags().Lookup(skipPromptFlag) != nil {
|
||||
if skip, _ := cmd.Flags().GetBool(skipPromptFlag); skip {
|
||||
return ConfirmYes, nil
|
||||
}
|
||||
}
|
||||
|
||||
_, _ = fmt.Fprint(cmd.OutOrStdout(), Styles.FocusedPrompt.String()+opts.Text+" ")
|
||||
if opts.IsConfirm {
|
||||
opts.Default = "yes"
|
||||
_, _ = fmt.Fprint(cmd.OutOrStdout(), Styles.Placeholder.Render("("+Styles.Bold.Render("yes")+Styles.Placeholder.Render("/no) ")))
|
||||
if len(opts.Default) == 0 {
|
||||
opts.Default = ConfirmYes
|
||||
}
|
||||
renderedYes := Styles.Placeholder.Render(ConfirmYes)
|
||||
renderedNo := Styles.Placeholder.Render(ConfirmNo)
|
||||
if opts.Default == ConfirmYes {
|
||||
renderedYes = Styles.Bold.Render(ConfirmYes)
|
||||
} else {
|
||||
renderedNo = Styles.Bold.Render(ConfirmNo)
|
||||
}
|
||||
_, _ = fmt.Fprint(cmd.OutOrStdout(), Styles.Placeholder.Render("("+renderedYes+Styles.Placeholder.Render("/"+renderedNo+Styles.Placeholder.Render(") "))))
|
||||
} else if opts.Default != "" {
|
||||
_, _ = fmt.Fprint(cmd.OutOrStdout(), Styles.Placeholder.Render("("+opts.Default+") "))
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@ func deleteWorkspace() *cobra.Command {
|
|||
_, err := cliui.Prompt(cmd, cliui.PromptOptions{
|
||||
Text: "Confirm delete workspace?",
|
||||
IsConfirm: true,
|
||||
Default: cliui.ConfirmNo,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -89,7 +89,7 @@ func login() *cobra.Command {
|
|||
}
|
||||
_, err := cliui.Prompt(cmd, cliui.PromptOptions{
|
||||
Text: "Would you like to create the first user?",
|
||||
Default: "yes",
|
||||
Default: cliui.ConfirmYes,
|
||||
IsConfirm: true,
|
||||
})
|
||||
if errors.Is(err, cliui.Canceled) {
|
||||
|
|
|
@ -28,7 +28,7 @@ func logout() *cobra.Command {
|
|||
_, err = cliui.Prompt(cmd, cliui.PromptOptions{
|
||||
Text: "Are you sure you want to log out?",
|
||||
IsConfirm: true,
|
||||
Default: "yes",
|
||||
Default: cliui.ConfirmYes,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -60,7 +60,7 @@ func templateCreate() *cobra.Command {
|
|||
_, err = cliui.Prompt(cmd, cliui.PromptOptions{
|
||||
Text: fmt.Sprintf("Create and upload %q?", prettyDir),
|
||||
IsConfirm: true,
|
||||
Default: "yes",
|
||||
Default: cliui.ConfirmYes,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -76,7 +76,7 @@ func templateDelete() *cobra.Command {
|
|||
_, err = cliui.Prompt(cmd, cliui.PromptOptions{
|
||||
Text: fmt.Sprintf("Delete these templates: %s?", cliui.Styles.Code.Render(strings.Join(templateNames, ", "))),
|
||||
IsConfirm: true,
|
||||
Default: "no",
|
||||
Default: cliui.ConfirmNo,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -53,7 +53,7 @@ func templateUpdate() *cobra.Command {
|
|||
_, err = cliui.Prompt(cmd, cliui.PromptOptions{
|
||||
Text: fmt.Sprintf("Upload %q?", prettyDir),
|
||||
IsConfirm: true,
|
||||
Default: "yes",
|
||||
Default: cliui.ConfirmYes,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -71,7 +71,7 @@ func createUserStatusCommand(sdkStatus codersdk.UserStatus) *cobra.Command {
|
|||
_, err = cliui.Prompt(cmd, cliui.PromptOptions{
|
||||
Text: fmt.Sprintf("Are you sure you want to %s this user?", verb),
|
||||
IsConfirm: true,
|
||||
Default: "yes",
|
||||
Default: cliui.ConfirmYes,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -43,7 +43,7 @@ func main() {
|
|||
}
|
||||
_, err = cliui.Prompt(cmd, cliui.PromptOptions{
|
||||
Text: "Do you want to accept?",
|
||||
Default: "yes",
|
||||
Default: cliui.ConfirmYes,
|
||||
IsConfirm: true,
|
||||
})
|
||||
if errors.Is(err, cliui.Canceled) {
|
||||
|
|
Loading…
Reference in New Issue