Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a44b1fc

Browse files
committed
feat: Add confirm prompts to some cli actions
- Workspace start/stop - Add optional -y skip. Standardize -y flag across commands
1 parent 3f770e1 commit a44b1fc

File tree

5 files changed

+43
-12
lines changed

5 files changed

+43
-12
lines changed

cli/cliui/prompt.go

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

27+
func AllowSkipPrompt(cmd *cobra.Command) {
28+
cmd.Flags().BoolP("yes", "y", false, "Bypass prompts")
29+
}
30+
2731
// Prompt asks the user for input.
2832
func Prompt(cmd *cobra.Command, opts PromptOptions) (string, error) {
33+
// If the cmd has a "yes" flag for skipping confirm prompts, honor it.
34+
// If it's not a "Confirm" prompt, then don't skip. As the default value of
35+
// "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
39+
}
40+
}
41+
2942
_, _ = fmt.Fprint(cmd.OutOrStdout(), Styles.FocusedPrompt.String()+opts.Text+" ")
3043
if opts.IsConfirm {
3144
opts.Default = "yes"

cli/create.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ func create() *cobra.Command {
194194
},
195195
}
196196

197+
cliui.AllowSkipPrompt(cmd)
197198
cliflag.StringVarP(cmd.Flags(), &templateName, "template", "t", "CODER_TEMPLATE_NAME", "", "Specify a template name.")
198199
return cmd
199200
}

cli/start.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,20 @@ import (
1111
)
1212

1313
func start() *cobra.Command {
14-
return &cobra.Command{
14+
cmd := &cobra.Command{
1515
Annotations: workspaceCommand,
1616
Use: "start <workspace>",
1717
Short: "Build a workspace with the start state",
1818
Args: cobra.ExactArgs(1),
1919
RunE: func(cmd *cobra.Command, args []string) error {
20+
_, err := cliui.Prompt(cmd, cliui.PromptOptions{
21+
Text: "Confirm start workspace?",
22+
IsConfirm: true,
23+
})
24+
if err != nil {
25+
return err
26+
}
27+
2028
client, err := createClient(cmd)
2129
if err != nil {
2230
return err
@@ -39,4 +47,6 @@ func start() *cobra.Command {
3947
return cliui.WorkspaceBuild(cmd.Context(), cmd.OutOrStdout(), client, build.ID, before)
4048
},
4149
}
50+
cliui.AllowSkipPrompt(cmd)
51+
return cmd
4252
}

cli/stop.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,20 @@ import (
1111
)
1212

1313
func stop() *cobra.Command {
14-
return &cobra.Command{
14+
cmd := &cobra.Command{
1515
Annotations: workspaceCommand,
1616
Use: "stop <workspace>",
1717
Short: "Build a workspace with the stop state",
1818
Args: cobra.ExactArgs(1),
1919
RunE: func(cmd *cobra.Command, args []string) error {
20+
_, err := cliui.Prompt(cmd, cliui.PromptOptions{
21+
Text: "Confirm stop workspace?",
22+
IsConfirm: true,
23+
})
24+
if err != nil {
25+
return err
26+
}
27+
2028
client, err := createClient(cmd)
2129
if err != nil {
2230
return err
@@ -39,4 +47,6 @@ func stop() *cobra.Command {
3947
return cliui.WorkspaceBuild(cmd.Context(), cmd.OutOrStdout(), client, build.ID, before)
4048
},
4149
}
50+
cliui.AllowSkipPrompt(cmd)
51+
return cmd
4252
}

cli/templatecreate.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121

2222
func templateCreate() *cobra.Command {
2323
var (
24-
yes bool
2524
directory string
2625
provisioner string
2726
)
@@ -84,14 +83,12 @@ func templateCreate() *cobra.Command {
8483
return err
8584
}
8685

87-
if !yes {
88-
_, err = cliui.Prompt(cmd, cliui.PromptOptions{
89-
Text: "Confirm create?",
90-
IsConfirm: true,
91-
})
92-
if err != nil {
93-
return err
94-
}
86+
_, err = cliui.Prompt(cmd, cliui.PromptOptions{
87+
Text: "Confirm create?",
88+
IsConfirm: true,
89+
})
90+
if err != nil {
91+
return err
9592
}
9693

9794
_, err = client.CreateTemplate(cmd.Context(), organization.ID, codersdk.CreateTemplateRequest{
@@ -121,7 +118,7 @@ func templateCreate() *cobra.Command {
121118
if err != nil {
122119
panic(err)
123120
}
124-
cmd.Flags().BoolVarP(&yes, "yes", "y", false, "Bypass prompts")
121+
cliui.AllowSkipPrompt(cmd)
125122
return cmd
126123
}
127124

0 commit comments

Comments
 (0)