Skip to content
31 changes: 23 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,15 @@ 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 +68,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 +95,7 @@ func templateDelete() *cobra.Command {
return nil
},
}

cliui.AllowSkipPrompt(cmd)
return cmd
}
46 changes: 44 additions & 2 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 @@ -32,7 +35,7 @@ func TestTemplateDelete(t *testing.T) {
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 +52,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 +62,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")

for _, template := range templates {
_, err := client.Template(context.Background(), template.ID)
require.Error(t, err, "template should not exist")
}

require.NoError(t, <-execDone)
})

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

Expand Down