copier

package module
v0.0.0-...-12e6fe8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 6, 2026 License: MIT Imports: 27 Imported by: 0

README

copier-go

copier-go is a Go implementation of Copier, the Python project templating and update tool.

The goal of this repository is a 1-to-1 behavior port of upstream Copier in the Go language. Templates should keep using Copier's existing template format, copier.yml configuration, answers files, Git tag based updates, and CLI workflow. This project is not intended to define a different template system.

Compatibility with upstream Copier is the target, not a claim that every upstream feature is already complete. Known differences and implementation gaps are listed below.

Relationship To Upstream Copier

Upstream Copier is the source of truth for behavior and compatibility. This Go port tracks upstream Copier changes and ports the behavior where it applies to a native Go implementation.

Use upstream Copier documentation when writing templates unless this README explicitly says otherwise:

https://copier.readthedocs.io/

Why A Go Port

  • Single native binary distribution.
  • Embeddable Go library API for Go programs.
  • No Python runtime requirement for users of the Go CLI.
  • Same Copier concepts for copying, recopying, updating, questionnaires, tasks, and answers files.

Current Compatibility

Implemented core behavior includes:

  • copy, recopy, update, and check-update CLI commands.
  • Go library API for Copy, Recopy, Update, and CheckUpdate.
  • Local path and Git template sources, including GitHub and GitLab shortcuts.
  • Latest semver Git tag selection, prerelease handling, pinned refs, and stored template metadata.
  • copier.yml and copier.yaml template configuration.
  • Interactive and defaulted questions with layered answer precedence.
  • Jinja-like rendering through pongo2, including common Copier filters such as to_yaml, to_nice_yaml, to_json, bool, and basename.
  • Configurable template delimiters through _envops.
  • _envops.undefined: jinja2.StrictUndefined error behavior for missing top-level variables.
  • _subdirectory, _exclude, _skip_if_exists, _answers_file, _secret_questions, _external_data, _preserve_symlinks, and template messages.
  • Core task and migration execution support.
  • Unsafe-feature gating for tasks, migrations, Jinja extensions, and external data reads outside the destination.
  • Three-way update flow using Git diffs.
  • Executable-bit preservation during copy and update.

Differences From Upstream Copier

The intended user-facing behavior is the same, but this is not the same codebase. Practical differences are:

Area Upstream Copier copier-go
Implementation language Python Go
Distribution Python package and CLI Go module and native CLI binary
Library API Python functions/classes Go functions and functional options
Template engine Jinja2 pongo2 Jinja-like engine
Python Jinja extensions Loadable Python extensions Not executed as Python extensions in Go
Plugin ecosystem Python package ecosystem Go implementation only
Exact edge cases Defined by upstream Copier and Jinja2 Ported where practical, but renderer edge cases can differ
Configuration loader Supports upstream YAML includes and multi-document merging Basic copier.yml/copier.yaml parsing today
Pattern matching PathSpec/gitignore behavior Glob-based matching today
Update algorithm Upstream Python update algorithm Go implementation with Git diff based merge
CLI surface Full upstream Python CLI Core commands and flags implemented

Templates that use standard Copier configuration and ordinary Jinja syntax should be the compatibility target. Templates that depend on custom Python Jinja extensions, Python-only filters, or very specific Jinja2 internals may need equivalent Go support before they work here.

Known Gaps

These are compatibility gaps, not intended product differences:

  • Custom Python Jinja extensions are not loaded or executed by the Go renderer.
  • Only a subset of upstream Copier's built-in filters and Jinja environment behavior is implemented.
  • The YAML configuration loader does not yet implement upstream !include handling or multi-document merge semantics.
  • Exclude and skip matching use glob behavior, not the full upstream PathSpec/gitignore semantics.
  • Some newer upstream task and migration schema details may need additional porting.
  • The update algorithm is implemented in Go but is not yet guaranteed to match every upstream conflict and edge-case behavior.
  • The CLI exposes the core workflow but not every upstream Python CLI option.

Install And Build

Build the CLI from this repository:

make build

Run tests:

make test

Run linting:

make lint

CLI Usage

Copy a template:

copier copy gh:org/template ./my-project

Update an existing project from its recorded template:

copier update ./my-project

Check whether a project has a newer template version:

