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

Function selectTemplate

cli/templates.go:46–76  ·  view source on GitHub ↗
(inv *serpent.Invocation, client *codersdk.Client, organization codersdk.Organization)

Source from the content-addressed store, hash-verified

44}
45
46func selectTemplate(inv *serpent.Invocation, client *codersdk.Client, organization codersdk.Organization) (codersdk.Template, error) {
47 var empty codersdk.Template
48 ctx := inv.Context()
49 allTemplates, err := client.TemplatesByOrganization(ctx, organization.ID)
50 if err != nil {
51 return empty, xerrors.Errorf("get templates by organization: %w", err)
52 }
53
54 if len(allTemplates) == 0 {
55 return empty, xerrors.Errorf("no templates exist in the current organization %q", organization.Name)
56 }
57
58 opts := make([]string, 0, len(allTemplates))
59 for _, template := range allTemplates {
60 opts = append(opts, template.Name)
61 }
62
63 selection, err := cliui.Select(inv, cliui.SelectOptions{
64 Options: opts,
65 })
66 if err != nil {
67 return empty, xerrors.Errorf("select template: %w", err)
68 }
69
70 for _, template := range allTemplates {
71 if template.Name == selection {
72 return template, nil
73 }
74 }
75 return empty, xerrors.Errorf("no template selected")
76}
77
78type templateTableRow struct {
79 // Used by json format:

Callers 2

templateDeleteMethod · 0.85

Calls 4

SelectFunction · 0.92
ContextMethod · 0.65
ErrorfMethod · 0.45

Tested by

no test coverage detected