utils

package
v0.7.9 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2025 License: GPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const EIRCTL_ENV_FILE string = "eirctl.env"

EIRCTL_ENV_FILE is the default location of env file ingested by eirctl for every run.

View Source
const PipelineDirectionChar string = "->"
View Source
const REPLACE_CHAR_DEFAULT = " "

Variables

View Source
var (
	ErrInvalidOptionsEnvFile  = errors.New("invalid options on envfile")
	ErrEnvfileFormatIncorrect = errors.New("envfile incorrect")
)

Functions

func CascadeName

func CascadeName(parents []string, current string) string

CascadeName builds the name using the ancestors with a pipeline separator

func ConvertEnv

func ConvertEnv(env map[string]string) []string

ConvertEnv converts map representing the environment to array of strings in the form "key=value"

func ConvertFromEnv

func ConvertFromEnv(env []string) map[string]string

ConvertFromEnv takes a string array and coverts it to a map of strings since an env variable can only really be a string it's safe to convert to string and not interface downstream programs need to cast values to what they expect

func ConvertToMachineFriendly

func ConvertToMachineFriendly(str string) string

ConvertToMachineFriendly converts a string containing characters that would not play nice key names e.g. `->` or `|` `/` `\\` `:` To make it easier to decipher the names we replace these characters with a map to a known sequence.

