template

package
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2026 License: BSD-3-Clause Imports: 20 Imported by: 1

Documentation

Overview

Package template is used to create files from template file systems

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FileGenerator

type FileGenerator interface {
	GenerateFile(ctx context.Context, c *OutputContext, name string) error
}

func Contents

func Contents(contents []byte) FileGenerator

Contents generates a file with the given contents, which are copied to the output file of the given name.

func ContentsFrom added in v0.18.0

func ContentsFrom(r io.Reader) FileGenerator

ContentsFrom generates a file whose contents are read from the given reader.

func ContentsJSON added in v0.18.0

func ContentsJSON(v any) FileGenerator

ContentsJSON generates a file whose contents are the JSON encoding of the given value.

func ContentsMarshal added in v0.18.0

func ContentsMarshal(v any, m MarshalWriter) FileGenerator

ContentsMarshal generates a file whose contents are the encoding of the given value produced by the MarshalWriter.

func ContentsString added in v0.18.0

func ContentsString(contents string) FileGenerator

ContentsString generates a file whose contents are the given string.

func Exec added in v0.15.0

func Exec(name string, arg ...string) FileGenerator

Exec generates a file by piping the existing file contents through a subprocess. The subprocess receives the current file contents on stdin, and its stdout becomes the new file contents.

func Gofmt

func Gofmt() FileGenerator

func OriginalContents added in v0.15.0

func OriginalContents() FileGenerator

OriginalContents is a FileGenerator sentinel. When used inside WithFileGenerator, it is replaced with the file's contents from the source file system, allowing other generators in the chain to operate on them. For example:

FS(fsys, WithFileGenerator("*.go", OriginalContents(), Gofmt()))

func Template

func Template(tt *tt.Template, namedata ...any) FileGenerator

Template generates a file by executing a template.

func Touch

func Touch() FileGenerator

Touch touches the file.

type FileGeneratorFunc

type FileGeneratorFunc func(context.Context, *OutputContext, string) error

func (FileGeneratorFunc) GenerateFile

func (f FileGeneratorFunc) GenerateFile(ctx context.Context, c *OutputContext, name string) error

type FileMode

type FileMode fs.FileMode

FileMode provides a step that applies the file mode to the file or directory

const (
	Executable FileMode = 0755
	ReadOnly   FileMode = 0600
)

File mode bits

func (FileMode) Generate added in v0.21.0

func (m FileMode) Generate(_ context.Context, c *OutputContext) error

func (FileMode) GenerateFile

func (m FileMode) GenerateFile(_ context.Context, c *OutputContext, name string) error

type Generator

type Generator interface {
	Generate(ctx context.Context, c *OutputContext) error
}

Generator is the interface for generating files.

func Data

func Data(namevalue ...any) Generator

func Dir

func Dir(name string, contents ...Generator) Generator

func FS added in v0.15.0

func FS(fsys fs.FS, opts ...Option) Generator

FS creates a generator that walks the given file system and reproduces its files and directories in the output. By default each file is copied as-is. Options may override this behavior for files whose names match a glob pattern.

func File

func File(name string, ops ...FileGenerator) Generator

File generates a file with the given operations

func GoGet

func GoGet(pkgs ...string) Generator

GoGet assumes that the current template is a Go module and adds the given go module via go get. An error results if the project is not a go module.

type GeneratorFunc added in v0.21.0

type GeneratorFunc func(_ context.Context, c *OutputContext) error

GeneratorFunc provides a function that can be used as a generator

func (GeneratorFunc) Generate added in v0.21.0

func (f GeneratorFunc) Generate(ctx context.Context, c *OutputContext) error

type MarshalWriter added in v0.18.0

type MarshalWriter interface {
	MarshalWrite(w io.Writer, in any) error
}

MarshalWriter is used by ContentsMarshal to encode a value into file contents.

type Option added in v0.15.0

type Option interface {
	// contains filtered or unexported methods
}

Option configures the behavior of the FS generator.

func WithFileGenerator added in v0.15.0

func WithFileGenerator(filename string, gen ...FileGenerator) Option

WithFileGenerator overrides the default copy behavior for files whose name matches the given glob pattern. Any OriginalContents generator in gen is replaced with the actual file contents from the source file system.

