Add environment control, fix checks and variable definition
This commit is contained in:
parent
09a4952082
commit
ecc668938d
1 changed files with 52 additions and 11 deletions
63
bashutils.sh
63
bashutils.sh
|
@ -25,6 +25,41 @@
|
|||
# unless it licensed under the same license without modifications.
|
||||
|
||||
|
||||
# Environment control
|
||||
function _bashutils_restore_environment() {
|
||||
if [ -n "${_BASHUTILS_ENVIRONMENT_PREVIOUS_OPTIONS}" ]; then
|
||||
[[ "${_BASHUTILS_ENVIRONMENT_PREVIOUS_OPTIONS}" == *"e"* ]] && set -e
|
||||
[[ "${_BASHUTILS_ENVIRONMENT_PREVIOUS_OPTIONS}" != *"e"* ]] && set +e
|
||||
[[ "${_BASHUTILS_ENVIRONMENT_PREVIOUS_OPTIONS}" == *"u"* ]] && set -u
|
||||
[[ "${_BASHUTILS_ENVIRONMENT_PREVIOUS_OPTIONS}" != *"u"* ]] && set +u
|
||||
[[ "${_BASHUTILS_ENVIRONMENT_PREVIOUS_OPTIONS}" == *"x"* ]] && set -x
|
||||
[[ "${_BASHUTILS_ENVIRONMENT_PREVIOUS_OPTIONS}" != *"x"* ]] && set +x
|
||||
[ -n "${_BASHUTILS_ENVIRONMENT_PREVIOUS_PIPEFAIL}" ] && set -o pipefail
|
||||
[ -z "${_BASHUTILS_ENVIRONMENT_PREVIOUS_PIPEFAIL}" ] && set +o pipefail
|
||||
unset _BASHUTILS_ENVIRONMENT_PREVIOUS_OPTIONS
|
||||
fi
|
||||
}
|
||||
function _bashutils_reset_environment() {
|
||||
export "_BASHUTILS_ENVIRONMENT_PREVIOUS_OPTIONS=${-}"
|
||||
[ -o "pipefail" ] && export "_BASHUTILS_ENVIRONMENT_PREVIOUS_PIPEFAIL=true"
|
||||
[ ! -o "pipefail" ] && export "_BASHUTILS_ENVIRONMENT_PREVIOUS_PIPEFAIL="
|
||||
set -eo pipefail
|
||||
set +ux
|
||||
|
||||
if [ -n "${*}" ]; then
|
||||
# shellcheck disable=SC2068
|
||||
${@}
|
||||
EXITCODE="${?}"
|
||||
_bashutils_restore_environment
|
||||
return "${EXITCODE}"
|
||||
fi
|
||||
}
|
||||
function _bashutils_restore_return() {
|
||||
_bashutils_restore_environment
|
||||
return "${1}"
|
||||
}
|
||||
|
||||
|
||||
# Logging
|
||||
function diag() { [ "${BASHUTILS_LOGLEVEL}" -lt 1 ] && echo -e "DIAG ${*//\\n/\\n }"; }
|
||||
function verb() { [ "${BASHUTILS_LOGLEVEL}" -lt 2 ] && echo -e "VERB ${*//\\n/\\n }"; }
|
||||
|
@ -33,6 +68,7 @@ function info() { [ "${BASHUTILS_LOGLEVEL}" -lt 4 ] && echo -e "INFO ${*//\\n/\
|
|||
function warn() { [ "${BASHUTILS_LOGLEVEL}" -lt 5 ] && echo -e "WARN ${*//\\n/\\n }"; }
|
||||
function error() { [ "${BASHUTILS_LOGLEVEL}" -lt 6 ] && echo -e "ERR! ${*//\\n/\\n }" &> /dev/stderr; }
|
||||
function crash() {
|
||||
_bashutils_reset_environment
|
||||
[ "${1}" == "true" ] && CRASH_TERMINATE=true || CRASH_TERMINATE=false
|
||||
shift
|
||||
# shellcheck disable=SC2002
|
||||
|
@ -61,24 +97,29 @@ $(for index in $(seq 0 1000); do if [ -z "$(caller "${index}")" ]; then break; e
|
|||
$([ "${CRASH_TERMINATE}" == "false" ] && echo "!!! This crash will not terminate the shell !!!")
|
||||
EOF
|
||||
[ "${CRASH_TERMINATE}" == "true" ] && exit 69
|
||||
_bashutils_restore_environment
|
||||
}
|
||||
|
||||
# Utility methods
|
||||
function set_undefined() { [ -z "${!1}" ] && export "${1}=${2}"; }
|
||||
function set_defined() { [ -n "${!1}" ] && export "${1}=${2}"; }
|
||||
function is_command_alias() { [ "$(type -t "${1}")" == "alias" ] && return 0 || return 1; }
|
||||
function is_command_keyword() { [ "$(type -t "${1}")" == "keyword" ] && return 0 || return 1; }
|
||||
function is_command_function() { [ "$(type -t "${1}")" == "function" ] && return 0 || return 1; }
|
||||
function is_command_builtin() { [ "$(type -t "${1}")" == "builtin" ] && return 0 || return 1; }
|
||||
function is_command_file() { [ "$(type -t "${1}")" == "file" ] && return 0 || return 1; }
|
||||
function is_command_defined() { [ "$(type -t "${1}")" == "" ] && return 0 || return 1; }
|
||||
# Checks
|
||||
function is_command_alias() { _bashutils_reset_environment; [[ "$(type -t "${1}")" == alias ]]; _bashutils_restore_return ${?}; }
|
||||
function is_command_keyword() { _bashutils_reset_environment; [[ "$(type -t "${1}")" == keyword ]]; _bashutils_restore_return ${?}; }
|
||||
function is_command_function() { _bashutils_reset_environment; [[ "$(type -t "${1}")" == function ]]; _bashutils_restore_return ${?}; }
|
||||
function is_command_builtin() { _bashutils_reset_environment; [[ "$(type -t "${1}")" == builtin ]]; _bashutils_restore_return ${?}; }
|
||||
function is_command_file() { _bashutils_reset_environment; [[ "$(type -t "${1}")" == file ]]; _bashutils_restore_return ${?}; }
|
||||
function is_command_defined() { _bashutils_reset_environment; [[ "$(type -t "${1}")" == "" ]]; _bashutils_restore_return ${?}; }
|
||||
|
||||
# Variable definition
|
||||
function set_undefined() { _bashutils_reset_environment; [ -z "${!1}" ] && export "${1}=${2}"; _bashutils_restore_environment; }
|
||||
function set_defined() { _bashutils_reset_environment; [ -n "${!1}" ] && export "${1}=${2}"; _bashutils_restore_environment; }
|
||||
|
||||
|
||||
# Initialize variables
|
||||
set_undefined BASHUTILS_LOGLEVEL 3
|
||||
unset _BASHUTILS_ENVIRONMENT_PREVIOUS_OPTIONS
|
||||
unset _BASHUTILS_ENVIRONMENT_PREVIOUS_PIPEFAIL
|
||||
set_undefined "BASHUTILS_LOGLEVEL" "3"
|
||||
|
||||
|
||||
################################
|
||||
### BASHUTILS CODE ENDS HERE ###
|
||||
################################
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue