Files
coder/cli/templatedelete.go
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

88 lines
2.1 KiB
Go
Raw Normal View History

2022-05-13 17:54:32 -05:00
package cli
import (
"fmt"
2022-07-11 13:13:56 -05:00
"strings"
"time"
2022-05-13 17:54:32 -05:00
"golang.org/x/xerrors"
"github.com/coder/pretty"
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/serpent"
2022-05-13 17:54:32 -05:00
)
2024-03-17 09:45:26 -05:00
func (r *RootCmd) templateDelete() *serpent.Command {
orgContext := NewOrganizationContext()
2023-03-23 17:42:20 -05:00
client := new(codersdk.Client)
2024-03-17 09:45:26 -05:00
cmd := &serpent.Command{
2022-05-13 17:54:32 -05:00
Use: "delete [name...]",
Short: "Delete templates",
Middleware: serpent.Chain(
2023-03-23 17:42:20 -05:00
r.InitClient(client),
),
Options: serpent.OptionSet{
2023-03-23 17:42:20 -05:00
cliui.SkipPromptOption(),
},
Handler: func(inv *serpent.Invocation) error {
2022-05-13 17:54:32 -05:00
var (
2023-03-23 17:42:20 -05:00
ctx = inv.Context()
2022-05-13 17:54:32 -05:00
templateNames = []string{}
templates = []codersdk.Template{}
)
organization, err := orgContext.Selected(inv, client)
2022-05-13 17:54:32 -05:00
if err != nil {
return err
}
2023-03-23 17:42:20 -05:00
if len(inv.Args) > 0 {
templateNames = inv.Args
2022-07-11 13:13:56 -05:00
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)
}
2022-05-13 17:54:32 -05:00
} else {
template, err := selectTemplate(inv, client, organization)
2022-05-13 17:54:32 -05:00
if err != nil {
return err
2022-05-13 17:54:32 -05:00
}
templates = append(templates, template)
templateNames = append(templateNames, template.Name)
2022-05-13 17:54:32 -05:00
}
2022-07-11 13:13:56 -05:00
// Confirm deletion of the template.
2023-03-23 17:42:20 -05:00
_, err = cliui.Prompt(inv, cliui.PromptOptions{
Text: fmt.Sprintf("Delete these templates: %s?", pretty.Sprint(cliui.DefaultStyles.Code, strings.Join(templateNames, ", "))),
2022-07-11 13:13:56 -05:00
IsConfirm: true,
Default: cliui.ConfirmNo,
2022-07-11 13:13:56 -05:00
})
if err != nil {
return err
2022-05-13 17:54:32 -05:00
}
for _, template := range templates {
err := client.DeleteTemplate(ctx, template.ID)
if err != nil {
return xerrors.Errorf("delete template %q: %w", template.Name, err)
}
_, _ = fmt.Fprintln(
inv.Stdout, "Deleted template "+pretty.Sprint(cliui.DefaultStyles.Keyword, template.Name)+" at "+cliui.Timestamp(time.Now()),
)
2022-05-13 17:54:32 -05:00
}
return nil
},
}
orgContext.AttachOptions(cmd)
2022-07-11 13:13:56 -05:00
return cmd
2022-05-13 17:54:32 -05:00
}