Documentation ¶
Index ¶
- Constants
- Variables
- func CelCompileAndEval(ctx context.Context, scope *Scope, expr model.String, outPtr any) error
- func CopyRecursive(ctx context.Context, pos *model.ConfigPos, p *CopyParams) (outErr error)
- func LoadDirContents(t *testing.T, dir string) map[string]ModeAndContents
- func LoadDirWithoutMode(t *testing.T, dir string) map[string]string
- func Run(ctx context.Context, args ...string) (stdout, stderr string, _ error)
- func SafeRelPath(pos *model.ConfigPos, p string) (string, error)
- func SortStrings(in []string) []string
- func ToHyphenCase(v string) string
- func ToLowerHyphenCase(v string) string
- func ToLowerSnakeCase(v string) string
- func ToSnakeCase(v string) string
- func ToUpperHyphenCase(v string) string
- func ToUpperSnakeCase(v string) string
- func WriteAll(t *testing.T, root string, files map[string]ModeAndContents)
- func WriteAllDefaultMode(t *testing.T, root string, files map[string]string)
- type CopyHint
- type CopyParams
- type CopyVisitor
- type FS
- type ModeAndContents
- type RealFS
- func (r *RealFS) MkdirAll(name string, perm os.FileMode) error
- func (r *RealFS) MkdirTemp(dir, pattern string) (string, error)
- func (r *RealFS) Open(name string) (fs.File, error)
- func (r *RealFS) OpenFile(name string, flag int, perm os.FileMode) (*os.File, error)
- func (r *RealFS) ReadFile(name string) ([]byte, error)
- func (r *RealFS) RemoveAll(name string) error
- func (r *RealFS) Stat(name string) (fs.FileInfo, error)
- func (r *RealFS) WriteFile(name string, data []byte, perm os.FileMode) error
- type Scope
Constants ¶
const DefaultRunTimeout = time.Minute
DefaultRunTimeout is how long we'll wait for commands to run in the case where the context doesn't already have a timeout. This was chosen arbitrarily.
Variables ¶
var CmpFileMode = cmp.Comparer(func(a, b fs.FileMode) bool { if runtime.GOOS == "windows" { return a.Perm().String()[1:3] == b.Perm().String()[1:3] } return a == b })
CmpFileMode is a cmp option that handles the conflict between Unix and Windows systems file permissions.
Functions ¶
func CelCompileAndEval ¶
celCompileAndEval parses, compiles, and executes the given CEL expr with the given variables in scope.
The output of CEL execution is written into the location pointed to by outPtr. It must be a pointer. If the output of the CEL expression can't be converted to the given type, then an error will be returned. For example, if the CEL expression is "hello" and outPtr points to an int, an error will returned because CEL cannot treat "hello" as an integer.
func CopyRecursive ¶ added in v0.2.0
CopyRecursive recursively copy folder contents with designated config params.
func LoadDirContents ¶ added in v0.2.0
func LoadDirContents(t *testing.T, dir string) map[string]ModeAndContents
LoadDirContents reads all the files recursively under "dir", returning their contents as a map[filename]->contents. Returns nil if dir doesn't exist. Keys use slash separators, not native.
func LoadDirWithoutMode ¶ added in v0.2.0
Read all the files recursively under "dir", returning their contents as a map[filename]->contents but without file mode. Returns nil if dir doesn't exist. Keys use slash separators, not native.
func Run ¶ added in v0.2.1
Run is a wrapper around exec.CommandContext and Run() that captures stdout and stderr as strings. The input args must have len>=1.
This is intended to be used for commands that run non-interactively then exit.
This doesn't execute a shell (unless of course args[0] is the name of a shell binary).
If the incoming context doesn't already have a timeout, then a default timeout will be added (see DefaultRunTimeout).
If the command fails, the error message will include the contents of stdout and stderr. This saves boilerplate in the caller.
func SafeRelPath ¶ added in v0.2.1
SafeRelPath returns an error if the path contains a ".." traversal, and converts it to a relative path by removing any leading "/".
func SortStrings ¶
sortStrings sorts the given list of strings. Go's built-in sorting behavior modifies the string in place. It would be very weird if rendering a template changed the order of an input further down the stack.
func ToHyphenCase ¶
toHyphenCase converts a string to hyphen-case by removing all characters (except alphanumeric, hyphens, underscores and spaces) and replacing any hyphens or spaces with underscores.
func ToLowerHyphenCase ¶
toLowerHyphenCase converts a string to hyphen-case by removing all characters (except alphanumeric, hyphens, underscores and spaces) and replacing any underscores or spaces with hyphens. The result is then returned as a lower case string.
func ToLowerSnakeCase ¶
toLowerSnakeCase converts a string to snake_case by removing all characters (except alphanumeric, hyphens, underscores and spaces) and replacing any hyphens or spaces with underscores. The result is then returned as a lower case string.
func ToSnakeCase ¶
toSnakeCase converts a string to snake_case by removing all characters (except alphanumeric, hyphens, underscores and spaces) and replacing any hyphens or spaces with underscores.
func ToUpperHyphenCase ¶
toUpperSnakeCase converts a string to hyphen-case by removing all characters (except alphanumeric, hyphens, underscores and spaces) and replacing any underscores or spaces with hyphens. The result is then returned as a upper case string.
func ToUpperSnakeCase ¶
toUpperSnakeCase converts a string to snake_case by removing all characters (except alphanumeric, hyphens, underscores and spaces) and replacing any hyphens or spaces with underscores. The result is then returned as a upper case string.
Types ¶
type CopyHint ¶ added in v0.2.0
type CopyHint struct { // Before overwriting a file in the destination dir, copy the preexisting // contents of the file into ~/.abc/$timestamp. Only used if // overwrite==true. // // This has no effect on directories, only files. BackupIfExists bool // Overwrite files in the destination if they already exist. The default is // to conservatively fail. // // This has no effect on directories, only files. Overwrite bool // Whether to skip this file or directory (don't write it to the // destination). For directories, this will cause all files underneath the // directory to be skipped. Skip bool }
type CopyParams ¶ added in v0.2.0
type CopyParams struct { // backupDirMaker will be called when we reach the first file that actually // needs to be backed up. It should create a directory and return its path, // either relative to the cwd or absolute. Use os.MkdirTemp() in real code // and something hardcoded in tests. BackupDirMaker func(FS) (string, error) // // backupDir provides the path at which files will be saved before they're // // overwritten. // backupDir string // dryRun skips actually copy anything, just checks whether the copy would // be likely to succeed. DryRun bool // dstRoot is the output directory. DstRoot string // srcRoot is the file or directory from which to copy. SrcRoot string // RFS is the filesytem to use. RFS FS // visitor is an optional function that will be called for each file in the // source, to allow customization of the copy operation on a per-file basis. Visitor CopyVisitor }
CopyParams contains most of the parameters to CopyRecursive(). There were too many of these, so they've been factored out into a struct to avoid having the function parameter list be really long.
type CopyVisitor ¶ added in v0.2.0
CopyVisitor is the type for callback functions that are called by CopyRecursive for each file and directory encountered. It gives the caller an opportunity to influence the behavior of the copy operation on a per-file basis, and also informs the of each file and directory being copied.
type FS ¶ added in v0.2.0
type FS interface { fs.StatFS // These methods correspond to methods in the "os" package of the same name. MkdirAll(string, os.FileMode) error MkdirTemp(string, string) (string, error) OpenFile(string, int, os.FileMode) (*os.File, error) ReadFile(string) ([]byte, error) RemoveAll(string) error WriteFile(string, []byte, os.FileMode) error }
Abstracts filesystem operations.
We can't use os.DirFS or fs.StatFS because they lack some methods we need. So we created our own interface.
type ModeAndContents ¶
type RealFS ¶ added in v0.2.0
type RealFS struct{}
This is the non-test implementation of the filesystem interface.
type Scope ¶
type Scope struct {
// contains filtered or unexported fields
}
scope binds variable names to values. It has a stack-like structure that allows inner scopes to inherit values from outer scopes. Variable names are looked up in order of innermost-to-outermost.
For example, a for_each action defines a key value that is assigned to each of a list of values. The new variable introduced in this way "shadows" any variable that may previously exist of the same name. When the for_each loop is finished, then the outer scope's variable becomes available again.
func (*Scope) All ¶
All returns all variable bindings that are in scope. Inner/top-of-stack bindings take priority over outer bindings of the same name.
The returned map is a copy that is owned by the caller; it can be changed safely.
The return value is never nil.