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

Method templateInit

cli/templateinit.go:23–132  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

21)
22
23func (*RootCmd) templateInit() *serpent.Command {
24 var templateID string
25 exampleList, err := examples.List()
26 if err != nil {
27 // This should not happen. If it does, something is very wrong.
28 panic(err)
29 }
30 var templateIDs []string
31 for _, ex := range exampleList {
32 templateIDs = append(templateIDs, ex.ID)
33 }
34 slices.Sort(templateIDs)
35 cmd := &serpent.Command{
36 Use: "init [directory]",
37 Short: "Get started with a templated template.",
38 Middleware: serpent.RequireRangeArgs(0, 1),
39 Handler: func(inv *serpent.Invocation) error {
40 // If the user didn't specify any template, prompt them to select one.
41 if templateID == "" {
42 optsToID := map[string]string{}
43 for _, example := range exampleList {
44 name := fmt.Sprintf(
45 "%s\n%s\n%s\n",
46 cliui.Bold(example.Name),
47 pretty.Sprint(cliui.DefaultStyles.Wrap.With(pretty.XPad(6, 0)), example.Description),
48 pretty.Sprint(cliui.DefaultStyles.Keyword.With(pretty.XPad(6, 0)), example.URL),
49 )
50 optsToID[name] = example.ID
51 }
52 opts := maps.Keys(optsToID)
53 slices.Sort(opts)
54 _, _ = fmt.Fprintln(
55 inv.Stdout,
56 pretty.Sprint(
57 cliui.DefaultStyles.Wrap,
58 "A template defines infrastructure as code to be provisioned "+
59 "for individual developer workspaces. Select an example to be copied to the active directory:\n"),
60 )
61 selected, err := cliui.Select(inv, cliui.SelectOptions{
62 Options: opts,
63 })
64 if err != nil {
65 if errors.Is(err, io.EOF) {
66 return xerrors.Errorf(
67 "Couldn't find a matching template!\n" +
68 "Tip: if you're trying to automate template creation, try\n" +
69 "coder templates init --id <template_id> instead!",
70 )
71 }
72 return err
73 }
74 templateID = optsToID[selected]
75 }
76
77 selectedTemplate, ok := templateByID(templateID, exampleList)
78 if !ok {
79 // serpent.EnumOf would normally handle this.
80 return xerrors.Errorf("template not found: %q", templateID)

Callers 1

templatesMethod · 0.95

Calls 10

ListFunction · 0.92
BoldFunction · 0.92
SelectFunction · 0.92
ArchiveFunction · 0.92
UntarFunction · 0.92
templateByIDFunction · 0.85
KeysMethod · 0.80
MkdirAllMethod · 0.80
IsMethod · 0.45
ErrorfMethod · 0.45

Tested by

no test coverage detected