common

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2024 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Permission bits: rwx------ .
	OwnerRWXPerms = 0o700
	// Permission bits: rw------- .
	OwnerRWPerms = 0o600
)
View Source
const (
	ABCInternalDir    = ".abc"
	ABCInternalStdout = "stdout"
)

ABCInternalDir is the name for internal directories that have things like manifests and .gitkeep files. These may be destination directories or golden-test data/ directories.

View Source
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

View Source
var DoNotModifyHeader = []byte("# Generated by the \"abc templates\" command. Do not modify.\n")

Functions

func CelCompileAndEval

func CelCompileAndEval(ctx context.Context, scope *Scope, expr model.String, outPtr any) error

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

func CopyRecursive(ctx context.Context, pos *model.ConfigPos, p *CopyParams) (outErr error)

CopyRecursive recursively copies folder contents with designated config params.

func IsReservedInDest added in v0.6.0

func IsReservedInDest(relPath string) bool

IsReservedInDest returns true if the given path cannot be created in the destination directory because that name is reserved for internal purposes.

The input path must use the local OS separators, since we process it with filepath. This path is relative to the destination directory.

func IsReservedStdout added in v0.6.0

func IsReservedStdout(relPath string) bool

IsReservedStdout returns true if the given path is the internal stdout path.

The input path must use the local OS separators, since we process it with filepath. This path is relative to the destination directory.

func IsStatNotExistErr added in v0.4.0

func IsStatNotExistErr(err error) bool

IsStatNotExistErr takes an error returned by os.Stat() and returns true if the error means "the path you stat'ed doesn't exist." It otherwise returns false.

func JoinIfRelative added in v0.7.0

func JoinIfRelative(cwd, path string) string

JoinIfRelative returns path if it's an absolute path, otherwise filepath.Join(cwd, path).

func Run added in v0.2.1

func Run(ctx context.Context, args ...string) (stdout, stderr string, _ error)

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 RunMany added in v0.4.0

func RunMany(ctx context.Context, args ...[]string) (stdouts, stderrs []string, _ error)

RunMany calls Run for each command in args. If any command returns error, then no further commands will be run, and that error will be returned. For any commands that were actually executed (not aborted by a previous error), their stdout and stderr will be returned. It's guaranteed that len(stdouts)==len(stderrs).

func SafeRelPath added in v0.2.1

func SafeRelPath(pos *model.ConfigPos, p string) (string, error)

SafeRelPath returns an error if the path contains a ".." traversal, and converts it to a relative path by removing any leading "/".

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. May be absolute or relative.
	DstRoot string
	// srcRoot is the file or directory from which to copy. May be absolute or
	// relative.
	SrcRoot string
	// FS is the filesytem to use.
	FS 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

	// If Hasher and OutHashes are not nil, then each copied file will be hashed
	// and the hex hash will be saved in OutHashes. If a file is "skipped"
	// (CopyHint.Skip==true) then the hash will not be computed. In dry run
	// mode, the hash will be computed normally. OutHashes always uses forward
	// slashes as path separator, regardless of OS.
	Hasher    func() hash.Hash
	OutHashes map[string][]byte
}

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

type CopyVisitor func(relPath string, de fs.DirEntry) (CopyHint, error)

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 ErrorFS added in v0.3.1

type ErrorFS struct {
	FS

	MkdirAllErr  error
	OpenErr      error
	OpenFileErr  error
	ReadFileErr  error
	RemoveAllErr error
	StatErr      error
	WriteFileErr error
}

A renderFS implementation that can inject errors for testing.

func (*ErrorFS) MkdirAll added in v0.3.1

func (e *ErrorFS) MkdirAll(name string, mode fs.FileMode) error

func (*ErrorFS) Open added in v0.3.1

func (e *ErrorFS) Open(name string) (fs.File, error)

func (*ErrorFS) OpenFile added in v0.3.1

func (e *ErrorFS) OpenFile(name string, flag int, mode os.FileMode) (*os.File, error)

func (*ErrorFS) ReadFile added in v0.3.1

func (e *ErrorFS) ReadFile(name string) ([]byte, error)

func (*ErrorFS) RemoveAll added in v0.3.1

func (e *ErrorFS) RemoveAll(name string) error

func (*ErrorFS) Stat added in v0.3.1

func (e *ErrorFS) Stat(name string) (fs.FileInfo, error)

func (*ErrorFS) WriteFile added in v0.3.1

func (e *ErrorFS) WriteFile(name string, data []byte, perm os.FileMode) error

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 RealFS added in v0.2.0

type RealFS struct{}

This is the non-test implementation of the filesystem interface.

func (*RealFS) MkdirAll added in v0.2.0

func (r *RealFS) MkdirAll(name string, perm os.FileMode) error

func (*RealFS) MkdirTemp added in v0.2.0

func (r *RealFS) MkdirTemp(dir, pattern string) (string, error)

func (*RealFS) Open added in v0.2.0

func (r *RealFS) Open(name string) (fs.File, error)

func (*RealFS) OpenFile added in v0.2.0

func (r *RealFS) OpenFile(name string, flag int, perm os.FileMode) (*os.File, error)

func (*RealFS) ReadFile added in v0.2.0

func (r *RealFS) ReadFile(name string) ([]byte, error)

func (*RealFS) RemoveAll added in v0.2.0

func (r *RealFS) RemoveAll(name string) error

func (*RealFS) Stat added in v0.2.0

func (r *RealFS) Stat(name string) (fs.FileInfo, error)

func (*RealFS) WriteFile added in v0.2.0

func (r *RealFS) WriteFile(name string, data []byte, perm os.FileMode) error

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 NewScope

func NewScope(vars map[string]string, goTmplFuncs map[string]any) *Scope

func (*Scope) AllVars added in v0.7.0

func (s *Scope) AllVars() map[string]string

AllVars 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.

func (*Scope) GoTmplFuncs added in v0.7.0

func (s *Scope) GoTmplFuncs() map[string]any

GoTmplFuncs returns all the Go-template functions that are in scope. The result is suitable for passing to text/template.Template.Funcs().

func (*Scope) Lookup

func (s *Scope) Lookup(name string) (string, bool)

Lookup returns the current value of a given variable name, or false.

func (*Scope) With

func (s *Scope) With(m map[string]string) *Scope

With returns a new scope containing a new set of variable values. It forwards lookups to the previously existing scope if the lookup key is not found in m.

Directories

Path Synopsis
Package builtinvar deals with so-called "built in vars".
Package builtinvar deals with so-called "built in vars".
Package errs contains errors that are shared across packages.
Package errs contains errors that are shared across packages.
Package flags contains flags that are commonly used by several commands.
Package flags contains flags that are commonly used by several commands.
Package rules contains function for handling rule evaluation
Package rules contains function for handling rule evaluation
Package spec contains commonly used function for handling spec files
Package spec contains commonly used function for handling spec files
Package render implements the template rendering related subcommands.
Package render implements the template rendering related subcommands.
Package upgrade implements template upgrading: taking a directory containing a rendered template and updating it with the latest version of the template.
Package upgrade implements template upgrading: taking a directory containing a rendered template and updating it with the latest version of the template.

Jump to

Keyboard shortcuts

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