mirror of
https://github.com/coder/coder.git
synced 2026-09-23 22:20:22 +08:00
Add the bundled `exampleID -> OS` Go map for Docker, Kubernetes, and AWS
EC2 Linux base templates. Create `.tf.tmpl` Go template files for each
within `coderd/templatebuilder/bases/`, along with `BaseRenderContext`
and `RenderBaseTemplate` rendering helpers.
The `.tf.tmpl` files are independent copies of the example templates
with module blocks (code-server, jetbrains) removed, since the template
builder composes modules separately into `modules.tf`. When
`ImageOptions` is provided, the container image field references the
Terraform parameter; otherwise it uses the hardcoded value via Go
template whitespace control (`{{-`).
Golden file snapshot tests verify rendered output stability with an
`-update` flag for regeneration.
Depends on #25909
> [!NOTE]
> This PR was authored by Coder Agents on behalf of @jeremyruppel.
48 lines
1.3 KiB
Go
48 lines
1.3 KiB
Go
package templatebuilder
|
|
|
|
import (
|
|
"bytes"
|
|
|
|
"golang.org/x/xerrors"
|
|
)
|
|
|
|
// ImageOption represents a container image choice for base template parameters.
|
|
type ImageOption struct {
|
|
Name string
|
|
Value string
|
|
}
|
|
|
|
// BaseRenderContext is the data passed to base template .tf.tmpl files.
|
|
type BaseRenderContext struct {
|
|
ContainerImage string
|
|
ImageOptions []ImageOption
|
|
Variables map[string]string
|
|
}
|
|
|
|
// RenderBaseTemplate executes a pre-parsed .tf.tmpl template for the given
|
|
// base, applying the provided render context. Templates are parsed once at
|
|
// startup; parse errors surface on first access rather than at render time.
|
|
func RenderBaseTemplate(exampleID, templatePath string, renderCtx BaseRenderContext) ([]byte, error) {
|
|
bases, err := loadBases()
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("load base catalog: %w", err)
|
|
}
|
|
|
|
base, ok := bases[exampleID]
|
|
if !ok {
|
|
return nil, xerrors.Errorf("unknown base template %q", exampleID)
|
|
}
|
|
|
|
tmpl, ok := base.Templates[templatePath]
|
|
if !ok {
|
|
return nil, xerrors.Errorf("template %s not found in base %q", templatePath, exampleID)
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
if err := tmpl.Execute(&buf, renderCtx); err != nil {
|
|
return nil, xerrors.Errorf("execute template %s: %w", templatePath, err)
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
}
|