()
| 14 | ) |
| 15 | |
| 16 | func (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 | } |
no test coverage detected