environment

package
v1.73.1 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package environment defines object describing the current environment.

Index

Constants

This section is empty.

Variables

View Source
var (

	// IsEnvironmentVariableKey defines a validation rule for environment variable keys ([IEEE Std 1003.1-2008 / IEEE POSIX P1003.2/ISO 9945.2](http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_10_02)) for use with github.com/go-ozzo/ozzo-validation
	// TODO use the built-in implementation in `is` package when https://github.com/go-ozzo/ozzo-validation/issues/186 is looked at.
	IsEnvironmentVariableKey = validation.NewStringRuleWithError(isEnvVarKey, errEnvvarInvalid)
)

Functions

func SortEnvironmentVariables added in v1.53.0

func SortEnvironmentVariables(envvars []IEnvironmentVariable)

SortEnvironmentVariables sorts a list of environment variable alphabetically no matter the case.

func ValidateEnvironmentVariables

func ValidateEnvironmentVariables(vars ...IEnvironmentVariable) error

ValidateEnvironmentVariables validates that environment variables are correctly defined in regard to their schema.

Types

type EnvVar

type EnvVar struct {
	// contains filtered or unexported fields
}

func (*EnvVar) Equal

func (e *EnvVar) Equal(v IEnvironmentVariable) bool

func (*EnvVar) GetKey

func (e *EnvVar) GetKey() string

func (*EnvVar) GetValue

func (e *EnvVar) GetValue() string

func (*EnvVar) MarshalText

func (e *EnvVar) MarshalText() (text []byte, err error)

func (*EnvVar) String

func (e *EnvVar) String() string

func (*EnvVar) UnmarshalText

func (e *EnvVar) UnmarshalText(text []byte) error

func (*EnvVar) Validate

func (e *EnvVar) Validate() (err error)

type IEnvironment

type IEnvironment interface {
	// GetCurrentUser returns the environment current user.
	GetCurrentUser() *user.User
	// GetEnvironmentVariables returns the variables defining the environment  (and optionally those supplied in `dotEnvFiles`)
	// `dotEnvFiles` corresponds to `.env` files present on the machine and follows the mechanism described by https://github.com/bkeepers/dotenv
	GetEnvironmentVariables(dotEnvFiles ...string) []IEnvironmentVariable
	// GetExpandedEnvironmentVariables  is similar to GetEnvironmentVariables but returns variables with fully expanded values.
	// e.g. on Linux, if variable1=${variable2}, then the reported value of variable1 will be the value of variable2
	GetExpandedEnvironmentVariables(dotEnvFiles ...string) []IEnvironmentVariable
	// GetFilesystem returns the filesystem associated with the current environment
	GetFilesystem() filesystem.FS
	// GetEnvironmentVariable returns the environment variable corresponding to `envvar` or an error if it not set. optionally it searches `dotEnvFiles` files too
	// `dotEnvFiles` corresponds to `.env` files present on the machine and follows the mechanism described by https://github.com/bkeepers/dotenv
	GetEnvironmentVariable(envvar string, dotEnvFiles ...string) (IEnvironmentVariable, error)
	// GetExpandedEnvironmentVariable is similar to GetEnvironmentVariable but returns variables with fully expanded values.
	//	// e.g. on Linux, if variable1=${variable2}, then the reported value of variable1 will be the value of variable2
	GetExpandedEnvironmentVariable(envvar string, dotEnvFiles ...string) (IEnvironmentVariable, error)
}

IEnvironment defines an environment for an application to run on.

func NewCurrentEnvironment

func NewCurrentEnvironment() IEnvironment

NewCurrentEnvironment returns system current environment.

type IEnvironmentVariable

type IEnvironmentVariable interface {
	encoding.TextMarshaler
	encoding.TextUnmarshaler
	fmt.Stringer
	// GetKey returns the variable key.
	GetKey() string
	// GetValue returns the variable value.
	GetValue() string
	// Validate checks whether the variable value is correctly defined
	Validate() error
	// Equal states whether two environment variables are equal or not.
	Equal(v IEnvironmentVariable) bool
}

IEnvironmentVariable defines an environment variable to be set for the commands to run.

func CloneEnvironmentVariable added in v1.53.0

func CloneEnvironmentVariable(envVar IEnvironmentVariable) IEnvironmentVariable

CloneEnvironmentVariable returns a clone of the environment variable.

func ExpandEnvironmentVariable added in v1.52.0

func ExpandEnvironmentVariable(recursive bool, envVarToExpand IEnvironmentVariable, envvars ...IEnvironmentVariable) (expandedEnvVar IEnvironmentVariable)

ExpandEnvironmentVariable returns a clone of envVarToExpand but with an expanded value based on environment variables defined in envvars list. Expansion assumes that all the variables are present in the envvars list. If recursive is set to true, then expansion is performed recursively over the variable list.

func ExpandEnvironmentVariables added in v1.52.0

func ExpandEnvironmentVariables(recursive bool, envvars ...IEnvironmentVariable) (expandedEnvVars []IEnvironmentVariable)

ExpandEnvironmentVariables returns a list of environment variables with their value being expanded. Expansion assumes that all the variables are present in the envvars list. If recursive is set to true, then expansion is performed recursively over the variable list.

func FindEnvironmentVariable added in v1.49.0

func FindEnvironmentVariable(envvar string, envvars ...IEnvironmentVariable) (IEnvironmentVariable, error)

FindEnvironmentVariable looks for an environment variable in a list. if no environment variable matches, an error is returned

func FindFoldEnvironmentVariable added in v1.53.0

func FindFoldEnvironmentVariable(envvar string, envvars ...IEnvironmentVariable) (IEnvironmentVariable, error)

FindFoldEnvironmentVariable looks for an environment variable in a list similarly to FindEnvironmentVariable but without case-sensitivity.

func MergeEnvironmentVariableSets added in v1.53.0

func MergeEnvironmentVariableSets(caseSensitive bool, envvarSet1 []IEnvironmentVariable, envvarSet2 ...IEnvironmentVariable) (mergedEnvVars []IEnvironmentVariable)

MergeEnvironmentVariableSets merges two sets of environment variables. If both sets have a same environment variable, its value in set 1 will take precedence. caseSensitive states whether two similar keys with different case should be considered as different

func NewEnvironmentVariable

func NewEnvironmentVariable(key, value string) IEnvironmentVariable

NewEnvironmentVariable returns an environment variable defined by a key and a value.

func NewEnvironmentVariableWithValidation

func NewEnvironmentVariableWithValidation(key, value string, rules ...validation.Rule) IEnvironmentVariable

NewEnvironmentVariableWithValidation returns an environment variable defined by a key and a value but with the possibility to define value validation rules.

func ParseEnvironmentVariable

func ParseEnvironmentVariable(variable string) (IEnvironmentVariable, error)

ParseEnvironmentVariable parses an environment variable definition, in the form "key=value".

func ParseEnvironmentVariables added in v1.49.0

func ParseEnvironmentVariables(variables ...string) (envVars []IEnvironmentVariable)

ParseEnvironmentVariables parses a list of key=value entries such as os.Environ() and returns a list of the corresponding environment variables. Any entry failing parsing will be ignored.

func UniqueEnvironmentVariables added in v1.53.0

func UniqueEnvironmentVariables(caseSensitive bool, envvars ...IEnvironmentVariable) (uniqueEnvVars []IEnvironmentVariable)

UniqueEnvironmentVariables returns a list of unique environment variables. caseSensitive states whether two same keys but with different case should be both considered unique.

Jump to

Keyboard shortcuts

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