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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 82 additions and 12 deletions

View File

@ -229,6 +229,7 @@ func TestTemplateCreate(t *testing.T) {
"templates",
"delete",
"my-template",
"--yes",
}
cmd, root := clitest.New(t, args...)
clitest.SetupConfig(t, client, root)

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
}

View File

@ -2,11 +2,14 @@ package cli_test
import (
"context"
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/cli/cliui"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/pty/ptytest"
@ -25,14 +28,54 @@ func TestTemplateDelete(t *testing.T) {
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
cmd, root := clitest.New(t, "templates", "delete", template.Name)
clitest.SetupConfig(t, client, root)
require.NoError(t, cmd.Execute())
pty := ptytest.New(t)
cmd.SetIn(pty.Input())
cmd.SetOut(pty.Output())
execDone := make(chan error)
go func() {
execDone <- cmd.Execute()
}()
pty.ExpectMatch(fmt.Sprintf("Delete these templates: %s?", cliui.Styles.Code.Render(template.Name)))
pty.WriteLine("yes")
require.NoError(t, <-execDone)
_, err := client.Template(context.Background(), template.ID)
require.Error(t, err, "template should not exist")
})
t.Run("Multiple", func(t *testing.T) {
t.Run("Multiple --yes", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true})
user := coderdtest.CreateFirstUser(t, client)
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
templates := []codersdk.Template{
coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID),
coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID),
coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID),
}
templateNames := []string{}
for _, template := range templates {
templateNames = append(templateNames, template.Name)
}
cmd, root := clitest.New(t, append([]string{"templates", "delete", "--yes"}, templateNames...)...)
clitest.SetupConfig(t, client, root)
require.NoError(t, cmd.Execute())
for _, template := range templates {
_, err := client.Template(context.Background(), template.ID)
require.Error(t, err, "template should not exist")
}
})
t.Run("Multiple prompted", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true})
@ -51,7 +94,19 @@ func TestTemplateDelete(t *testing.T) {
cmd, root := clitest.New(t, append([]string{"templates", "delete"}, templateNames...)...)
clitest.SetupConfig(t, client, root)
require.NoError(t, cmd.Execute())
pty := ptytest.New(t)
cmd.SetIn(pty.Input())
cmd.SetOut(pty.Output())
execDone := make(chan error)
go func() {
execDone <- cmd.Execute()
}()
pty.ExpectMatch(fmt.Sprintf("Delete these templates: %s?", cliui.Styles.Code.Render(strings.Join(templateNames, ", "))))
pty.WriteLine("yes")
require.NoError(t, <-execDone)
for _, template := range templates {
_, err := client.Template(context.Background(), template.ID)
@ -80,7 +135,7 @@ func TestTemplateDelete(t *testing.T) {
execDone <- cmd.Execute()
}()
pty.WriteLine("docker-local")
pty.WriteLine("yes")
require.NoError(t, <-execDone)
_, err := client.Template(context.Background(), template.ID)