lib/helpers: fix `_command_exists()`

The weird subshell is weird AF. Just do a normal `if`.

Ditto `_binary_exists()`, `_completion_exists()`, and `_is_function()`!
pull/1904/head
John D Pell 2021-10-09 23:22:10 -07:00
parent 1dfa28af25
commit 9c77c0f61c
1 changed files with 53 additions and 45 deletions

98
lib/helpers.bash 100755 → 100644
View File

@ -18,49 +18,49 @@ case "$OSTYPE" in
'darwin'*) BASH_IT_SED_I_PARAMETERS=(-i "") 'darwin'*) BASH_IT_SED_I_PARAMETERS=(-i "")
esac esac
function _command_exists () function _command_exists() {
{ _about 'checks for existence of a command'
_about 'checks for existence of a command' _param '1: command to check'
_param '1: command to check' _param '2: (optional) log message to include when command not found'
_param '2: (optional) log message to include when command not found' _example '$ _command_exists ls && echo exists'
_example '$ _command_exists ls && echo exists' _group 'lib'
_group 'lib' local msg="${2:-Command '$1' does not exist\!}"
local msg="${2:-Command '$1' does not exist!}" if type "$1" &> /dev/null; then
if type -t "$1" &> /dev/null return "$?"
then else
return 0 _log_debug "$msg"
else return 1
_log_warning "$msg" fi
return 1
fi
} }
function _binary_exists () function _binary_exists() {
{ _about 'checks for existence of a binary'
_about 'checks for existence of a binary' _param '1: binary to check'
_param '1: binary to check' _param '2: (optional) log message to include when binary not found'
_param '2: (optional) log message to include when binary not found' _example '$ _binary_exists ls && echo exists'
_example '$ _binary_exists ls && echo exists' _group 'lib'
_group 'lib' local msg="${2:-Binary '$1' does not exist\!}"
local msg="${2:-Binary '$1' does not exist!}" if type -P "$1" &> /dev/null; then
if type -P "$1" &> /dev/null return "$?"
then else
return 0 _log_debug "$msg"
else return 1
_log_warning "$msg" fi
return 1
fi
} }
function _completion_exists () function _completion_exists() {
{ _about 'checks for existence of a completion'
_about 'checks for existence of a completion' _param '1: command to check'
_param '1: command to check' _param '2: (optional) log message to include when completion is found'
_param '2: (optional) log message to include when completion is found' _example '$ _completion_exists gh && echo exists'
_example '$ _completion_exists gh && echo exists' _group 'lib'
_group 'lib' local msg="${2:-Completion for '$1' already exists!}"
local msg="${2:-Completion for '$1' already exists!}" if complete -p "$1" &> /dev/null; then
complete -p "$1" &> /dev/null && _log_warning "$msg" ; return "$?"
else
_log_debug "$msg"
return 1
fi
} }
function _bash_it_homebrew_check() function _bash_it_homebrew_check()
@ -180,12 +180,20 @@ bash-it ()
fi fi
} }
_is_function () function _is_function() {
{ _about 'sets $? to true if parameter is the name of a function'
_about 'sets $? to true if parameter is the name of a function' _param '1: name of alleged function'
_param '1: name of alleged function' _param '2: (optional) log message to include when function not found'
_group 'lib' _group 'lib'
LANG=C type -t "$1" | grep -q 'function' _example '$ _is_function ls && echo exists'
_group 'lib'
local msg="${2:-Function '$1' does not exist!}"
if LC_ALL=C type -t "$1" | grep -q 'function'; then
return "$?"
else
_log_debug "$msg"
return 1
fi
} }
_bash-it-aliases () _bash-it-aliases ()