common

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2023 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

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

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

func LoadDirWithoutMode(t *testing.T, dir string) map[string]string

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

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 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 "/".

func SortStrings

func SortStrings(in []string) []string

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

func ToHyphenCase(v string) string

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

func ToLowerHyphenCase(v string) string

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

func ToLowerSnakeCase(v string) string

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

func ToSnakeCase(v string) string

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

func ToUpperHyphenCase(v string) string

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

func ToUpperSnakeCase(v string) string

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.

func WriteAll

func WriteAll(t *testing.T, root string, files map[string]ModeAndContents)

WriteAll saves the given file contents with the given permissions.

func WriteAllDefaultMode

func WriteAllDefaultMode(t *testing.T, root string, files map[string]string)

WriteAllDefaultMode wraps writeAll and sets file permissions to 0600.

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

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 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 ModeAndContents struct {
	Mode     os.FileMode
	Contents string
}

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(m map[string]string) *Scope

func (*Scope) All

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

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.

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

Jump to

Keyboard shortcuts

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