Skip to content

fix: confirm when deleting template #2866

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 10 commits into from
Jul 11, 2022
1 change: 1 addition & 0 deletions cli/templatecreate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 22 additions & 8 deletions cli/templatedelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"fmt"
"strings"
"time"

"github.com/spf13/cobra"
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -83,4 +94,7 @@ func templateDelete() *cobra.Command {
return nil
},
}

cliui.AllowSkipPrompt(cmd)
return cmd
}
63 changes: 59 additions & 4 deletions cli/templatedelete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -25,14 +28,27 @@ 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})
Expand All @@ -49,7 +65,7 @@ func TestTemplateDelete(t *testing.T) {
templateNames = append(templateNames, template.Name)
}

cmd, root := clitest.New(t, append([]string{"templates", "delete"}, templateNames...)...)
cmd, root := clitest.New(t, append([]string{"templates", "delete", "--yes"}, templateNames...)...)
clitest.SetupConfig(t, client, root)
require.NoError(t, cmd.Execute())

Expand All @@ -59,6 +75,45 @@ func TestTemplateDelete(t *testing.T) {
}
})

t.Run("Multiple prompted", 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"}, templateNames...)...)
clitest.SetupConfig(t, client, root)
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)
require.Error(t, err, "template should not exist")
}
})

t.Run("Selector", func(t *testing.T) {
t.Parallel()

Expand All @@ -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)
Expand Down