Merge pull request #1938 from gaelicWizard/_command_exists

Use `_command_exists` everywhere
pull/1960/head
Noah Gorny 2021-09-20 14:56:45 +03:00 committed by GitHub
commit 8c697715eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
36 changed files with 163 additions and 96 deletions

View File

@ -1,4 +1,4 @@
#!/bin/bash
# shellcheck shell=bash
#
# -binaryanomaly
@ -8,7 +8,8 @@ about-alias 'Apt and dpkg aliases for Ubuntu and Debian distros.'
# set apt aliases
function _set_pkg_aliases()
{
if [ -x $(which apt) ]; then
if _command_exists apt
then
alias apts='apt-cache search'
alias aptshow='apt-cache show'
alias aptinst='sudo apt-get install -V'

View File

@ -6,7 +6,8 @@ about-alias 'Curl aliases for convenience.'
# set apt aliases
function _set_pkg_aliases()
{
if [ -x $(which curl) ]; then
if _command_exists curl
then
# follow redirects
alias cl='curl -L'
# follow redirects, download as original name

View File

@ -30,7 +30,7 @@ then
alias grep='grep --color=auto'
fi
if which gshuf &> /dev/null
if _command_exists gshuf
then
alias shuf=gshuf
fi
@ -65,7 +65,7 @@ alias -- -='cd -' # Go back
alias h='history'
# Tree
if [ ! -x "$(which tree 2>/dev/null)" ]
if ! _command_exists tree
then
alias tree="find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'"
fi

View File

@ -2,11 +2,7 @@
cite 'about-alias'
about-alias 'vim abbreviations'
VIM=$(command -v vim)
GVIM=$(command -v gvim)
MVIM=$(command -v mvim)
if [[ -n $VIM ]]; then
if _command_exists vim; then
alias v='$VIM'
# open the vim help in fullscreen incorporated from
# https://stackoverflow.com/a/4687513
@ -17,9 +13,9 @@ fi
# http://stackoverflow.com/questions/936501/let-gvim-always-run-a-single-instancek
case $OSTYPE in
darwin*)
[[ -n $MVIM ]] && function mvimt { command mvim --remote-tab-silent "$@" || command mvim "$@"; }
_command_exists mvim && function mvimt { command mvim --remote-tab-silent "$@" || command mvim "$@"; }
;;
*)
[[ -n $GVIM ]] && function gvimt { command gvim --remote-tab-silent "$@" || command gvim "$@"; }
_command_exists gvim && function gvimt { command gvim --remote-tab-silent "$@" || command gvim "$@"; }
;;
esac

View File

@ -145,7 +145,7 @@ if [ -e "$HOME/.jekyllconfig" ]; then
fi
# BASH_IT_RELOAD_LEGACY is set.
if ! command -v reload &> /dev/null && [ -n "${BASH_IT_RELOAD_LEGACY:-}" ]; then
if ! _command_exists reload && [[ -n "${BASH_IT_RELOAD_LEGACY:-}" ]]; then
case $OSTYPE in
darwin*)
alias reload='source ~/.bash_profile'

View File

@ -77,8 +77,10 @@ completion/available/wpscan.completion.bash
# plugins
#
plugins/available/alias-completion.plugin.bash
plugins/available/autojump.plugin.bash
plugins/available/basher.plugin.bash
plugins/available/cmd-returned-notify.plugin.bash
plugins/available/direnv.plugin.bash
plugins/available/docker-machine.plugin.bash
plugins/available/git.plugin.bash
plugins/available/go.plugin.bash
@ -86,6 +88,14 @@ plugins/available/goenv.plugin.bash
plugins/available/history-search.plugin.bash
plugins/available/history-substring-search.plugin.bash
plugins/available/history.plugin.bash
plugins/available/hub.plugin.bash
plugins/available/jump.plugin.bash
plugins/available/node.plugin.bash
plugins/available/nodenv.plugin.bash
plugins/available/plenv.plugin.bash
plugins/available/pyenv.plugin.bash
plugins/available/rbenv.plugin.bash
plugins/available/textmate.plugin.bash
plugins/available/xterm.plugin.bash
# tests

View File

@ -1 +1,6 @@
[[ -x "$(which aws_completer)" ]] && complete -C "$(which aws_completer)" aws
# shellcheck shell=bash
if _command_exists aws_completer
then
complete -C "$(command -v aws_completer)" aws
fi

View File