type OutputContext

type OutputContext struct {
	Vars      map[string]any
	Overwrite bool
	DryRun    bool
	MakeDirs  bool
	FS        cli.FS
	// contains filtered or unexported fields
}

func (*OutputContext) Chmod

func (c *OutputContext) Chmod(name string, mode fs.FileMode) error

func (*OutputContext) Chown

func (c *OutputContext) Chown(name string, uid, gid int) error

func (*OutputContext) Chtimes

func (c *OutputContext) Chtimes(name string, atime time.Time, mtime time.Time) error

func (*OutputContext) Create

func (c *OutputContext) Create(name string) (fs.File, error)

func (*OutputContext) Do

func (c *OutputContext) Do(ctx context.Context, gens ...Generator) error

func (*OutputContext) Exists

func (c *OutputContext) Exists(name string) bool

func (*OutputContext) File

func (c *OutputContext) File(name string) string

func (*OutputContext) Mkdir

func (c *OutputContext) Mkdir(name string, perm fs.FileMode) error

func (*OutputContext) MkdirAll

func (c *OutputContext) MkdirAll(path string, perm fs.FileMode) error

func (*OutputContext) Open

func (c *OutputContext) Open(name string) (fs.File, error)

func (*OutputContext) OpenContext

func (c *OutputContext) OpenContext(ctx context.Context, name string) (fs.File, error)

func (*OutputContext) OpenFile

func (c *OutputContext) OpenFile(name string, flag int, perm fs.FileMode) (fs.File, error)

func (*OutputContext) PopDir

func (c *OutputContext) PopDir() error

func (*OutputContext) PushDir

func (c *OutputContext) PushDir(name string) error

func (*OutputContext) Remove

func (c *OutputContext) Remove(name string) error

func (*OutputContext) RemoveAll

func (c *OutputContext) RemoveAll(path string) error

func (*OutputContext) Rename

func (c *OutputContext) Rename(oldpath, newpath string) error

func (*OutputContext) SetData

func (c *OutputContext) SetData(name string, value any)

func (*OutputContext) Stat

func (c *OutputContext) Stat(name string) (fs.FileInfo, error)

func (*OutputContext) WorkDir

func (c *OutputContext) WorkDir() string

WorkDir is the path to the working directory

type Root

type Root struct {

	// Action provides the action for when the Root value is added to
	// a pipeline.
	cli.Action

	Generator Generator
	Overwrite bool
	DryRun    bool
	Path      string

	// MakeDirs controls whether to implicitly create directories.
	// It is set to true by default by [New]. When set to false, the
	// [Dir] generator becomes a container only and does not create directories.
	// To ensure a directory exists, add a FileMode, usually FileMode(0755),
	// as the first generator argument.
	MakeDirs bool
}

Root is the root of a template, used to compose a sequence and configuration. You typically create an instance using the New function, which in addition to specifying the generator you want, sets up the default action for the template when it runs in the pipeline, which is to register the flags and run Generate at the action timing.

func New

func New(items ...Generator) *Root

New creates a new template using the given sequence of generators. Directories will automatically be created by default because MakeDirs will also be set to true. The Action will be set to the default action, which registers the flags if used as an initializer and invokes the generator in the action timing.

func (*Root) DryRunFlag

func (r *Root) DryRunFlag() cli.Prototype

DryRunFlag obtains a conventions-based flag for overwriting

func (*Root) Generate

func (r *Root) Generate(ctx context.Context) error

Generate provides the behavior of generating the output from the template

func (*Root) OverwriteFlag

func (r *Root) OverwriteFlag() cli.Prototype

OverwriteFlag obtains a conventions-based flag for overwriting

func (*Root) Pipeline added in v0.14.0

func (r *Root) Pipeline() cli.Action

Pipeline converts the root into a pipeline

type Sequence

type Sequence []Generator

Sequence is a sequence of template generators

func (Sequence) Generate

func (s Sequence) Generate(ctx context.Context, c *OutputContext) error

type Vars

type Vars map[string]any

Vars contains template variables. Variables are copied into the template context

func (Vars) Generate

func (v Vars) Generate(_ context.Context, c *OutputContext) error

Jump to

Keyboard shortcuts

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