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"

"github.com/spf13/cobra"
"golang.org/x/xerrors"
Expand All @@ -11,7 +12,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 @@ -32,6 +33,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 @@ -57,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 @@ -82,4 +94,7 @@ func templateDelete() *cobra.Command {
return nil
},
}

cliui.AllowSkipPrompt(cmd)
return cmd
}