Skip to content

Commit 61c52b3

Browse files
authored
feat: default confirm to no for cli delete (coder#2919)
1 parent b0bab3e commit 61c52b3

File tree

9 files changed

+30
-13
lines changed

9 files changed

+30
-13
lines changed

cli/cliui/prompt.go

+22-6
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,41 @@ type PromptOptions struct {
2424
Validate func(string) error
2525
}
2626

27+
const skipPromptFlag = "yes"
28+
2729
func AllowSkipPrompt(cmd *cobra.Command) {
28-
cmd.Flags().BoolP("yes", "y", false, "Bypass prompts")
30+
cmd.Flags().BoolP(skipPromptFlag, "y", false, "Bypass prompts")
2931
}
3032

33+
const (
34+
ConfirmYes = "yes"
35+
ConfirmNo = "no"
36+
)
37+
3138
// Prompt asks the user for input.
3239
func Prompt(cmd *cobra.Command, opts PromptOptions) (string, error) {
3340
// If the cmd has a "yes" flag for skipping confirm prompts, honor it.
3441
// If it's not a "Confirm" prompt, then don't skip. As the default value of
3542
// "yes" makes no sense.
36-
if opts.IsConfirm && cmd.Flags().Lookup("yes") != nil {
37-
if skip, _ := cmd.Flags().GetBool("yes"); skip {
38-
return "yes", nil
43+
if opts.IsConfirm && cmd.Flags().Lookup(skipPromptFlag) != nil {
44+
if skip, _ := cmd.Flags().GetBool(skipPromptFlag); skip {
45+
return ConfirmYes, nil
3946
}
4047
}
4148

4249
_, _ = fmt.Fprint(cmd.OutOrStdout(), Styles.FocusedPrompt.String()+opts.Text+" ")
4350
if opts.IsConfirm {
44-
opts.Default = "yes"
45-
_, _ = fmt.Fprint(cmd.OutOrStdout(), Styles.Placeholder.Render("("+Styles.Bold.Render("yes")+Styles.Placeholder.Render("/no) ")))
51+
if len(opts.Default) == 0 {
52+
opts.Default = ConfirmYes
53+
}
54+
renderedYes := Styles.Placeholder.Render(ConfirmYes)
55+
renderedNo := Styles.Placeholder.Render(ConfirmNo)
56+
if opts.Default == ConfirmYes {
57+
renderedYes = Styles.Bold.Render(ConfirmYes)
58+
} else {
59+
renderedNo = Styles.Bold.Render(ConfirmNo)
60+
}
61+
_, _ = fmt.Fprint(cmd.OutOrStdout(), Styles.Placeholder.Render("("+renderedYes+Styles.Placeholder.Render("/"+renderedNo+Styles.Placeholder.Render(") "))))
4662
} else if opts.Default != "" {
4763
_, _ = fmt.Fprint(cmd.OutOrStdout(), Styles.Placeholder.Render("("+opts.Default+") "))
4864
}

cli/delete.go

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func deleteWorkspace() *cobra.Command {
2222
_, err := cliui.Prompt(cmd, cliui.PromptOptions{
2323
Text: "Confirm delete workspace?",
2424
IsConfirm: true,
25+
Default: cliui.ConfirmNo,
2526
})
2627
if err != nil {
2728
return err

cli/login.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func login() *cobra.Command {
8989
}
9090
_, err := cliui.Prompt(cmd, cliui.PromptOptions{
9191
Text: "Would you like to create the first user?",
92-
Default: "yes",
92+
Default: cliui.ConfirmYes,
9393
IsConfirm: true,
9494
})
9595
if errors.Is(err, cliui.Canceled) {

cli/logout.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func logout() *cobra.Command {
2828
_, err = cliui.Prompt(cmd, cliui.PromptOptions{
2929
Text: "Are you sure you want to log out?",
3030
IsConfirm: true,
31-
Default: "yes",
31+
Default: cliui.ConfirmYes,
3232
})
3333
if err != nil {
3434
return err

cli/templatecreate.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func templateCreate() *cobra.Command {
6060
_, err = cliui.Prompt(cmd, cliui.PromptOptions{
6161
Text: fmt.Sprintf("Create and upload %q?", prettyDir),
6262
IsConfirm: true,
63-
Default: "yes",
63+
Default: cliui.ConfirmYes,
6464
})
6565
if err != nil {
6666
return err

cli/templatedelete.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func templateDelete() *cobra.Command {
7676
_, err = cliui.Prompt(cmd, cliui.PromptOptions{
7777
Text: fmt.Sprintf("Delete these templates: %s?", cliui.Styles.Code.Render(strings.Join(templateNames, ", "))),
7878
IsConfirm: true,
79-
Default: "no",
79+
Default: cliui.ConfirmNo,
8080
})
8181
if err != nil {
8282
return err

cli/templateupdate.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func templateUpdate() *cobra.Command {
5353
_, err = cliui.Prompt(cmd, cliui.PromptOptions{
5454
Text: fmt.Sprintf("Upload %q?", prettyDir),
5555
IsConfirm: true,
56-
Default: "yes",
56+
Default: cliui.ConfirmYes,
5757
})
5858
if err != nil {
5959
return err

cli/userstatus.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func createUserStatusCommand(sdkStatus codersdk.UserStatus) *cobra.Command {
7171
_, err = cliui.Prompt(cmd, cliui.PromptOptions{
7272
Text: fmt.Sprintf("Are you sure you want to %s this user?", verb),
7373
IsConfirm: true,
74-
Default: "yes",
74+
Default: cliui.ConfirmYes,
7575
})
7676
if err != nil {
7777
return err

cmd/cliui/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func main() {
4343
}
4444
_, err = cliui.Prompt(cmd, cliui.PromptOptions{
4545
Text: "Do you want to accept?",
46-
Default: "yes",
46+
Default: cliui.ConfirmYes,
4747
IsConfirm: true,
4848
})
4949
if errors.Is(err, cliui.Canceled) {

0 commit comments

Comments
 (0)