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

Function setupStarterTemplate

scripts/develop/main.go:1027–1071  ·  view source on GitHub ↗

setupStarterTemplate creates a template from a starter example. For starters tagged with "docker", it checks Docker availability and resolves the Docker host for template variables.

(ctx context.Context, logger slog.Logger, cfg *devConfig, client *codersdk.Client)

Source from the content-addressed store, hash-verified

1025// For starters tagged with "docker", it checks Docker availability
1026// and resolves the Docker host for template variables.
1027func setupStarterTemplate(ctx context.Context, logger slog.Logger, cfg *devConfig, client *codersdk.Client) error {
1028 templateID := cfg.starterTemplate
1029
1030 // Fetch starter template metadata from the running coderd.
1031 examples, err := client.StarterTemplates(ctx)
1032 if err != nil {
1033 return xerrors.Errorf("fetch starter templates failed: %w", err)
1034 }
1035 example, ok := slice.Find(examples, func(e codersdk.TemplateExample) bool {
1036 return e.ID == templateID
1037 })
1038 if !ok {
1039 return xerrors.Errorf("starter template %q not found", templateID)
1040 }
1041
1042 // Docker-specific: check availability and resolve host.
1043 var userVars []codersdk.VariableValue
1044 if slices.Contains(example.Tags, "docker") {
1045 if err := exec.CommandContext(ctx, "docker", "info").Run(); err != nil {
1046 logger.Debug(ctx, "docker not available, skipping template setup")
1047 return nil
1048 }
1049 dockerHost := ""
1050 if out, err := exec.CommandContext(ctx, "docker", "context", "inspect",
1051 "--format", "{{ .Endpoints.docker.Host }}").Output(); err == nil {
1052 dockerHost = strings.TrimSpace(string(out))
1053 }
1054 userVars = []codersdk.VariableValue{
1055 {Name: "docker_arch", Value: runtime.GOARCH},
1056 {Name: "docker_host", Value: dockerHost},
1057 }
1058 }
1059
1060 if err := createTemplateInOrg(ctx, logger, client, codersdk.DefaultOrganization, example, userVars); err != nil {
1061 return err
1062 }
1063
1064 if cfg.multiOrg {
1065 if err := createTemplateInOrg(ctx, logger, client, "second-organization", example, userVars); err != nil {
1066 logger.Warn(ctx, "failed to create starter template in second org", slog.Error(err))
1067 }
1068 }
1069
1070 return nil
1071}
1072
1073// waitForVersion polls until a template version's provisioner job
1074// reaches a terminal state.

Callers 1

developFunction · 0.85

Calls 9

FindFunction · 0.92
createTemplateInOrgFunction · 0.85
StarterTemplatesMethod · 0.80
RunMethod · 0.65
CommandContextMethod · 0.65
OutputMethod · 0.65
ErrorfMethod · 0.45
ContainsMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected