MCPcopy Index your code
hub / github.com/coder/coder / templateDelete

Method templateDelete

cli/templatedelete.go:16–86  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

14)
15
16func (r *RootCmd) templateDelete() *serpent.Command {
17 orgContext := NewOrganizationContext()
18 cmd := &serpent.Command{
19 Use: "delete [name...]",
20 Short: "Delete templates",
21 Options: serpent.OptionSet{
22 cliui.SkipPromptOption(),
23 },
24 Handler: func(inv *serpent.Invocation) error {
25 var (
26 ctx = inv.Context()
27 templateNames = []string{}
28 templates = []codersdk.Template{}
29 )
30 client, err := r.InitClient(inv)
31 if err != nil {
32 return err
33 }
34 organization, err := orgContext.Selected(inv, client)
35 if err != nil {
36 return err
37 }
38
39 if len(inv.Args) > 0 {
40 templateNames = inv.Args
41
42 for _, templateName := range templateNames {
43 template, err := client.TemplateByName(ctx, organization.ID, templateName)
44 if err != nil {
45 return xerrors.Errorf("get template by name: %w", err)
46 }
47 templates = append(templates, template)
48 }
49 } else {
50 template, err := selectTemplate(inv, client, organization)
51 if err != nil {
52 return err
53 }
54
55 templates = append(templates, template)
56 templateNames = append(templateNames, template.Name)
57 }
58
59 // Confirm deletion of the template.
60 _, err = cliui.Prompt(inv, cliui.PromptOptions{
61 Text: fmt.Sprintf("Delete these templates: %s?", pretty.Sprint(cliui.DefaultStyles.Code, strings.Join(templateNames, ", "))),
62 IsConfirm: true,
63 Default: cliui.ConfirmNo,
64 })
65 if err != nil {
66 return err
67 }
68
69 for _, template := range templates {
70 err := client.DeleteTemplate(ctx, template.ID)
71 if err != nil {
72 return xerrors.Errorf("delete template %q: %w", template.Name, err)
73 }

Callers 1

templatesMethod · 0.95

Calls 12

InitClientMethod · 0.95
SelectedMethod · 0.95
AttachOptionsMethod · 0.95
SkipPromptOptionFunction · 0.92
PromptFunction · 0.92
TimestampFunction · 0.92
NewOrganizationContextFunction · 0.85
selectTemplateFunction · 0.85
DeleteTemplateMethod · 0.80
ContextMethod · 0.65
TemplateByNameMethod · 0.65
ErrorfMethod · 0.45

Tested by

no test coverage detected