copier check-update ./my-project

Recopy a project from its template using existing answers:

copier recopy ./my-project

Go Library Usage

package main

import copier "github.com/fyltr/copier-go"

func main() {
	_ = copier.Copy(
		"gh:org/template",
		"./my-project",
		copier.WithData(map[string]any{"project_name": "my-project"}),
		copier.WithDefaults(true),
	)
}

Sync Policy

When upstream Copier changes behavior, the Go port should prefer matching upstream semantics over inventing Go-specific behavior. Differences should be documented here and reduced over time when they are implementation gaps rather than intentional Go API differences.

License

This project uses the MIT license, matching upstream Copier.

Documentation

Overview

Package copier provides a Go port of Copier for rendering project templates.

It tracks the behavior of the upstream Python project at https://github.com/copier-org/copier while exposing a native Go library API. Copier templates, answers files, Git-based updates, and questionnaires are intended to remain compatible with upstream Copier wherever practical.

Use Copy, Update, and Recopy as the main entry points.

Index

Constants

View Source
const AnswersFileName = ".copier-answers.yml"

AnswersFileName is the default file for storing copier answers.

View Source
const DefaultTemplateSuffix = ".jinja"

DefaultTemplateSuffix is the suffix identifying Jinja template files.

Variables

View Source
var (
	ErrUnsupportedVersion = errors.New("copier version not supported by template")
	ErrConfig             = errors.New("template configuration error")
	ErrMultipleConfigs    = errors.New("both copier.yml and copier.yaml found")
	ErrUnsafeTemplate     = errors.New("template uses unsafe features; use WithUnsafe(true) or trust the repository")
	ErrExtensionNotFound  = errors.New("jinja extension not found")
	ErrInteractiveNeeded  = errors.New("interactive session required but not available")
	ErrInvalidType        = errors.New("invalid question type")
	ErrPathNotAbsolute    = errors.New("path must be absolute")
	ErrPathNotRelative    = errors.New("path must be relative")
	ErrForbiddenPath      = errors.New("path escapes destination directory")
	ErrYieldInFile        = errors.New("yield tag found in file content")
	ErrMultipleYields     = errors.New("multiple yield tags in a single path segment")
	ErrTaskFailed         = errors.New("task execution failed")
	ErrInterrupted        = errors.New("operation interrupted by user")
)

Sentinel errors for type-checking with errors.Is.

View Source
var DefaultExclude = []string{
	"copier.yaml",
	"copier.yml",
	"~*",
	"*.py[co]",
	"__pycache__",
	".git",
	".DS_Store",
	".svn",
}

DefaultExclude contains patterns excluded from all template renders.

Functions

func CloneTemplate

func CloneTemplate(url, ref string, usePreReleases bool) (localPath string, resolvedRef string, err error)

CloneTemplate clones a git template to a temporary directory and checks out the specified ref. If ref is empty, the latest semver tag is used.

Uses the git CLI for cloning so that SSH keys, credential helpers, and other user-configured auth mechanisms work transparently.

func Copy

func Copy(src, dst string, opts ...Option) error

Copy scaffolds a new project from a template.

src is the template source (local path or Git URL). dst is the destination directory (created if it does not exist).

err := copier.Copy("gh:user/template", "./myproject",
    copier.WithData(map[string]any{"project_name": "myapp"}),
)

func CopyDir

func CopyDir(src, dst string) error

CopyDir recursively copies a directory tree, preserving permissions and symlinks.

func CopyFile

func CopyFile(src, dst string) error

CopyFile copies a single file preserving permissions.

func GitAdd

func GitAdd(repoPath string) error

GitAdd stages all files in a repository.

func GitApplyDiff

func GitApplyDiff(repoPath string, diffContent []byte, contextLines int, reject bool) error

GitApplyDiff applies a unified diff to a repository using the git command line. go-git does not support git apply, so we shell out.

func GitCommit

func GitCommit(repoPath, message string) error

GitCommit creates a commit with the given message.

func GitDiff

func GitDiff(repoPath string, contextLines int) ([]byte, error)

GitDiff produces a unified diff between two paths using the git command line.

func GitInit

func GitInit(path string) error

GitInit initialises a new git repository at the given path.

func IsBinary

func IsBinary(path string) (bool, error)

