Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: Add confirm prompts to some cli actions
- Workspace start/stop
- Add optional -y skip. Standardize -y flag across commands
  • Loading branch information
Emyrk committed May 19, 2022
commit a44b1fc55735ed610e8a103c4c9faa97c3a32c12
13 changes: 13 additions & 0 deletions cli/cliui/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we print to the terminal here e.g. Confirm create? (yes/no) yes, or Skipping: Confirm create? (yes/no) just to make it clear that the --yes flag had an effect?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mafredri I was unsure. Do other applications do this? apt-get does not have any indication for example.

For scripting purposes, I would imagine we want to reduce output to near 0 if we can.

}
}

_, _ = fmt.Fprint(cmd.OutOrStdout(), Styles.FocusedPrompt.String()+opts.Text+" ")
if opts.IsConfirm {
opts.Default = "yes"
Expand Down
1 change: 1 addition & 0 deletions cli/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ func create() *cobra.Command {
},
}

cliui.AllowSkipPrompt(cmd)
cliflag.StringVarP(cmd.Flags(), &templateName, "template", "t", "CODER_TEMPLATE_NAME", "", "Specify a template name.")
return cmd
}
12 changes: 11 additions & 1 deletion cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@ import (
)

func start() *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Annotations: workspaceCommand,
Use: "start <workspace>",
Short: "Build a workspace with the start state",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
_, err := cliui.Prompt(cmd, cliui.PromptOptions{
Text: "Confirm start workspace?",
IsConfirm: true,
})
if err != nil {
return err
}

client, err := createClient(cmd)
if err != nil {
return err
Expand All @@ -39,4 +47,6 @@ func start() *cobra.Command {
return cliui.WorkspaceBuild(cmd.Context(), cmd.OutOrStdout(), client, build.ID, before)
},
}
cliui.AllowSkipPrompt(cmd)
return cmd
}
12 changes: 11 additions & 1 deletion cli/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@ import (
)

func stop() *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Annotations: workspaceCommand,
Use: "stop <workspace>",
Short: "Build a workspace with the stop state",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
_, err := cliui.Prompt(cmd, cliui.PromptOptions{
Text: "Confirm stop workspace?",
IsConfirm: true,
})
if err != nil {
return err
}

client, err := createClient(cmd)
if err != nil {
return err
Expand All @@ -39,4 +47,6 @@ func stop() *cobra.Command {
return cliui.WorkspaceBuild(cmd.Context(), cmd.OutOrStdout(), client, build.ID, before)
},
}
cliui.AllowSkipPrompt(cmd)
return cmd
}
17 changes: 7 additions & 10 deletions cli/templatecreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

func templateCreate() *cobra.Command {
var (
yes bool
directory string
provisioner string
)
Expand Down Expand Up @@ -84,14 +83,12 @@ func templateCreate() *cobra.Command {
return err
}

if !yes {
_, err = cliui.Prompt(cmd, cliui.PromptOptions{
Text: "Confirm create?",
IsConfirm: true,
})
if err != nil {
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{
Expand Down Expand Up @@ -121,7 +118,7 @@ func templateCreate() *cobra.Command {
if err != nil {
panic(err)
}
cmd.Flags().BoolVarP(&yes, "yes", "y", false, "Bypass prompts")
cliui.AllowSkipPrompt(cmd)
return cmd
}

Expand Down