Documentation
¶
Overview ¶
Package template is used to create files from template file systems
Index ¶
- type FileGenerator
- type FileGeneratorFunc
- type FileMode
- type Generator
- type Interface
- type Option
- type OutputContext
- func (c *OutputContext) Chmod(name string, mode fs.FileMode) error
- func (c *OutputContext) Chown(name string, uid, gid int) error
- func (c *OutputContext) Chtimes(name string, atime time.Time, mtime time.Time) error
- func (c *OutputContext) Create(name string) (fs.File, error)
- func (c *OutputContext) Do(ctx context.Context, gens ...Generator) error
- func (c *OutputContext) Exists(name string) bool
- func (c *OutputContext) File(name string) string
- func (c *OutputContext) Mkdir(name string, perm fs.FileMode) error
- func (c *OutputContext) MkdirAll(path string, perm fs.FileMode) error
- func (c *OutputContext) Open(name string) (fs.File, error)
- func (c *OutputContext) OpenContext(ctx context.Context, name string) (fs.File, error)
- func (c *OutputContext) OpenFile(name string, flag int, perm fs.FileMode) (fs.File, error)
- func (c *OutputContext) PopDir() error
- func (c *OutputContext) PushDir(name string) error
- func (c *OutputContext) Remove(name string) error
- func (c *OutputContext) RemoveAll(path string) error
- func (c *OutputContext) Rename(oldpath, newpath string) error
- func (c *OutputContext) SetData(name string, value any)
- func (c *OutputContext) Stat(name string) (fs.FileInfo, error)
- func (c *OutputContext) WorkDir() string
- type Root
- type Sequence
- type Vars
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 any) FileGenerator
Contents generates a file with the given contents. Contents is either a string, []byte, or io.Reader, which is copied to the output file of the given name. As a special case, if contents is some other type, it is JSON encoded.
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 Mode ¶
func Mode(mode fs.FileMode) 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 Interface, namedata ...any) FileGenerator
Template generates a file by executing a template.
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 Generator ¶
type Generator interface {
Generate(ctx context.Context, c *OutputContext) error
}
Generator is the interface for generating files.
func FS ¶ added in v0.15.0
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
type Interface ¶
Interface provides the interface of a template. The primary implementation is usually from the text/template package.
type Option ¶ added in v0.15.0
type Option func(*fsGenerator)
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
FS cli.FS
// contains filtered or unexported fields
}
func (*OutputContext) Exists ¶
func (c *OutputContext) Exists(name string) bool
func (*OutputContext) File ¶
func (c *OutputContext) File(name string) string
func (*OutputContext) MkdirAll ¶
func (c *OutputContext) MkdirAll(path string, perm fs.FileMode) error
func (*OutputContext) OpenContext ¶
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) WorkDir ¶
func (c *OutputContext) WorkDir() string
WorkDir is the path to the working directory
type Root ¶
Root is the root of a template, used to compose a sequence and configuration
func (*Root) DryRunFlag ¶
func (r *Root) DryRunFlag() cli.Prototype
DryRunFlag obtains a conventions-based flag for overwriting
func (*Root) OverwriteFlag ¶
func (r *Root) OverwriteFlag() cli.Prototype
OverwriteFlag obtains a conventions-based flag for overwriting