IsBinary performs a simple heuristic to detect binary files by checking the first 8KB for null bytes.

func IsGitInstalled

func IsGitInstalled() bool

IsGitInstalled reports whether git is available on PATH.

func IsGitURL

func IsGitURL(url string) bool

IsGitURL reports whether url points to a Git repository.

func IsTemplateSuffix

func IsTemplateSuffix(path, suffix string) bool

IsTemplateSuffix reports whether the file path ends with the template suffix.

func LoadAnswersFile

func LoadAnswersFile(path string) (map[string]any, error)

LoadAnswersFile reads a .copier-answers.yml file.

func NormalizeURL

func NormalizeURL(rawURL string) (string, bool)

NormalizeURL expands shorthand URLs and detects git repositories. Supported shorthands: gh:org/repo, gl:org/repo, git+<url>.

func ParseAnswer

func ParseAnswer(q QuestionDef, raw any) (any, error)

ParseAnswer converts a raw answer value to the expected Go type for the question.

func Recopy

func Recopy(dst string, opts ...Option) error

Recopy re-applies a template to a project, discarding the project's evolution and using the existing answers.

err := copier.Recopy("./myproject")

func RepoCommitDescription

func RepoCommitDescription(repoPath string) (string, error)

RepoCommitDescription returns a git describe-style reference for HEAD.

func RepoCommitHash

func RepoCommitHash(repoPath string) (string, error)

RepoCommitHash returns the HEAD commit hash for a local git repository.

func ResolveDefault

func ResolveDefault(q QuestionDef, answers *AnswersMap, settings *Settings) any

ResolveDefault returns the effective default for a question given all answer sources. Priority: Init → Last → UserDefaults → Settings → Question.Default.

func ShouldAsk

func ShouldAsk(q QuestionDef, renderer *Renderer, answers map[string]any) bool

ShouldAsk evaluates the when condition for a question. Returns true if the question should be shown.

func StripTemplateSuffix

func StripTemplateSuffix(path, suffix string) string

StripTemplateSuffix removes the template suffix from a path.

func SyncGitIndexExecutableBit

func SyncGitIndexExecutableBit(dstRoot, dstPath string, srcMode os.FileMode)

SyncGitIndexExecutableBit updates the destination git index mode when git is configured to ignore filesystem mode changes.

func Update

func Update(dst string, opts ...Option) error

Update updates an existing project to a newer version of its template. The destination must contain a .copier-answers.yml from a previous Copy.

err := copier.Update("./myproject",
    copier.WithConflict(copier.ConflictInline),
)

func ValidateAnswer

func ValidateAnswer(q QuestionDef, answer any, renderer *Renderer, answers map[string]any) error

ValidateAnswer runs the question's validator template. Returns nil if valid, or a ValidationError if the validator returns a non-empty string.

func WalkTemplate

func WalkTemplate(root string, excludeMatcher *PatternMatcher, fn func(relPath string, d fs.DirEntry) error) error

WalkTemplate walks the template directory and calls fn for each file/dir that is not excluded. Paths passed to fn are relative to root.

func WriteAnswersFile

func WriteAnswersFile(path string, answers map[string]any, metadata map[string]any) error

WriteAnswersFile writes the copier answers to the destination.

Types

type AnswersMap

type AnswersMap struct {
	User         map[string]any  // Interactive answers from the current session.
	Init         map[string]any  // Pre-set answers from --data flags.
	Metadata     map[string]any  // Template metadata (_src_path, _commit).
	Last         map[string]any  // Answers from a previous .copier-answers.yml.
	UserDefaults map[string]any  // User-configured defaults from settings.
	Hidden       map[string]bool // Questions whose answers should not be persisted.
}

AnswersMap holds layered answer data with defined precedence. Lookup order: User → Init → Metadata → Last → UserDefaults → Builtin.

func NewAnswersMap

func NewAnswersMap() *AnswersMap

NewAnswersMap creates an AnswersMap with initialised maps.

func (*AnswersMap) Combined

func (a *AnswersMap) Combined() map[string]any

Combined returns a flat map merging all layers in precedence order.

func (*AnswersMap) Get

func (a *AnswersMap) Get(key string) (any, bool)

Get looks up a value following the precedence chain.

func (*AnswersMap) Remembered

