Skip to content

Commit

Permalink
Merge pull request #1938 from gaelicWizard/_command_exists
Browse files Browse the repository at this point in the history
Use `_command_exists` everywhere
  • Loading branch information
Noah Gorny authored Sep 20, 2021
2 parents 4700d7b + fb6e05d commit 8c69771
Show file tree
Hide file tree
Showing 36 changed files with 163 additions and 96 deletions.
5 changes: 3 additions & 2 deletions aliases/available/apt.aliases.bash
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
# shellcheck shell=bash
#
# -binaryanomaly

Expand All @@ -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'
Expand Down
3 changes: 2 additions & 1 deletion aliases/available/curl.aliases.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions aliases/available/general.aliases.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 3 additions & 7 deletions aliases/available/vim.aliases.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion bash_it.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
10 changes: 10 additions & 0 deletions clean_files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,25 @@ 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
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
Expand Down
7 changes: 6 additions & 1 deletion completion/available/awscli.completion.bash
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion completion/available/consul.completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion completion/available/docker-compose.completion.bash
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion completion/available/git_flow.completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion completion/available/git_flow_avh.completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions completion/available/gradle.completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions completion/available/hub.completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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/')"
Expand Down
8 changes: 5 additions & 3 deletions completion/available/laravel.completion.bash
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#!/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
if [[ "$_opt_" == "$2"* ]]; then
COMPREPLY+=("$_opt_")
fi
done
}
}

complete -F __laravel_completion laravel
fi
7 changes: 6 additions & 1 deletion completion/available/pew.completion.bash
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
[[ -x "$(which pew)" ]] && source "$(pew shell_config)"
# shellcheck shell=bash

if _command_exists pew
then
source "$(pew shell_config)"
fi
17 changes: 9 additions & 8 deletions completion/available/sqlmap.completion.bash
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
#!/bin/bash
# shellcheck shell=bash

# ---------------------------------------------------------------------------+
# |
# Thanks to Alexander Korznikov |
# Thanks to Alexander Korznikov |
# http://www.korznikov.com/2014/12/bash-tab-completion-for-awesome-tool.html |
# |
# ---------------------------------------------------------------------------+

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

Expand Down Expand Up @@ -143,7 +144,7 @@ if command -v sqlmap > /dev/null; then
--mobile --page-rank --purge-output --smart \
--sqlmap-shell --wizard '
COMPREPLY=( $( \
(while read -d ' ' i; do
(while read -d ' ' i; do
[[ -z "$i" || "${onlyonce/ ${i%% *} / }" == "$onlyonce" ]] &&
continue
# flatten array with spaces on either side,
Expand All @@ -152,7 +153,7 @@ if command -v sqlmap > /dev/null; then
COMPREPLY=" ${COMPREPLY[@]} "
# remove word from list of completions
COMPREPLY=( ${COMPREPLY/ ${i%% *} / } )
done
done
printf '%s ' "${COMPREPLY[@]}") <<<"${COMP_WORDS[@]}"
) )

Expand Down
11 changes: 8 additions & 3 deletions completion/available/travis.completion.bash
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions completion/available/wpscan.completion.bash
Original file line number Diff line number Diff line change
@@ -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
Expand Down
16 changes: 14 additions & 2 deletions lib/helpers.bash
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand All @@ -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 ()
Expand Down
13 changes: 8 additions & 5 deletions plugins/available/autojump.plugin.bash
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions plugins/available/base.plugin.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion plugins/available/direnv.plugin.bash
Original file line number Diff line number Diff line change
@@ -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
5 changes: 4 additions & 1 deletion plugins/available/hub.plugin.bash
Original file line number Diff line number Diff line change
@@ -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
9 changes: 6 additions & 3 deletions plugins/available/jump.plugin.bash
Original file line number Diff line number Diff line change
@@ -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
eval "$(jump shell bash "${JUMP_OPTS[@]}")"
function __init_jump() {
if _command_exists jump; then
eval "$(jump shell bash "${JUMP_OPTS[@]}")"
fi
}

__init_jump
Loading

0 comments on commit 8c69771

Please sign in to comment.