@ -3,5 +3,5 @@ cite "about-completion"
about-completion "Hashicorp consul completion"
if _command_exists consul; then
complete -C "$(which consul)" consul
complete -C "$(command -v consul)" consul
fi

View File

@ -676,7 +676,7 @@ _docker_compose() {
done
local completions_func=_docker_compose_${command//-/_}
declare -F $completions_func >/dev/null && $completions_func
_is_function $completions_func && $completions_func
eval "$previous_extglob_setting"
return 0

View File

@ -172,6 +172,7 @@ __git_flow_list_hotfixes ()
}
# temporarily wrap __git_find_on_cmdline() for backwards compatibility
if [ -z "`type -t __git_find_subcommand`" ]; then
if ! _command_exists __git_find_subcommand
then
alias __git_find_subcommand=__git_find_on_cmdline
fi

View File

@ -505,6 +505,7 @@ __git_flow_list_branches ()
}
# alias __git_find_on_cmdline for backwards compatibility
if [ -z "`type -t __git_find_on_cmdline`" ]; then
if ! _command_exists __git_find_on_cmdline
then
alias __git_find_on_cmdline=__git_find_subcommand
fi

View File

@ -58,9 +58,9 @@ __gradle-set-cache-name() {
__gradle-set-files-checksum() {
# Cache MD5 sum of all Gradle scripts and modified timestamps
if builtin command -v md5 > /dev/null; then
if _command_exists md5; then
gradle_files_checksum=$(md5 -q -s "$(cat "$cache_dir/$cache_name" | xargs ls -o 2>/dev/null)")
elif builtin command -v md5sum > /dev/null; then
elif _command_exists md5sum; then
gradle_files_checksum=$(cat "$cache_dir/$cache_name" | xargs ls -o 2>/dev/null | md5sum | awk '{print $1}')
else
echo "Cannot generate completions as neither md5 nor md5sum exist on \$PATH"

View File

@ -23,12 +23,12 @@
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# If there is no git tab completion, but we have the _completion loader try to load it
if ! declare -F _git > /dev/null && declare -F _completion_loader > /dev/null; then
if ! _is_function _git && _is_function _completion_loader; then
_completion_loader git
fi
# Check that git tab completion is available and we haven't already set up completion
if declare -F _git > /dev/null && ! declare -F __git_list_all_commands_without_hub > /dev/null; then
if _is_function _git && ! _is_function __git_list_all_commands_without_hub; then
# Duplicate and rename the 'list_all_commands' function
eval "$(declare -f __git_list_all_commands | \
sed 's/__git_list_all_commands/__git_list_all_commands_without_hub/')"

View File

@ -1,7 +1,9 @@
#!/usr/bin/bash
if command -v laravel > /dev/null; then
__laravel_completion() {
if _command_exists laravel
then
function __laravel_completion()
{
local OPTS=("-h --help -q --quiet --ansi --no-ansi -n --no-interaction -v -vv -vvv --verbose help list new")
COMPREPLY=()
for _opt_ in ${OPTS[@]}; do

View File

@ -1 +1,6 @@
[[ -x "$(which pew)" ]] && source "$(pew shell_config)"
# shellcheck shell=bash
if _command_exists pew
then
source "$(pew shell_config)"
fi

View File

@ -1,4 +1,4 @@
#!/bin/bash
# shellcheck shell=bash
# ---------------------------------------------------------------------------+
# |
@ -7,15 +7,16 @@
# |
# ---------------------------------------------------------------------------+
if command -v sqlmap > /dev/null; then
if _command_exists sqlmap
then
_sqlmap()
function _sqlmap()
{
local cur prev
COMPREPLY=()
cur=$(_get_cword)
prev=$(_get_pword)
cur="$(_get_cword)"
prev="$(_get_pword)"
case $prev in

View File

@ -1,5 +1,10 @@
if [[ -x "$(which travis)" ]]; then
__TRAVIS_COMPLETION_SCRIPT="${TRAVIS_CONFIG_PATH:-${HOME}/.travis}/travis.sh"
[[ -f "${__TRAVIS_COMPLETION_SCRIPT}" ]] && source "${__TRAVIS_COMPLETION_SCRIPT}"
# shellcheck shell=bash
if _command_exists travis
then
if [[ -s "${__TRAVIS_COMPLETION_SCRIPT:=${TRAVIS_CONFIG_PATH:-${HOME}/.travis}/travis.sh}" ]]
then
source "${__TRAVIS_COMPLETION_SCRIPT}"
fi
unset __TRAVIS_COMPLETION_SCRIPT
fi

View File

@ -1,7 +1,7 @@
# shellcheck shell=bash
if command -v wpscan > /dev/null; then
__wpscan_completion() {
if _command_exists wpscan; then
function __wpscan_completion() {
local OPTS=("--help --hh --version --url --ignore-main-redirect --verbose --output --format --detection-mode --scope --headers --user-agent --vhost --random-user-agent --user-agents-list --http-auth --max-threads --throttle --request-timeout --connect-timeout --disable-tlc-checks --proxy --proxy-auth --cookie-string --cookie-jar --cache-ttl --clear-cache --server --cache-dir --update --no-update --wp-content-dir --wp-plugins-dir --wp-version-detection --main-theme-detection --enumerate --exclude-content-based --plugins-list --plugins-detection --plugins-version-all --plugins-version-detection --themes-list --themes-detection --themes-version-all --themes-version-detection --timthumbs-list --timthumbs-detection --config-backups-list --config-backups-detection --db-exports-list --db-exports-detection --medias-detection --users-list --users-detection --passwords --usernames --multicall-max-passwords --password-attack --stealthy")
COMPREPLY=()
for _opt_ in "${OPTS[@]}"; do

16
lib/helpers.bash 100644 → 100755
View File

@ -23,7 +23,13 @@ function _command_exists ()
_example '$ _command_exists ls && echo exists'
_group 'lib'
local msg="${2:-Command '$1' does not exist!}"
type "$1" &> /dev/null || (_log_warning "$msg" && return 1) ;
if type -t "$1" &> /dev/null
then
return 0
else
_log_warning "$msg"
return 1
fi
}
function _binary_exists ()
@ -34,7 +40,13 @@ function _binary_exists ()
_example '$ _binary_exists ls && echo exists'
_group 'lib'
local msg="${2:-Binary '$1' does not exist!}"
type -P "$1" &> /dev/null || (_log_warning "$msg" && return 1) ;
if type -P "$1" &> /dev/null
then
return 0
else
_log_warning "$msg"
return 1
fi
}
function _completion_exists ()

View File

@ -1,12 +1,15 @@
# shellcheck shell=bash
cite about-plugin
about-plugin 'Autojump configuration, see https://github.com/wting/autojump for more details'
# Only supports the Homebrew variant, Debian and Arch at the moment.
# Feel free to provide a PR to support other install locations
if _bash_it_homebrew_check && [[ -s "${BASH_IT_HOMEBREW_PREFIX}/etc/profile.d/autojump.sh" ]]; then
. "${BASH_IT_HOMEBREW_PREFIX}/etc/profile.d/autojump.sh"
elif command -v dpkg &>/dev/null && dpkg -s autojump &>/dev/null ; then
. "$(dpkg-query -S autojump.sh | cut -d' ' -f2)"
elif command -v pacman &>/dev/null && pacman -Q autojump &>/dev/null ; then
. "$(pacman -Ql autojump | grep autojump.sh | cut -d' ' -f2)"
source "${BASH_IT_HOMEBREW_PREFIX}/etc/profile.d/autojump.sh"
elif _command_exists dpkg && dpkg -s autojump &> /dev/null; then
# shellcheck disable=SC1090
source "$(dpkg-query -S autojump.sh | cut -d' ' -f2)"
elif _command_exists pacman && pacman -Q autojump &> /dev/null; then
# shellcheck disable=SC1090
source "$(pacman -Ql autojump | grep autojump.sh | cut -d' ' -f2)"
fi

View File

@ -5,10 +5,10 @@ function ips ()
{
about 'display all ip addresses for this host'
group 'base'
if command -v ifconfig &>/dev/null
if _command_exists ifconfig
then
ifconfig | awk '/inet /{ gsub(/addr:/, ""); print $2 }'
elif command -v ip &>/dev/null
elif _command_exists ip
then
ip addr | grep -oP 'inet \K[\d.]+'
else
@ -70,7 +70,7 @@ function passgen ()
# Create alias pass to passgen when pass isn't installed or
# BASH_IT_LEGACY_PASS is true.
if ! command -v pass &>/dev/null || [[ "${BASH_IT_LEGACY_PASS:-}" = true ]]
if ! _command_exists pass || [[ "${BASH_IT_LEGACY_PASS:-}" = true ]]
then
alias pass=passgen
fi
@ -81,7 +81,7 @@ function pmdown ()
param '1: markdown file'
example '$ pmdown README.md'
group 'base'
if command -v markdown &>/dev/null
if _command_exists markdown
then
markdown $1 | browser
else

View File

@ -1,4 +1,7 @@
# shellcheck shell=bash
cite about-plugin
about-plugin 'load direnv, if you are using it: https://direnv.net/'
[ -x "$(which direnv)" ] && eval "$(direnv hook bash)"
if _command_exists direnv; then
eval "$(direnv hook bash)"
fi

View File

@ -1,4 +1,7 @@
# shellcheck shell=bash
cite about-plugin
about-plugin 'load hub, if you are using it'
command -v hub &> /dev/null && eval "$(hub alias -s)"
if _command_exists hub; then
eval "$(hub alias -s)"
fi

View File

@ -1,9 +1,12 @@
# shellcheck shell=bash
# shellcheck disable=SC2016
cite about-plugin
about-plugin 'initialize jump (see https://github.com/gsamokovarov/jump). Add `export JUMP_OPTS=("--bind=z")` to change keybinding'
__init_jump() {
command -v jump &> /dev/null || return
function __init_jump() {
if _command_exists jump; then
eval "$(jump shell bash "${JUMP_OPTS[@]}")"
fi
}
__init_jump

View File

@ -1,13 +1,14 @@
# shellcheck shell=bash
cite about-plugin
about-plugin 'Node.js helper functions'
# Check that we have npm
_command_exists npm || return
# Ensure local modules are preferred in PATH
pathmunge "./node_modules/.bin" "after"
# Check that we have npm
out=$(command -v npm 2>&1) || return
# If not using nodenv, ensure global modules are in PATH
if [[ ! $out == *"nodenv/shims"* ]] ; then
if [[ ! "$(type -p npm)" == *"nodenv/shims"* ]]; then
pathmunge "$(npm config get prefix)/bin" "after"
fi

View File

@ -1,7 +1,10 @@
# shellcheck shell=bash
cite about-plugin
about-plugin 'load nodenv, if you are using it'
export NODENV_ROOT="$HOME/.nodenv"
pathmunge "$NODENV_ROOT/bin"
[[ `which nodenv` ]] && eval "$(nodenv init - bash)"
if _command_exists nodenv; then
eval "$(nodenv init - bash)"
fi

View File

@ -1,22 +1,23 @@
# Bash-it no longer bundles nvm, as this was quickly becoming outdated.
# shellcheck shell=bash
#
# BASH_IT_LOAD_PRIORITY: 225
#
# Bash-it no longer bundles nvm, as this was quickly becoming outdated.
# Please install nvm from https://github.com/creationix/nvm.git if you want to use it.
cite about-plugin
about-plugin 'node version manager configuration'
export NVM_DIR=${NVM_DIR:-$HOME/.nvm}
export NVM_DIR="${NVM_DIR:-$HOME/.nvm}"
# This loads nvm
if _bash_it_homebrew_check && [ -s "${BASH_IT_HOMEBREW_PREFIX}/nvm.sh" ]
if _bash_it_homebrew_check && [[ -s "${BASH_IT_HOMEBREW_PREFIX}/nvm.sh" ]]
then
. "${BASH_IT_HOMEBREW_PREFIX}/nvm.sh"
source "${BASH_IT_HOMEBREW_PREFIX}/nvm.sh"
else
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
[[ -s "$NVM_DIR/nvm.sh" ]] && source "$NVM_DIR/nvm.sh"
fi
if ! command -v nvm &>/dev/null
if ! _command_exists nvm
then
function nvm() {
echo "Bash-it no longer bundles the nvm script. Please install the latest version from"

View File

@ -1,18 +1,16 @@
# shellcheck shell=bash
#
# plugin for plenv
cite about-plugin
about-plugin 'plenv plugin for Perl'
if [[ -e "${HOME}/.plenv/bin" ]] ; then
if [[ -d "${HOME}/.plenv/bin" ]]; then
# load plenv bin dir into path if it exists
pathmunge "${HOME}/.plenv/bin"
fi
if [[ `which plenv` ]] ; then
if _command_exists plenv; then
# init plenv
eval "$(plenv init - bash)"
fi

View File

@ -1,9 +1,9 @@
#!/usr/bin/env bash
# shellcheck shell=bash
cite about-plugin
about-plugin 'enables powerline daemon'
command -v powerline-daemon &>/dev/null || return
_command_exists powerline-daemon || return
powerline-daemon -q
#the following should not be executed if bashit powerline themes in use
@ -14,13 +14,13 @@ case "$BASH_IT_THEME" in
esac
POWERLINE_BASH_CONTINUATION=1
POWERLINE_BASH_SELECT=1
bashPowerlineInit=$(python -c \
bashPowerlineInit="$(python -c \
"import os; \
import powerline;\
print(os.path.join(os.path.dirname(\
powerline.__file__),\
'bindings', \
'bash', \
'powerline.sh'))")
'powerline.sh'))")"
[ -e $bashPowerlineInit ] || return
. $bashPowerlineInit
source $bashPowerlineInit

View File

@ -1,10 +1,13 @@
# shellcheck shell=bash
cite about-plugin
about-plugin 'load pyenv, if you are using it'
export PYENV_ROOT="$HOME/.pyenv"
pathmunge "$PYENV_ROOT/bin"
[[ `which pyenv 2>/dev/null` ]] && eval "$(pyenv init - bash)"
if _command_exists pyenv; then
eval "$(pyenv init - bash)"
fi
#Load pyenv virtualenv if the virtualenv plugin is installed.
if pyenv virtualenv-init - &> /dev/null; then

View File

@ -1,7 +1,10 @@
# shellcheck shell=bash
cite about-plugin
about-plugin 'load rbenv, if you are using it'
export RBENV_ROOT="$HOME/.rbenv"
pathmunge "$RBENV_ROOT/bin"
[[ `which rbenv 2>/dev/null` ]] && eval "$(rbenv init - bash)"
if _command_exists rbenv; then
eval "$(rbenv init - bash)"
fi

View File

@ -1,9 +1,11 @@
# shellcheck shell=bash
cite about-plugin
about-plugin 'ruby and rubygems specific functions and settings'
# Make commands installed with 'gem install --user-install' available
# ~/.gem/ruby/${RUBY_VERSION}/bin/
if which ruby >/dev/null && which gem >/dev/null; then
if _command_exists ruby && _command_exists gem
then
pathmunge "$(ruby -e 'print Gem.user_dir')/bin" after
fi

View File

@ -1,7 +1,9 @@
# shellcheck shell=bash
cite about-plugin
about-plugin 'set textmate as a default editor'
if $(command -v mate &> /dev/null) ; then
export EDITOR="$(which mate) -w"
export GIT_EDITOR=$EDITOR
if _command_exists mate; then
EDITOR="$(type -p mate) -w"
GIT_EDITOR="$EDITOR"
export EDITOR GIT_EDITOR
fi

View File

@ -1,3 +1,5 @@
# shellcheck shell=bash
#
# make sure virtualenvwrapper is enabled if available
cite about-plugin
@ -5,8 +7,8 @@ about-plugin 'virtualenvwrapper and pyenv-virtualenvwrapper helper functions'
if _command_exists pyenv; then
pyenv virtualenvwrapper
else
[[ `which virtualenvwrapper.sh` ]] && . virtualenvwrapper.sh
elif _command_exists virtualenvwrapper.sh; then
source virtualenvwrapper.sh
fi

View File

@ -396,7 +396,7 @@ function hg_prompt_vars {
function nvm_version_prompt {
local node
if declare -f -F nvm &> /dev/null; then
if _is_function nvm; then
node=$(nvm current 2> /dev/null)
[[ "${node}" == "system" ]] && return
echo -e "${NVM_THEME_PROMPT_PREFIX}${node}${NVM_THEME_PROMPT_SUFFIX}"
@ -433,8 +433,8 @@ function rbfu_version_prompt {
}
function chruby_version_prompt {
if declare -f -F chruby &> /dev/null; then
if declare -f -F chruby_auto &> /dev/null; then
if _is_function chruby; then
if _is_function chruby_auto; then
chruby_auto
fi

2
themes/dulcie/dulcie.theme.bash 100644 → 100755
View File

@ -77,7 +77,7 @@ dulcie_prompt() {
printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/\~}"
# Open the new terminal in the same directory
declare -f __vte_osc7 > /dev/null && __vte_osc7
_is_function __vte_osc7 && __vte_osc7
PS1="${reset_color}[${DULCIE_USER}@${DULCIE_HOST}$(scm_prompt_info)${reset_color} ${DULCIE_WORKINGDIR}]"
if [[ "${DULCIE_MULTILINE}" -eq "1" ]]; then