Environment Tools
Tools to manipulate environment files in Bash:
- Standardizing
- Application defaults
- Reading and writing to/from files
Environment
environmentVariables
Output a list of environment variables and ignore function definitions
Usage
environmentVariables
Output a list of environment variables and ignore function definitions
both set and env output functions and this is an easy way to just output
exported variables
Arguments
- none
Return codes
0- Success1- Environment error2- Argument error
environmentVariableNameValid
Validates zero or more environment variable names.
Usage
environmentVariableNameValid variableName ... [ --help ]
Validates zero or more environment variable names. - alpha - digit - underscore First letter MUST NOT be a digit
Arguments
variableName ...- String. Required. Exit status 0 if all variables names are valid ones.--help- Flag. Optional. Display this help.
Return codes
0- Success1- Environment error2- Argument error
environmentOutput
Output all exported environment variables, hiding secure ones and ones
Usage
environmentOutput [ --underscore ] [ --skip-prefix prefixString ] [ --secure ] [ variable ... ]
Output all exported environment variables, hiding secure ones and ones prefixed with underscore. Any values which contain a newline are also skipped.
Arguments
--underscore- Flag. Optional. Include environment variables which begin with underscore_.--skip-prefix prefixString- String. Optional. Skip environment variables which begin with this exact prefix (case-sensitive).--secure- Flag. Optional. Include environment variables which are inenvironmentSecureVariablesvariable ...- String. Optional. Output these variables explicitly.
Return codes
0- Success1- Environment error2- Argument error
environmentCompile
Load an environment file and evaluate it using bash and
Usage
environmentCompile [ --underscore ] [ --secure ] [ --keep-comments ] [ --variables ] [ --parse ] environmentFile
Load an environment file and evaluate it using bash and output the changed environment variables after running Do not perform this operation on files which are untrusted.
Arguments
--underscore- Flag. Optional. Include environment variables which begin with underscore_.--secure- Flag. Optional. Include environment variables which are inenvironmentSecureVariables--keep-comments- Flag. Keep all comments in the source--variables- CommaDelimitedList. Optional. Always output the value of these variables.--parse- Flag. Optional. Parse the file for things which look like variables to output (basically^foo=)environmentFile- File. Required. Environment file to load, evaluate, and output in raw form (Bash-compatible).
Return codes
0- Success1- Environment error2- Argument error
environmentClean
Clean most exported variables from the current context except a
Usage
environmentClean [ keepEnvironment ]
Clean most exported variables from the current context except a few important ones: - CI PATH LD_LIBRARY USER HOME PS1 PS2 PS3 PS4 BUILD_HOME - __BUILD_DECORATE BUILD_COLORS BUILD_DEBUG BUILD_HOOK_DIRS __BUILD_LOADER Calls unset on any variable in the global environment and exported. Use with caution. Any additional environment variables you wish to preserve, simply pass those on the command line
Arguments
keepEnvironment- EnvironmentVariable. Optional. Keep this environment variable. ZeroOrMore.
Return codes
0- Success1- Environment error2- Argument error
environmentParseVariables
Parse variables from an environment variable stream
Usage
environmentParseVariables [ --help ]
Parse variables from an environment variable stream
Extracts lines with NAME=value
Details:
- Remove export from lines
- Skip lines containing read -r
- Anything before a = is considered a variable name
- Returns a sorted, unique list
Arguments
--help- Flag. Optional. Display this help.
Reads standard input
Environment File
Writes to standard output
EnvironmentVariable. One per line.
Return codes
0- Success1- Environment error2- Argument error
Variable lists
environmentSecureVariables
List environment variables related to security
Usage
environmentSecureVariables [ --help ]
List environment variables related to security
Arguments
--help- Flag. Optional. Display this help.
Return codes
0- Success1- Environment error2- Argument error
environmentApplicationVariables
List environment variables related to application deployments
Usage
environmentApplicationVariables [ --help ]
List environment variables related to application deployments
Arguments
--help- Flag. Optional. Display this help.
Return codes
0- Success1- Environment error2- Argument error
Converting
environmentFileIsDocker
Ensure an environment file is compatible with non-quoted docker environment
Usage
environmentFileIsDocker [ filename ]
Ensure an environment file is compatible with non-quoted docker environment files
Arguments
filename- Docker environment file to check for common issues
Return codes
1- if errors occur0- if file is valid
environmentFileToBashCompatible
Takes any environment file and makes it bash-compatible
Usage
environmentFileToBashCompatible [ filename ... ]
Takes any environment file and makes it bash-compatible Outputs the compatible env to stdout
Arguments
filename ...- File. Optional. One or more files to convert.
Reads standard input
environment file
Writes to standard output
bash-compatible environment statements
Return codes
0- Success1- Environment error2- Argument error
environmentFileToDocker
Takes any environment file and makes it docker-compatible
Usage
environmentFileToDocker envFile ...
Takes any environment file and makes it docker-compatible Outputs the compatible env to stdout
Arguments
envFile ...- File. Required. One or more files to convert.
Return codes
0- Success1- Environment error2- Argument error
environmentFileBashCompatibleToDocker
Ensure an environment file is compatible with non-quoted docker environment
Usage
environmentFileBashCompatibleToDocker [ filename ]
Ensure an environment file is compatible with non-quoted docker environment files
Arguments
filename- File. Optional. Docker environment file to check for common issues
Reads standard input
text - Environment file to convert. (Optional)
Writes to standard output
text - Only if stdin is supplied and no filename arguments.
Return codes
1- if errors occur0- if file is valid
environmentFileDockerToBashCompatible
Ensure an environment file is compatible with non-quoted docker environment
Usage
environmentFileDockerToBashCompatible [ filename ... ]
Ensure an environment file is compatible with non-quoted docker environment files May take a list of files to convert or stdin piped in Outputs bash-compatible entries to stdout Any output to stdout is considered valid output Any output to stderr is errors in the file but is written to be compatible with a bash
Arguments
filename ...- File. Optional. Docker environment file to convert.
Reads standard input
An environment file of any format
Writes to standard output
Environment file in Bash-compatible format
Return codes
1- if errors occur0- if file is valid
State files
environmentLoad
Safely load an environment from stdin (no code execution)
Usage
environmentLoad [ --verbose ] [ --debug ] [ --prefix ] [ --context ] [ --ignore environmentName ] [ --secure environmentName ] [ --secure-defaults ] [ --execute arguments ... ] [ --help ]
Safely load an environment from stdin (no code execution)
Arguments
--verbose- Flag. Optional. Output errors with variables.--debug- Flag. Optional. Debugging mode, for developers probably.--prefix- String. Optional. Prefix each environment variable defined with this string. e.g.NAME->DSN_NAMEfor--prefix DSN_--context- String. Optional. Name of the context for debugging or error messages. (e.g. what is this doing for whom and why)--ignore environmentName- String. Optional. Environment value to ignore on load.--secure environmentName- String. Optional. If found, entire load fails.--secure-defaults- Flag. Optional. Add a list of environment variables considered security risks to the--ignorelist.--execute arguments ...- Callable. Optional. All additional arguments are passed to callable after loading environment.--help- Flag. Optional. Display this help.
Return codes
2- if file does not exist; outputs an error0- if files are loaded successfully
environmentFileLoad
Safely load an environment file (no code execution)
Usage
environmentFileLoad [ --prefix ] --require [ --optional ] [ --verbose ] environmentFile [ --ignore environmentName ] [ --secure environmentName ] [ --secure-defaults ] [ --execute arguments ... ] [ --help ]
Safely load an environment file (no code execution)
Arguments
--prefix- EnvironmentVariable|Blank. Optional. All subsequent environment variables are prefixed with this prefix.--require- Flag. Optional. All subsequent environment files on the command line will be required.--optional- Flag. Optional. All subsequent environment files on the command line will be optional. (If they do not exist, no errors.)--verbose- Flag. Optional. Output errors with variables in files.environmentFile- Required. Environment file to load. For--optionalfiles the directory must exist.--ignore environmentName- String. Optional. Environment value to ignore on load.--secure environmentName- String. Optional. If found in a loaded file, entire file fails.--secure-defaults- Flag. Optional. Add a list of environment variables considered security risks to the--ignorelist.--execute arguments ...- Callable. Optional. All additional arguments are passed to callable after loading environment files.--help- Flag. Optional. Display this help.
Return codes
2- if file does not exist; outputs an error0- if files are loaded successfully
environmentValueWrite
Write a value to a state file as NAME="value"
Usage
environmentValueWrite name [ value ] [ ... ] [ --help ]
Write a value to a state file as NAME="value"
Arguments
name- String. Required. Name to write.value- EmptyString. Optional. Value to write....- EmptyString. Optional. Additional values, when supplied, write this value as an array.--help- Flag. Optional. Display this help.
Return codes
0- Success1- Environment error2- Argument error
environmentValueWriteArray
Write an array value as NAME=([0]="a" [1]="b" [2]="c")
Usage
environmentValueWriteArray [ --help ] [ value ... ] [ --help ]
Write an array value as NAME=([0]="a" [1]="b" [2]="c") Supports empty arrays Bash outputs on different versions: declare -a foo='([0]="a" [1]="b" [2]="c")' declare -a foo=([0]="a" [1]="b" [2]="c")
Arguments
--help- Flag. Optional. Display this help.value ...- Arguments. Optional. Array values as arguments.--help- Flag. Optional. Display this help.
Return codes
0- Success1- Environment error2- Argument error
environmentValueRead
undocumented
Usage
environmentValueRead stateFile name [ default ] [ --help ]
No documentation for environmentValueRead.
Arguments
stateFile- EnvironmentFile. Required. File to read a value from.name- EnvironmentVariable. Required. Variable to read.default- EmptyString. Optional. Default value of the environment variable if it does not exist.--help- Flag. Optional. Display this help.
Return codes
1- If value is not found and no default argument is supplied (2 arguments)0- If value
environmentValueReadArray
Read an array value from a state file
Usage
environmentValueReadArray stateFile name [ --help ]
Read an array value from a state file Outputs array elements, one per line.
Arguments
stateFile- File. Required. File to access, must exist.name- EnvironmentVariable. Required. Name to read.--help- Flag. Optional. Display this help.
Return codes
0- Success1- Environment error2- Argument error
environmentValueConvertArray
Convert an array value which was loaded already
Usage
environmentValueConvertArray encodedValue [ --help ]
Convert an array value which was loaded already
Arguments
encodedValue- String. Required. Value to convert to tokens, one per line--help- Flag. Optional. Display this help.
Writes to standard output
Array values separated by newlines
Return codes
0- Success1- Environment error2- Argument error
environmentLines
List lines of environment values set in a bash state
Usage
environmentLines [ --help ]
List lines of environment values set in a bash state file
Arguments
--help- Flag. Optional. Display this help.
Examples
environmentLines < "$stateFile"
Return codes
0- Success1- Environment error2- Argument error
environmentNames
List names of environment values set in a bash state
Usage
environmentNames [ --help ]
List names of environment values set in a bash state file
Arguments
--help- Flag. Optional. Display this help.
Examples
environmentNames < "$stateFile"
Return codes
0- Success1- Environment error2- Argument error
Application Environment (.env)
environmentFileShow
Display and validate application variables.
Usage
environmentFileShow [ environmentName ] [ -- ] [ optionalEnvironmentName ]
Display and validate application variables.
Arguments
environmentName- EnvironmentVariable. Optional. A required environment variable name--- Separator. Optional. Separates requires from optional environment variablesoptionalEnvironmentName- EnvironmentVariable. Optional. An optional environment variable name.
Return codes
1- If any required application variables are blank, the function fails with an environment error0- All required application variables are non-blank
environmentApplicationLoad
Loads application environment variables, set them to their default values
Usage
environmentApplicationLoad [ --help ]
Loads application environment variables, set them to their default values if needed, and outputs the list of variables and values.
Arguments
--help- Flag. Optional. Display this help.
Return codes
0- Success1- Environment error2- Argument error
Environment
- {SEE:BUILD_TIMESTAMP.sh}
- {SEE:APPLICATION_BUILD_DATE.sh}
- {SEE:APPLICATION_VERSION.sh}
- {SEE:APPLICATION_ID.sh}
- {SEE:APPLICATION_TAG.sh}
environmentFileApplicationMake
Create environment file
.envfor build.
Usage
environmentFileApplicationMake [ --help ] [ requiredVariable ... ] [ -- ] [ optionalVariable ... ]
Create environment file .env for build.
Note that this does NOT change or modify the current environment.
Arguments
--help- Flag. Optional. Display this help.requiredVariable ...- EnvironmentVariable. Optional. One or more environment variables which should be non-blank and included in the.envfile.--- Divider. Optional. Divides the requiredEnvironment values from the optionalEnvironment. Should appear once and only once.optionalVariable ...- EnvironmentVariable. Optional. One or more environment variables which are included if blank or not
Return codes
0- Success1- Environment error2- Argument error
Environment
- {SEE:APPLICATION_VERSION.sh} - reserved and set to
hookRun version-currentif not set already - {SEE:APPLICATION_BUILD_DATE.sh} - reserved and set to current date; format like SQL.
- {SEE:APPLICATION_TAG.sh} - reserved and set to
hookRun application-id - {SEE:APPLICATION_ID.sh} - reserved and set to
hookRun application-tag
environmentFileApplicationVerify
Check application environment is populated correctly.
Usage
environmentFileApplicationVerify [ --help ] [ requiredEnvironment ... ] [ -- ] [ optionalEnvironment ... ]
Check application environment is populated correctly.
Also verifies that environmentApplicationVariables and environmentApplicationLoad are defined.
Arguments
--help- Flag. Optional. Display this help.requiredEnvironment ...- EnvironmentName. Optional. One or more environment variables which should be non-blank and included in the.envfile.--- Divider. Optional. Divides the requiredEnvironment values from the optionalEnvironmentoptionalEnvironment ...- EnvironmentName. Optional. One or more environment variables which are included if blank or not
Return codes
0- Success1- Environment error2- Argument error