func (a *AnswersMap) Remembered() map[string]any

Remembered returns answers suitable for writing to the answers file, excluding hidden questions and metadata.

type CheckUpdateResult

type CheckUpdateResult struct {
	UpdateAvailable bool   `json:"update_available"`
	CurrentVersion  string `json:"current_version"`
	LatestVersion   string `json:"latest_version"`
}

CheckUpdateResult describes whether a rendered project has a newer template version.

func CheckUpdate

func CheckUpdate(dst string, opts ...Option) (CheckUpdateResult, error)

CheckUpdate checks whether dst was generated from an older template version.

type Config

type Config struct {
	// SrcPath is the template source (local path or Git URL).
	SrcPath string

	// DstPath is the destination directory.
	DstPath string

	// AnswersFile overrides the default answers file path (.copier-answers.yml).
	AnswersFile string

	// VcsRef selects a specific Git tag, branch, or commit. Empty means latest tag.
	VcsRef string

	// Data provides pre-set answers that skip interactive prompting.
	Data map[string]any

	// Defaults uses default values for all questions without prompting.
	Defaults bool

	// UserDefaults supplies fallback defaults for questions (lower priority than Data).
	UserDefaults map[string]any

	// Overwrite replaces existing files without asking.
	Overwrite bool

	// Skip lists patterns of files to leave untouched if they already exist.
	Skip []string

	// Exclude lists additional patterns of template files to ignore.
	Exclude []string

	// Pretend performs a dry run without writing any files.
	Pretend bool

	// Quiet suppresses informational output.
	Quiet bool

	// Unsafe allows templates that use tasks or other potentially dangerous features.
	Unsafe bool

	// SkipTasks skips execution of pre/post-copy tasks.
	SkipTasks bool

	// SkipAnswered skips questions whose answers are already known (update/recopy).
	SkipAnswered bool

	// UsePreReleases includes pre-release Git tags when selecting the latest version.
	UsePreReleases bool

	// CleanupOnError removes the destination directory if an error occurs during copy.
	CleanupOnError bool

	// Conflict sets the merge conflict strategy for updates ("inline" or "rej").
	Conflict ConflictStrategy

	// ContextLines sets the number of context lines in diffs for updates.
	ContextLines int
}

Config holds all configuration for a copier operation. Use functional Option values to construct one via Copy, Update, or Recopy.

type ConflictStrategy

type ConflictStrategy string

ConflictStrategy defines how merge conflicts are handled during updates.

const (
	ConflictInline ConflictStrategy = "inline"
	ConflictReject ConflictStrategy = "rej"
)

type Envops

type Envops struct {
	BlockStartString    string `yaml:"block_start_string"`
	BlockEndString      string `yaml:"block_end_string"`
	VariableStartString string `yaml:"variable_start_string"`
	VariableEndString   string `yaml:"variable_end_string"`
	CommentStartString  string `yaml:"comment_start_string"`
	CommentEndString    string `yaml:"comment_end_string"`
	Undefined           string `yaml:"undefined"`
}

