- Notifications
You must be signed in to change notification settings - Fork1k
feat: Add confirm prompts to some cli actions#1591
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
a44b1fc
664ea38
373cb23
3b6cea6
843dd21
9c09a08
15acead
6a4682a
031134a
e334b22
2ecf664
e1b6bfb
0101e6d
d31fbc2
bed1d44
9d527bd
d3f0c6d
7cdb06e
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -24,8 +24,21 @@ type PromptOptions struct { | ||
Validate func(string) error | ||
} | ||
func AllowSkipPrompt(cmd *cobra.Command) { | ||
cmd.Flags().BoolP("yes", "y", false, "Bypass prompts") | ||
} | ||
// 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Should we print to the terminal here e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. @mafredri I was unsure. Do other applications do this? For scripting purposes, I would imagine we want to reduce output to near 0 if we can. | ||
} | ||
} | ||
Emyrk marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
_, _ = fmt.Fprint(cmd.OutOrStdout(), Styles.FocusedPrompt.String()+opts.Text+" ") | ||
if opts.IsConfirm { | ||
opts.Default = "yes" | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -11,13 +11,21 @@ import ( | ||
// nolint | ||
func delete() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Annotations: workspaceCommand, | ||
Use: "delete <workspace>", | ||
Short: "Delete a workspace", | ||
Aliases: []string{"rm"}, | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
_, err := cliui.Prompt(cmd, cliui.PromptOptions{ | ||
Text: "Confirm delete workspace?", | ||
IsConfirm: true, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
Emyrk marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
client, err := createClient(cmd) | ||
if err != nil { | ||
return err | ||
@@ -40,4 +48,6 @@ func delete() *cobra.Command { | ||
return cliui.WorkspaceBuild(cmd.Context(), cmd.OutOrStdout(), client, build.ID, before) | ||
}, | ||
} | ||
cliui.AllowSkipPrompt(cmd) | ||
return cmd | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -21,7 +21,6 @@ import ( | ||
func templateCreate() *cobra.Command { | ||
var ( | ||
directory string | ||
provisioner string | ||
parameterFile string | ||
@@ -85,14 +84,12 @@ func templateCreate() *cobra.Command { | ||
return err | ||
} | ||
_, err = cliui.Prompt(cmd, cliui.PromptOptions{ | ||
Text: "Confirm create?", | ||
IsConfirm: true, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = client.CreateTemplate(cmd.Context(), organization.ID, codersdk.CreateTemplateRequest{ | ||
@@ -123,7 +120,7 @@ func templateCreate() *cobra.Command { | ||
if err != nil { | ||
panic(err) | ||
} | ||
cliui.AllowSkipPrompt(cmd) | ||
Emyrk marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
return cmd | ||
} | ||