replaceSequence := []string{
		"->", "__a__",
		`|`, "__b__",
		`/`, "__c__",
		`\`, "__d__",
		`:`, "__e__",
		` `, "__f__", // space sequence
}

func ConvertToMapOfStrings

func ConvertToMapOfStrings(m map[string]interface{}) map[string]string

ConvertToMapOfStrings converts map of interfaces to map of strings

func DecodeBase62

func DecodeBase62(str string) string

DecodeBase62 takes a EncodeBase62 generated string and and converts it back to its original human friendly form

func DefaultTaskctlEnv

func DefaultTaskctlEnv() *variables.Variables

DefaultTaskctlEnv checks if there is a file in the current directory `eirctl.env` if we ingest it into the Env variable giving preference to the `eirctl.env` specified K/V.

Or should this be done once on start up?

func EncodeBase62

func EncodeBase62(str string) string

EncodeBase62 takes a string and converts it to base62 format - this is safer than using regex or strings replace.

func FileExists

func FileExists(file string) bool

FileExists checks if the file exists

func GetFullPath

func GetFullPath(path string) string

GetFullPath

func IsExitError

func IsExitError(err error) bool

IsExitError checks if given error is an instance of exec.ExitError

func IsURL

func IsURL(s string) bool

IsURL checks if given string is a valid URL

func LastLine

func LastLine(r io.Reader) (l string)

LastLine returns last line from provided reader

func MapKeys

func MapKeys(m interface{}) (keys []string)

MapKeys returns an array of map's keys

func MustGetUserHomeDir

func MustGetUserHomeDir() string

MustGetUserHomeDir returns current user's home directory. Panics is os.UserHomeDir() returns error

func MustGetwd

func MustGetwd() string

MustGetwd returns current working directory. Panics is os.Getwd() returns error

func NormalizeHome added in v0.7.1

func NormalizeHome(v string) string

func ReadEnvFile

func ReadEnvFile(r io.ReadCloser) (map[string]string, error)

ReadEnvFile reads env file inv `k=v` format

func ReaderFromPath

func ReaderFromPath(envfile *Envfile) (io.ReadCloser, bool)

ReaderFromPath returns an io.ReaderCloser from provided path Returning false if the file does not exist or is unable to read it

func RenderString

func RenderString(tmpl string, variables map[string]interface{}) (string, error)

RenderString parses given string as a template and executes it with provided params

func TailExtract

func TailExtract(v string) string

TailExtract takes the last possible node from a pipeline string

Types

type Binary

type Binary struct {
	// Bin is the name of the executable to run
	// it must exist on the path
	// If using a default mvdn.sh context then
	// ensure it is on your path as symlink if you are only using aliases.
	Bin  string   `mapstructure:"bin" yaml:"bin" json:"bin"`
	Args []string `mapstructure:"args" yaml:"args,omitempty" json:"args,omitempty"`
}

Binary is a structure for storing binary file path and arguments that should be passed on binary's invocation

func (*Binary) GetArgs

func (b *Binary) GetArgs() []string

func (*Binary) WithBaseArgs

func (b *Binary) WithBaseArgs(args []string) *Binary

func (*Binary) WithContainerArgs

func (b *Binary) WithContainerArgs(args []string) *Binary

func (*Binary) WithShellArgs

func (b *Binary) WithShellArgs(args []string) *Binary

type Container

type Container struct {
	// Name is the name of the container
	//
	// can be specified in the following formats
	//
	// - <image-name> (Same as using <image-name> with the latest tag)
	//
	// - <image-name>:<tag>
	//
	// - <image-name>@<digest>
	//
	// If the known runtime is podman it should include the registry domain
	// e.g. `docker.io/alpine:latest`
	Name string `mapstructure:"name" yaml:"name" json:"name"`
	// Entrypoint Overwrites the default ENTRYPOINT of the image
	Entrypoint schema.StringSlice `mapstructure:"entrypoint" yaml:"entrypoint,omitempty" json:"entrypoint,omitempty" jsonschema:"oneof_type=string;array"`
	// EnableDinD mounts the docker sock...
	//
	// > highly discouraged
	EnableDinD bool `mapstructure:"enable_dind" yaml:"enable_dind,omitempty" json:"enable_dind,omitempty"`
	// EnableBindMount signifies whether to use the --volume or --mount specification.
	// Default false.
	// will use --volume as a default
	EnableBindMount bool `mapstructure:"enable_mount" yaml:"enable_mount,omitempty" json:"enable_mount,omitempty"`
	// ContainerArgs are additional args used for the container supplied by the user
	//
	// e.g. dcoker run (EIRCTL_ARGS...) (CONTAINER_ARGS...) image (command)
	// The internals will strip out any unwanted/forbidden args
	//
	// Args like the switch --privileged and the --volume|-v flag with the value of /var/run/docker.sock:/var/run/docker.sock
	// will be removed.
	//
	// Currently the only the -v|--volume and -u|--user -p|--port args get parsed.
	ContainerArgs []string `mapstructure:"container_args" yaml:"container_args,omitempty" json:"container_args,omitempty"`
	// Shell will be used to run the command in a specific shell on the container
	//
	// Must exist in the container
	Shell string `mapstructure:"shell" yaml:"shell,omitempty" json:"shell,omitempty"`
	// Args are additional args to pass to the shell if provided.
	// Once you provide the ShellArgs, you must also specify the Shell as well, as there is no reliable way to ensure the default `sh` accepts provided shell arguments
	//
	// Default Shell and ShellArgs are `sh -c`
	//
	// e.g. docker run (EIRCTL_ARGS...) (CONTAINER_ARGS...) image (shell) (SHELL_ARGS...) (command)
	//
	// Example: with powershell could be: `-Command -NonInteractive` along with a custom shell of `pwsh` would result in `pwsh -Command -NonInteractive (command)`
	ShellArgs []string `mapstructure:"shell_args" yaml:"shell_args,omitempty" json:"shell_args,omitempty"`
}

Container is the specific context for containers only available to docker API compliant implementations

e.g. docker and podman

The aim is to remove some of the boilerplate away from the existing more generic context and introduce a specific context for tasks run in containers.

type EnvFileOpts

type EnvFileOpts func(*Envfile)

Opts is a task runner configuration function.

type Envfile

type Envfile struct {
	// list of variables to be excluded
	// from the injection into container runtimes
	//
	// Currently this is based on a prefix
	//
	// Example:
	// HOME=foo,HOMELAB=bar
	//
	// Both of these will be skipped
	Exclude []string `mapstructure:"exclude" yaml:"exclude,omitempty" json:"exclude,omitempty"`
	Include []string `mapstructure:"include" yaml:"include,omitempty" json:"include,omitempty"`
	// PathValue points to the file to read in and compute using the modify/include/exclude instructions.
	PathValue   string `mapstructure:"path" yaml:"path,omitempty" json:"path,omitempty"`
	ReplaceChar string `mapstructure:"replace_char" yaml:"replace_char,omitempty" json:"replace_char,omitempty"`
	Quote       bool   `mapstructure:"quote" yaml:"quote,omitempty" json:"quote,omitempty"`
	// Modify specifies the modifications to make to each env var and whether it meets the criteria
	// example:
	// - pattern: "^(?P<keyword>TF_VAR_)(?P<varname>.*)"
	// 	 operation: lower
	// the inputs are validated at task/pipeline build time and will fail if the
	// <keyword> and <varname> sub expressions are not present in the `pattern`
	Modify []ModifyEnv `mapstructure:"modify" yaml:"modify,omitempty" json:"modify,omitempty"`
	// defaults to .eirctl in the current directory
	// again this should be hidden from the user...
	GeneratedDir string `mapstructure:"generated_dir" yaml:"generated_dir,omitempty" json:"generated_dir,omitempty"`
	// contains filtered or unexported fields
}

Envile is a structure for storing the information required to generate an envfile which can be consumed by the specified binary

func NewEnvFile

func NewEnvFile(opts ...EnvFileOpts) *Envfile

NewEnvFile creates a new instance of the EnvFile initializes it with some defaults

func (*Envfile) Path

func (e *Envfile) Path() string

func (*Envfile) Validate

func (e *Envfile) Validate() error

Validate checks input is correct

This will be added to later

func (*Envfile) WithPath

func (e *Envfile) WithPath(path string) *Envfile

type ModifyEnv

type ModifyEnv struct {
	Pattern   string `mapstructure:"pattern" yaml:"pattern" json:"pattern"`
	Operation string `mapstructure:"operation" yaml:"operation" json:"operation" jsonschema:"enum=upper,enum=lower"`
}

func (ModifyEnv) IsValid

func (me ModifyEnv) IsValid() bool

Jump to

Keyboard shortcuts

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