Envops configures template delimiters (mirrors Jinja2's Environment options).

func DefaultEnvops

func DefaultEnvops() Envops

DefaultEnvops returns pongo2/Jinja2 standard delimiters.

type InterruptError

type InterruptError struct {
	PartialAnswers map[string]any
}

InterruptError carries partial answers when the user interrupts a prompt session.

func (*InterruptError) Error

func (e *InterruptError) Error() string

func (*InterruptError) Unwrap

func (e *InterruptError) Unwrap() error

type LazyMap

type LazyMap[V any] struct {
	// contains filtered or unexported fields
}

LazyMap is a concurrent-safe map where values are lazily computed on first access. Compute functions are called at most once per key.

func NewLazyMap

func NewLazyMap[V any](funcs map[string]func() V) *LazyMap[V]

NewLazyMap creates a LazyMap with the given compute functions.

func (*LazyMap[V]) Get

func (m *LazyMap[V]) Get(key string) (V, bool)

Get retrieves a value, computing it on first access.

func (*LazyMap[V]) Keys

func (m *LazyMap[V]) Keys() []string

Keys returns all registered keys.

func (*LazyMap[V]) Set

func (m *LazyMap[V]) Set(key string, val V)

Set stores a pre-computed value directly.

type MigrationDef

type MigrationDef struct {
	Version string    `yaml:"version"`
	Before  []TaskDef `yaml:"before"`
	After   []TaskDef `yaml:"after"`
}

MigrationDef defines a migration step between template versions.

type Operation

type Operation string

Operation represents the type of copier operation being performed.

const (
	OpCopy   Operation = "copy"
	OpUpdate Operation = "update"
)

type Option

type Option func(*Config)

Option configures a copier operation.

func WithAnswersFile

func WithAnswersFile(path string) Option

WithAnswersFile sets a custom path for the copier answers file.

func WithCleanupOnError

func WithCleanupOnError(v bool) Option

WithCleanupOnError removes the destination on failure (default: true).

func WithConflict

func WithConflict(s ConflictStrategy) Option

WithConflict sets the merge conflict strategy for updates.

func WithContextLines

func WithContextLines(n int) Option

WithContextLines sets the number of diff context lines for updates.

func WithData

func WithData(data map[string]any) Option

WithData provides answers that bypass interactive prompting.

func WithDefaults

func WithDefaults(v bool) Option

WithDefaults uses all default values instead of prompting.

func WithExclude

func WithExclude(patterns ...string) Option

WithExclude adds patterns for template files to exclude from rendering.

func WithOverwrite

func WithOverwrite(v bool) Option

WithOverwrite allows overwriting existing files without confirmation.

func WithPreReleases

func WithPreReleases(v bool) Option

WithPreReleases includes pre-release tags when selecting the latest version.

func WithPretend

func WithPretend(v bool) Option

WithPretend enables dry-run mode (no files written).

func WithQuiet

func WithQuiet(v bool) Option

WithQuiet suppresses informational output.

func WithSkip

func WithSkip(patterns ...string) Option

WithSkip adds patterns for files to skip if they already exist.

func WithSkipAnswered

func WithSkipAnswered(v bool) Option

WithSkipAnswered skips questions that already have answers from a previous run.

func WithSkipTasks

func WithSkipTasks(v bool) Option

WithSkipTasks disables execution of template tasks.

func WithUnsafe

func WithUnsafe(v bool) Option

WithUnsafe allows templates with tasks or other potentially dangerous features.

func WithUserDefaults

func WithUserDefaults(defaults map[string]any) Option

WithUserDefaults supplies fallback defaults for questions.

func WithVcsRef

func WithVcsRef(ref string) Option

WithVcsRef pins the template to a specific Git reference (tag, branch, commit).

type PatternMatcher

type PatternMatcher struct {
	// contains filtered or unexported fields
}

PatternMatcher compiles glob patterns and provides matching against relative paths.

func NewPatternMatcher

func NewPatternMatcher(patterns []string) *PatternMatcher

NewPatternMatcher compiles the given glob patterns.

func (*PatternMatcher) Matches

func (m *PatternMatcher) Matches(path string) bool

Matches reports whether path matches any of the compiled patterns.

type Phase

type Phase string

Phase represents the current stage of a copier operation.

const (
	PhaseUndefined Phase = "undefined"
	PhasePrompt    Phase = "prompt"
	PhaseRender    Phase = "render"
	PhaseTasks     Phase = "tasks"
	PhaseMigrate   Phase = "migrate"
)

type Prompter

type Prompter interface {
	Ask(q QuestionDef, currentAnswers map[string]any) (any, error)
	Confirm(message string, defaultVal bool) (bool, error)
}

Prompter handles interactive question prompting. It is an interface to allow testing and alternative UIs.

type QuestionDef

type QuestionDef struct {
	Name        string
	Type        QuestionType `yaml:"type"`
	Help        string       `yaml:"help"`
	Choices     any          `yaml:"choices"` // []any or map[string]any or string (Jinja)
	Multiselect bool         `yaml:"multiselect"`
	Default     any          `yaml:"default"`
	Secret      bool         `yaml:"secret"`
	Multiline   any          `yaml:"multiline"` // bool or string
	Placeholder string       `yaml:"placeholder"`
	Validator   string       `yaml:"validator"`
	When        any          `yaml:"when"` // bool or string (Jinja condition)
}

QuestionDef holds the raw question definition from copier.yml.

type QuestionError

type QuestionError struct {
	Name string
	Err  error
}

QuestionError wraps errors related to question processing.

func (*QuestionError) Error

func (e *QuestionError) Error() string

func (*QuestionError) Unwrap

func (e *QuestionError) Unwrap() error

type QuestionType

type QuestionType string

QuestionType defines the expected answer type for a question.

const (
	TypeStr   QuestionType = "str"
	TypeInt   QuestionType = "int"
	TypeFloat QuestionType = "float"
	TypeBool  QuestionType = "bool"
	TypeYAML  QuestionType = "yaml"
	TypeJSON  QuestionType = "json"
	TypePath  QuestionType = "path"
)

type Renderer

type Renderer struct {
	// contains filtered or unexported fields
}

Renderer handles Jinja2-compatible template rendering using pongo2.

func NewRenderer

func NewRenderer(baseCtx map[string]any, templateDir string, envops ...Envops) *Renderer

NewRenderer creates a Renderer with the given base context, template directory, and optional custom delimiters.

func (*Renderer) RenderFile

func (r *Renderer) RenderFile(srcPath, dstPath string, extra map[string]any) error

RenderFile renders a template file to the destination path.

func (*Renderer) RenderPath

func (r *Renderer) RenderPath(pathTemplate string, extra map[string]any) ([]string, error)

RenderPath renders a path string, expanding template expressions in path segments. Returns all expanded paths (multiple if yield tags are used).

func (*Renderer) RenderString

func (r *Renderer) RenderString(template string, extra map[string]any) (string, error)

RenderString renders a template string with the given extra context. If custom envops are configured, delimiters are translated before parsing.

type Settings

type Settings struct {
	// Defaults maps question names to default values.
	Defaults map[string]any `yaml:"defaults,omitempty"`

	// Trust lists repository URLs or prefixes that are allowed to run unsafe features.
	Trust []string `yaml:"trust,omitempty"`
}

Settings holds user-level copier configuration loaded from disk.

func LoadSettings

func LoadSettings() (*Settings, error)

LoadSettings reads the user settings file. It checks, in order:

  1. $COPIER_SETTINGS_PATH
  2. <XDG_CONFIG_HOME>/copier/settings.yml

Returns an empty Settings (not an error) if no file exists.

func (*Settings) DefaultFor

func (s *Settings) DefaultFor(name string) (any, bool)

DefaultFor returns the default value for a question, if one is configured.

func (*Settings) IsTrusted

func (s *Settings) IsTrusted(repo string) bool

IsTrusted checks whether repo matches any entry in the trust list. An entry matches exactly, or as a prefix if it ends with "/".

type TaskDef

type TaskDef struct {
	Cmd              any    `yaml:"cmd"`       // string or []string
	Condition        any    `yaml:"condition"` // string or bool, default true
	WorkingDirectory string `yaml:"working_directory"`
}

TaskDef defines a command to run during template operations.

func (TaskDef) CmdArgs

func (t TaskDef) CmdArgs() []string

CmdArgs returns the command as a slice of arguments.

func (TaskDef) CmdString

func (t TaskDef) CmdString() string

CmdString returns the command as a single string.

type TaskExecError

type TaskExecError struct {
	Cmd      string
	ExitCode int
	Err      error
}

TaskExecError wraps errors from task execution with the command and exit code.

func (*TaskExecError) Error

func (e *TaskExecError) Error() string

func (*TaskExecError) Unwrap

func (e *TaskExecError) Unwrap() error

type Template

type Template struct {
	// URL is the original template URL or local path.
	URL string

	// LocalPath is the absolute path to the template on disk (cloned or direct).
	LocalPath string

	// Temporary is true when LocalPath points to a clone that should be removed.
	Temporary bool

	// Ref is the resolved git reference (tag, branch, commit).
	Ref string

	// CommitHash is the full git commit hash of the checked-out template.
	CommitHash string

	// CommitDescription is the git describe-style reference saved for updates.
	CommitDescription string

	// Config holds parsed copier.yml settings (keys prefixed with _).
	Config TemplateConfig

	// Questions holds parsed question definitions (non-prefixed keys).
	Questions []QuestionDef
	// contains filtered or unexported fields
}

Template represents a loaded copier template with its configuration.

func LoadTemplate

func LoadTemplate(url, ref string, usePreReleases bool) (*Template, error)

LoadTemplate loads a template from a local path or Git URL. If url is a Git URL, it clones the repository. ref selects a specific version.

func (*Template) Cleanup

func (t *Template) Cleanup()

Cleanup removes temporary template clones created while loading Git templates.

func (*Template) CopyRoot

func (t *Template) CopyRoot() string

CopyRoot returns the root directory for template files, accounting for subdirectory.

func (*Template) Exclusions

func (t *Template) Exclusions() []string

Exclusions returns the combined list of exclude patterns (defaults + template config).

func (*Template) IsSecret

func (t *Template) IsSecret(name string) bool

IsSecret reports whether the named question is marked as secret.

func (*Template) Metadata

func (t *Template) Metadata() map[string]any

Metadata returns template metadata to embed in the answers file.

func (*Template) MigrationTasks

func (t *Template) MigrationTasks(stage string, fromVer, toVer *semver.Version) []TaskDef

MigrationTasks returns tasks for a given migration stage (before/after) filtered by version range.

func (*Template) MinVersion

func (t *Template) MinVersion() *semver.Version

MinVersion returns the parsed minimum copier version, or nil if not set.

type TemplateConfig

type TemplateConfig struct {
	AnswersFile      string            `yaml:"_answers_file"`
	Subdirectory     string            `yaml:"_subdirectory"`
	TemplateSuffix   string            `yaml:"_templates_suffix"`
	Exclude          []string          `yaml:"_exclude"`
	SkipIfExists     []string          `yaml:"_skip_if_exists"`
	Tasks            []TaskDef         `yaml:"_tasks"`
	JinjaExtensions  []string          `yaml:"_jinja_extensions"`
	SecretQuestions  []string          `yaml:"_secret_questions"`
	PreserveSymlinks bool              `yaml:"_preserve_symlinks"`
	MinCopierVersion string            `yaml:"_min_copier_version"`
	Envops           Envops            `yaml:"_envops"`
	ExternalData     map[string]string `yaml:"_external_data"`

	MessageBeforeCopy   string `yaml:"_message_before_copy"`
	MessageAfterCopy    string `yaml:"_message_after_copy"`
	MessageBeforeUpdate string `yaml:"_message_before_update"`
	MessageAfterUpdate  string `yaml:"_message_after_update"`

	Migrations []MigrationDef `yaml:"_migrations"`
}

TemplateConfig holds configuration from copier.yml underscore-prefixed keys.

type TemplateError

type TemplateError struct {
	Path string
	Err  error
}

TemplateError wraps errors related to template loading or configuration.

func (*TemplateError) Error

func (e *TemplateError) Error() string

func (*TemplateError) Unwrap

func (e *TemplateError) Unwrap() error

type TerminalPrompter

type TerminalPrompter struct{}

TerminalPrompter implements Prompter using charmbracelet/huh for terminal UI.

func NewTerminalPrompter

func NewTerminalPrompter() *TerminalPrompter

NewTerminalPrompter creates a new terminal prompter.

func (*TerminalPrompter) Ask

func (p *TerminalPrompter) Ask(q QuestionDef, currentAnswers map[string]any) (any, error)

Ask prompts the user for an answer to the given question.

func (*TerminalPrompter) Confirm

func (p *TerminalPrompter) Confirm(message string, defaultVal bool) (bool, error)

Confirm asks a yes/no question.

type ValidationError

type ValidationError struct {
	Question string
	Message  string
}

ValidationError is returned when a question answer fails validation.

func (*ValidationError) Error

func (e *ValidationError) Error() string

type VcsRef

type VcsRef string

VcsRef is a special enum for VCS reference handling.

const (
	// VcsRefCurrent tells copier to use the existing template ref from .copier-answers.yml.
	VcsRefCurrent VcsRef = ":current:"
)

Directories

Path Synopsis
cmd
copier command
Package main provides the copier CLI.
Package main provides the copier CLI.
internal
pathutil
Package pathutil provides path validation and manipulation helpers.
Package pathutil provides path validation and manipulation helpers.
textutil
Package textutil provides text manipulation helpers.
Package textutil provides text manipulation helpers.
version
Package version holds the build-time version string.
Package version holds the build-time version string.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL