Documentation
¶
Overview ¶
Package template provides a template rendering engine for generating project files.
The template system uses Go's embed.FS to bundle template files into the binary, eliminating external dependencies and ensuring templates are always available at runtime. Templates use Go's text/template syntax for variable substitution.
Architecture ¶
The template system consists of three main components:
- Renderer interface - defines template rendering operations
- TemplateData struct - provides variables to templates
- Embedded templates - .tmpl files bundled via embed.FS
Templates are embedded from internal/templates/project/ and rendered using the Renderer interface with TemplateData for variable substitution.
Basic Usage ¶
Create a renderer and render a template:
import "github.com/anomalousventures/tracks/internal/templates"
renderer := template.NewRenderer(templates.FS)
data := template.TemplateData{
ModuleName: "github.com/user/myapp",
ProjectName: "myapp",
GoVersion: "1.25",
}
content, err := renderer.Render("go.mod.tmpl", data)
if err != nil {
// handle error
}
Rendering to Files ¶
Render directly to a file with automatic directory creation:
outputPath := filepath.Join(projectDir, "go.mod")
err := renderer.RenderToFile("go.mod.tmpl", data, outputPath)
Template Variables ¶
All templates have access to TemplateData fields:
{{.ModuleName}} - Go module path (e.g., github.com/user/myapp)
{{.ProjectName}} - Project name (e.g., myapp)
{{.DBDriver}} - Database driver (go-libsql, sqlite3, postgres)
{{.GoVersion}} - Go version (e.g., 1.25)
{{.Year}} - Current year for copyright
Adding New Templates ¶
To add a new template:
- Create a .tmpl file in internal/templates/project/
- Use {{.VariableName}} for variable substitution
- Add tests in templates_test.go
- Template path structure is preserved in output (cmd/server/main.go.tmpl → cmd/server/main.go)
Error Handling ¶
The package provides two error types:
TemplateError - wraps errors from file I/O and rendering ValidationError - reports template syntax errors
Both implement error unwrapping for error chain inspection.
Cross-Platform Support ¶
The package uses filepath for OS-specific paths and path for embed.FS paths, ensuring correct behavior on Windows, macOS, and Linux.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewRenderer ¶
func NewRenderer(fs embed.FS) interfaces.TemplateRenderer
NewRenderer creates a new template renderer that implements interfaces.TemplateRenderer. The provided embed.FS should contain template files in a "project" subdirectory.
Types ¶
type TemplateData ¶
type TemplateData struct {
// ModuleName is the full Go module path for the generated project.
// Example: "github.com/user/myapp"
ModuleName string
// ProjectName is the short name of the project, typically the last segment of the module path.
// Example: "myapp"
ProjectName string
// DBDriver specifies the database driver to use in the generated project.
// Valid values: "go-libsql", "sqlite3", "postgres"
DBDriver string
// GoVersion specifies the Go version to use in the generated project's go.mod file.
// Example: "1.25"
GoVersion string
// Year is the current year, used for copyright notices in generated files.
// Example: 2025
Year int
// EnvPrefix is the prefix for environment variables (used with Viper's SetEnvPrefix).
// Default: "APP"
// Example: "MYAPP" results in MYAPP_DATABASE_URL, MYAPP_SERVER_PORT, etc.
EnvPrefix string
// SecretKey is a cryptographically secure random key for session management.
SecretKey string
}
TemplateData contains all variables available to templates during rendering. This struct defines the complete schema of data that can be used in template files.
type TemplateError ¶
type TemplateError struct {
// Template is the name of the template that caused the error
Template string
// Err is the underlying error
Err error
}
TemplateError represents an error that occurred during template rendering. It wraps the underlying error with the template name for better context.
func (*TemplateError) Error ¶
func (e *TemplateError) Error() string
Error implements the error interface for TemplateError. It returns a formatted error message that includes the template name and underlying error.
func (*TemplateError) Unwrap ¶
func (e *TemplateError) Unwrap() error
Unwrap returns the underlying error for error chain unwrapping.
type ValidationError ¶
type ValidationError struct {
// Template is the name of the template being validated
Template string
// Field is the name of the field that failed validation (empty if not field-specific)
Field string
// Message describes the validation failure
Message string
}
ValidationError represents an error that occurred during template validation. It includes the template name, the problematic field, and a descriptive message.
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string
Error implements the error interface for ValidationError. It returns a formatted error message with template name, field, and message.