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
Requires
declare grep cut usageDocument __help
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
Requires
throwArgument decorate environmentSecureVariables grepSafe env removeFields
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: - BUILD_HOME PATH LD_LIBRARY USER HOME PS1 PS2 PS3 PS4 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
Reads standard input
Environment File
Writes to standard output
EnvironmentVariable. One per line.
Arguments
--help- Flag. Optional. Display this help.
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
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
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
Usage
environmentFileToBashCompatible [ filename ... ]
Takes any environment file and makes it bash-compatible Outputs the compatible env to stdout
Reads standard input
environment file
Writes to standard output
bash-compatible environment statements
Arguments
filename ...- File. Optional. One or more files to convert.
Return codes
0- Success1- Environment error2- Argument error
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
Usage
environmentFileBashCompatibleToDocker [ filename ]
Ensure an environment file is compatible with non-quoted docker environment files
Reads standard input
text - Environment file to convert. (Optional)
Writes to standard output
text - Only if stdin is supplied and no filename arguments.
Arguments
filename- File. Optional. Docker environment file to check for common issues
Return codes
1- if errors occur0- if file is valid
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
Reads standard input
An environment file of any format
Writes to standard output
Environment file in Bash-compatible format
Arguments
filename ...- File. Optional. Docker environment file to convert.
Return codes
1- if errors occur0- if file is valid
State files
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
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
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
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
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
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
Usage
environmentValueConvertArray encodedValue [ --help ]
Convert an array value which was loaded already
Writes to standard output
Array values separated by newlines
Arguments
encodedValue- String. Required. Value to convert to tokens, one per line--help- Flag. Optional. Display this help.
Return codes
0- Success1- Environment error2- Argument error
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
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 .env
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
- APPLICATION_VERSION - String. This is the version number which can be displayed
- reserved and set to
hookRun version-currentif not set already - APPLICATION_BUILD_DATE - Date. Time when a build was initiated, set upon first invocation if not already
- reserved and set to current date; format like SQL.
- APPLICATION_TAG - String. This is the full version number including debugging or release identifiers
- reserved and set to
hookRun application-id - APPLICATION_ID - String. This is the unique hash which represents the source code state (typically a git hash)
- reserved and set to
hookRun application-tag
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