Documentation
¶
Index ¶
- Variables
- func ModifyMarshalledFunc[V any](unmarshal func([]byte, any) error, modify func(*V) ([]byte, error)) func(contents []byte, w io.Writer) error
- func Render(files ...File) string
- type Directory
- type File
- type Generator
- type MapFSOutputFS
- func (fsys *MapFSOutputFS) Mkdir(name string) error
- func (fsys *MapFSOutputFS) MkdirTemp(pattern string) (OutputFS, string, error)
- func (fsys *MapFSOutputFS) OpenFile(name string, flag int, perm fs.FileMode) (fs.File, error)
- func (fsys *MapFSOutputFS) Remove(name string) error
- func (fsys *MapFSOutputFS) RemoveAll(name string) error
- func (fsys *MapFSOutputFS) Rename(oldpath string, newpath string) error
- type Option
- type OutputFS
- type Template
- type WriterToModify
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrCleaningOutputDir = errors.New("error cleaning output dir")
Functions ¶
func ModifyMarshalledFunc ¶
func Render ¶
Render returns a textual representation of the file structure, primarily used for debugging.
Example ¶
files := Render(PlainFile("fileA", ""),
Dir("dirA", PlainFile("dirAFileA", "")),
Dir("dirB", PlainFile("dirBFileA", ""), PlainFile("dirBFileB", "")),
PlainFile("fileD", ""),
Dir("dirC", Dir("dirCdirA",
PlainFile("dirCdirAFileA", ""),
Dir("dirCdirADirA", Dir("dirCdirADirADirA",
PlainFile("dirCdirADirADirAFileA", ""),
PlainFile("dirCdirADirADirAFileB", ""),
)),
)))
fmt.Println(files)
Output: . ├── fileA ├── dirA/ │ └── dirAFileA ├── dirB/ │ ├── dirBFileA │ └── dirBFileB ├── fileD └── dirC/ └── dirCdirA/ ├── dirCdirAFileA └── dirCdirADirA/ └── dirCdirADirADirA/ ├── dirCdirADirADirAFileA └── dirCdirADirADirAFileB
Types ¶
type Directory ¶
A Directory can contain files and other directories.
type File ¶
type File interface {
Name() string
}
File is either a real file to be generated or a directory. Plain files must also implement io.Writer to write their content to the output file.
func ModifyFile ¶
ModifyFile can modify an existing file's contents. Generator.Generate will return an error, if the file doesn't exist yet.
func TemplatedFile ¶
TemplatedFile will execute the template with the given data and write the resuls to the output file.
type Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
func NewGenerator ¶
func (*Generator) Generate ¶
Example ¶
outpath, err := os.MkdirTemp("", "drydock.ExampleGenerator_Generate-*")
if err != nil {
panic(err)
}
defer os.RemoveAll(outpath)
outfs := NewOSOutputFS(outpath)
g := NewGenerator(outfs)
err = g.Generate(
context.Background(),
PlainFile("README.md", "# drydock"),
Dir("bin",
Dir("cli",
PlainFile("main.go", "package main"),
),
),
Dir("pkg",
PlainFile("README.md", "how to use this thing"),
Dir("cli",
PlainFile("cli.go", "package cli..."),
PlainFile("run.go", "package cli...run..."),
),
),
)
if err != nil {
panic(err)
}
entries, err := os.ReadDir(outpath)
if err != nil {
panic(err)
}
for _, e := range entries {
fmt.Println(e)
}
Output: - README.md d bin/ d pkg/
type MapFSOutputFS ¶
MapFSOutputFS extends testing/fstest.MapFS with OutputFS capabilities.
func (*MapFSOutputFS) Mkdir ¶
func (fsys *MapFSOutputFS) Mkdir(name string) error
func (*MapFSOutputFS) MkdirTemp ¶
func (fsys *MapFSOutputFS) MkdirTemp(pattern string) (OutputFS, string, error)
func (*MapFSOutputFS) Remove ¶
func (fsys *MapFSOutputFS) Remove(name string) error
func (*MapFSOutputFS) RemoveAll ¶
func (fsys *MapFSOutputFS) RemoveAll(name string) error
type Option ¶
type Option func(g *Generator)
func WithEmptyOutputDir ¶
func WithErrorOnExistingDir ¶
func WithErrorOnExistingFile ¶
type OutputFS ¶
type OutputFS interface {
fs.FS
OpenFile(name string, flag int, perm fs.FileMode) (fs.File, error)
// Mkdir creates a new directory and must return fs.ErrExist if the directory already exists.
Mkdir(name string) error
// Rename a given file or directory. Returned errors must be [os.LinkError] errors.
Rename(oldpath string, newpath string) error
// Remove the file at path or directory, if the directory is empty.
Remove(path string) error
// RemoveAll is like [OutputFS.Remove] but also removes any non-empty directories.
RemoveAll(path string) error
// MkdirTemp creates a temporary directory with the pattern, as described in [os.MkdirTemp].
// The caller is responsible for removing and temporary directories, they will not be cleaned up automatically.
MkdirTemp(pattern string) (OutputFS, string, error)
}
OutputFS extends the standard io/fs.FS interface with writing capabilities for hierarchical file systems.
func NewOSOutputFS ¶
NewOSOutputFS creates a new [OutputsFS] backed by the real filesystem, like os.DirFS.