Documentation
¶
Overview ¶
Package scaffold renders directory trees from Go or Jet templates.
Templates can be supplied as an on-disk source directory or as an in-memory map of filenames to content. The rendered output is written atomically to a target directory; only files whose content has changed are copied, making repeated renders safe for use with existing targets when MergeTargetDirectory is enabled.
Built-in template functions include the Sprig library, a write() function that creates additional files from within a template, and a render() function that evaluates a partial template from the _partials subdirectory. Custom delimiters, post-processing commands, and caller-supplied template functions are all supported.
Example ¶
package main
import (
"fmt"
"os"
"path/filepath"
"text/template"
"github.com/choria-io/scaffold"
)
func main() {
base, _ := os.MkdirTemp("", "scaffold-example-")
defer os.RemoveAll(base)
target := filepath.Join(base, "output")
s, err := scaffold.New(scaffold.Config{
TargetDirectory: target,
SkipEmpty: true,
Source: map[string]any{
"README.md": "# {{ .Name }}\n\n{{ .Description }}\n",
"lib": map[string]any{
"main.go": `package {{ .Package }}
`,
},
"empty.txt": "{{ if .Include }}content{{ end }}",
},
}, template.FuncMap{})
if err != nil {
panic(err)
}
result, err := s.Render(map[string]any{
"Name": "My Project",
"Description": "A scaffolded project.",
"Package": "main",
"Include": false,
})
if err != nil {
panic(err)
}
readme, _ := os.ReadFile(filepath.Join(target, "README.md"))
fmt.Println(string(readme))
main, _ := os.ReadFile(filepath.Join(target, "lib", "main.go"))
fmt.Println(string(main))
_, statErr := os.Stat(filepath.Join(target, "empty.txt"))
fmt.Println("empty.txt exists:", !os.IsNotExist(statErr))
for _, f := range result {
fmt.Printf("%s: %s\n", f.Action, f.Path)
}
}
Output: # My Project A scaffolded project. package main empty.txt exists: false add: README.md add: lib/main.go
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// TargetDirectory is where to place the resulting rendered files, must not exist
TargetDirectory string `yaml:"target"`
// SourceDirectory reads templates from a directory, mutually exclusive with Source
SourceDirectory string `yaml:"source_directory"`
// MergeTargetDirectory writes into existing target directories
MergeTargetDirectory bool `yaml:"merge_target_directory"`
// Source reads templates from in-process memory
Source map[string]any `yaml:"source"`
// Post configures post-processing of files using filepath globs
Post []map[string]string `yaml:"post"`
// SkipEmpty skips files that are 0 bytes after rendering
SkipEmpty bool `yaml:"skip_empty"`
// Sets a custom template delimiter, useful for generating templates from templates
CustomLeftDelimiter string `yaml:"left_delimiter"`
// Sets a custom template delimiter, useful for generating templates from templates
CustomRightDelimiter string `yaml:"right_delimiter"`
}
Config configures a scaffolding operation
type FileAction ¶ added in v0.0.7
type FileAction string
FileAction represents the type of change a file would undergo during rendering
const ( FileActionAdd FileAction = "add" FileActionUpdate FileAction = "update" FileActionEqual FileAction = "equal" FileActionRemove FileAction = "remove" )
type ManagedFile ¶ added in v0.0.9
type ManagedFile struct {
Path string
Action FileAction
}
ManagedFile represents a file and the action that would be taken on it during rendering
type Scaffold ¶
type Scaffold struct {
// contains filtered or unexported fields
}
func (*Scaffold) Render ¶
func (s *Scaffold) Render(data any) ([]ManagedFile, error)
Render creates the target directory and places all files into it after template processing and post-processing. Files are rendered into a temporary directory first, then atomically copied to the real target. The returned slice describes every managed file and the action taken (add, update, equal).
func (*Scaffold) RenderNoop ¶ added in v0.0.7
func (s *Scaffold) RenderNoop(data any) ([]ManagedFile, error)
RenderNoop performs a full render into a temporary directory and compares the result against the real target directory. It returns a list of files with their planned action (add, update, equal, remove) without modifying the real target.