fix: confirm when deleting template (#2866)

* prompt for confirmation before deleting templates (#2830)

* populate templateNames from the interactive picker too

* allow skipping delete confirmation prompt with --yes flag

* eliminate unnecessary newline

* test both confirmation of delete and `--yes` with no confirmation

* fix failing test that needed --yes

* remove unnecessary empty line the linter disliked

* make the tests correct
This commit is contained in:
Ketan Gangatirkar
2022-07-11 13:13:56 -05:00
committed by GitHub
parent fa7dcf615a
commit 536c77af5d
3 changed files with 82 additions and 12 deletions
+22 -8
View File
@@ -2,6 +2,7 @@ package cli
import (
"fmt"
"strings"
"time"
"github.com/spf13/cobra"
@@ -12,7 +13,7 @@ import (
)
func templateDelete() *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: "delete [name...]",
Short: "Delete templates",
RunE: func(cmd *cobra.Command, args []string) error {
@@ -33,6 +34,14 @@ func templateDelete() *cobra.Command {
if len(args) > 0 {
templateNames = args
for _, templateName := range templateNames {
template, err := client.TemplateByName(ctx, organization.ID, templateName)
if err != nil {
return xerrors.Errorf("get template by name: %w", err)
}
templates = append(templates, template)
}
} else {
allTemplates, err := client.TemplatesByOrganization(ctx, organization.ID)
if err != nil {
@@ -58,17 +67,19 @@ func templateDelete() *cobra.Command {
for _, template := range allTemplates {
if template.Name == selection {
templates = append(templates, template)
templateNames = append(templateNames, template.Name)
}
}
}
for _, templateName := range templateNames {
template, err := client.TemplateByName(ctx, organization.ID, templateName)
if err != nil {
return xerrors.Errorf("get template by name: %w", err)
}
templates = append(templates, template)
// Confirm deletion of the template.
_, err = cliui.Prompt(cmd, cliui.PromptOptions{
Text: fmt.Sprintf("Delete these templates: %s?", cliui.Styles.Code.Render(strings.Join(templateNames, ", "))),
IsConfirm: true,
Default: "no",
})
if err != nil {
return err
}
for _, template := range templates {
@@ -83,4 +94,7 @@ func templateDelete() *cobra.Command {
return nil
},
}
cliui.AllowSkipPrompt(cmd)
return cmd
}