lib/helpers: `shfmt`

My apologies to future `git blame` hunters ♥
pull/1904/head
John D Pell 2021-10-16 14:32:28 -07:00
parent a0a359f22c
commit 5043c78701
2 changed files with 574 additions and 579 deletions

View File

@ -77,6 +77,7 @@ completion/available/vuejs.completion.bash
completion/available/wpscan.completion.bash
# libraries
lib/helpers.bash
lib/utilities.bash
# plugins

View File

@ -64,18 +64,18 @@ function _completion_exists() {
fi
}
function _bash_it_homebrew_check()
{
if _binary_exists 'brew'
then # Homebrew is installed
if [[ "${BASH_IT_HOMEBREW_PREFIX:-unset}" == 'unset' ]]
then # variable isn't set
function _bash_it_homebrew_check() {
if _binary_exists 'brew'; then
# Homebrew is installed
if [[ "${BASH_IT_HOMEBREW_PREFIX:-unset}" == 'unset' ]]; then
# variable isn't set
BASH_IT_HOMEBREW_PREFIX="$(brew --prefix)"
else
true # Variable is set already, don't invoke `brew`.
fi
else # Homebrew is not installed.
BASH_IT_HOMEBREW_PREFIX= # clear variable, if set to anything.
else
# Homebrew is not installed: clear variable.
BASH_IT_HOMEBREW_PREFIX=
false # return failure if brew not installed.
fi
}
@ -96,8 +96,7 @@ alias reload_completion="$(_make_reload_alias completion completion)"
# shellcheck disable=SC2139
alias reload_plugins="$(_make_reload_alias plugin plugins)"
bash-it ()
{
function bash-it() {
about 'Bash-it help and maintenance'
param '1: verb [one of: help | show | enable | disable | migrate | update | search | version | reload | restart | doctor ] '
param '2: component type [one of: alias(es) | completion(s) | plugin(s) ] or search term(s)'
@ -121,31 +120,43 @@ bash-it ()
case $verb in
show)
func="_bash-it-$component";;
func="_bash-it-$component"
;;
enable)
func="_enable-$component";;
func="_enable-$component"
;;
disable)
func="_disable-$component";;
func="_disable-$component"
;;
help)
func="_help-$component";;
func="_help-$component"
;;
doctor)
func="_bash-it-doctor-$component";;
func="_bash-it-doctor-$component"
;;
search)
_bash-it-search "$component" "$@"
return;;
return
;;
update)
func="_bash-it-update-$component";;
func="_bash-it-update-$component"
;;
migrate)
func="_bash-it-migrate";;
func="_bash-it-migrate"
;;
version)
func="_bash-it-version";;
func="_bash-it-version"
;;
restart)
func="_bash-it-restart";;
func="_bash-it-restart"
;;
reload)
func="_bash-it-reload";;
func="_bash-it-reload"
;;
*)
reference "bash-it"
return;;
return
;;
esac
# pluralize component if necessary
@ -163,13 +174,11 @@ bash-it ()
fi
fi
if [[ "$verb" == "enable" || "$verb" == "disable" ]]
then
if [[ "$verb" == "enable" || "$verb" == "disable" ]]; then
# Automatically run a migration if required
_bash-it-migrate
for arg in "$@"
do
for arg in "$@"; do
"$func" "$arg"
done
@ -197,50 +206,46 @@ function _is_function() {
fi
}
_bash-it-aliases ()
{
function _bash-it-aliases() {
_about 'summarizes available bash_it aliases'
_group 'lib'
_bash-it-describe "aliases" "an" "alias" "Alias"
}
_bash-it-completions ()
{
function _bash-it-completions() {
_about 'summarizes available bash_it completions'
_group 'lib'
_bash-it-describe "completion" "a" "completion" "Completion"
}
_bash-it-plugins ()
{
function _bash-it-plugins() {
_about 'summarizes available bash_it plugins'
_group 'lib'
_bash-it-describe "plugins" "a" "plugin" "Plugin"
}
_bash-it-update-dev() {
function _bash-it-update-dev() {
_about 'updates Bash-it to the latest master'
_group 'lib'
_bash-it-update- dev "$@"
}
_bash-it-update-stable() {
function _bash-it-update-stable() {
_about 'updates Bash-it to the latest tag'
_group 'lib'
_bash-it-update- stable "$@"
}
_bash-it_update_migrate_and_restart() {
function _bash-it_update_migrate_and_restart() {
_about 'Checks out the wanted version, pops directory and restart. Does not return (because of the restart!)'
_param '1: Which branch to checkout to'
_param '2: Which type of version we are using'
if git checkout "$1" &> /dev/null
then
if git checkout "$1" &> /dev/null; then
echo "Bash-it successfully updated."
echo ""
echo "Migrating your installation to the latest $2 version now..."
@ -256,7 +261,7 @@ _bash-it_update_migrate_and_restart() {
fi
}
_bash-it-update-() {
function _bash-it-update-() {
_about 'updates Bash-it'
_param '1: What kind of update to do (stable|dev)'
_group 'lib'
@ -268,7 +273,7 @@ _bash-it-update-() {
fi
done
pushd "${BASH_IT?}" >/dev/null || return
pushd "${BASH_IT?}" > /dev/null || return
DIFF=$(git diff --name-status)
if [[ -n "$DIFF" ]]; then
@ -338,7 +343,7 @@ _bash-it-update-() {
else
read -r -e -n 1 -p "Would you like to update to ${TARGET}($(git log -1 --format=%h "${TARGET}"))? [Y/n] " RESP
case "$RESP" in
[yY]|"")
[yY] | "")
_bash-it_update_migrate_and_restart "$TARGET" "$version"
;;
[nN])
@ -399,7 +404,7 @@ function _bash-it-migrate() {
fi
}
_bash-it-version() {
function _bash-it-version() {
_about 'shows current Bash-it version'
_group 'lib'
@ -437,7 +442,7 @@ _bash-it-version() {
cd - &> /dev/null || return
}
_bash-it-doctor() {
function _bash-it-doctor() {
_about 'reloads a profile file with a BASH_IT_LOG_LEVEL set'
_param '1: BASH_IT_LOG_LEVEL argument: "errors" "warnings" "all"'
_group 'lib'
@ -447,35 +452,35 @@ _bash-it-doctor() {
_bash-it-reload
}
_bash-it-doctor-all() {
function _bash-it-doctor-all() {
_about 'reloads a profile file with error, warning and debug logs'
_group 'lib'
_bash-it-doctor "${BASH_IT_LOG_LEVEL_ALL?}"
}
_bash-it-doctor-warnings() {
function _bash-it-doctor-warnings() {
_about 'reloads a profile file with error and warning logs'
_group 'lib'
_bash-it-doctor "${BASH_IT_LOG_LEVEL_WARNING?}"
}
_bash-it-doctor-errors() {
function _bash-it-doctor-errors() {
_about 'reloads a profile file with error logs'
_group 'lib'
_bash-it-doctor "${BASH_IT_LOG_LEVEL_ERROR?}"
}
_bash-it-doctor-() {
function _bash-it-doctor-() {
_about 'default bash-it doctor behavior, behaves like bash-it doctor all'
_group 'lib'
_bash-it-doctor-all
}
_bash-it-restart() {
function _bash-it-restart() {
_about 'restarts the shell in order to fully reload it'
_group 'lib'
@ -492,11 +497,11 @@ _bash-it-restart() {
exec "${0/-/}" --rcfile <(echo "source \"$HOME/$init_file\"; cd \"$saved_pwd\"")
}
_bash-it-reload() {
function _bash-it-reload() {
_about 'reloads a profile file'
_group 'lib'
pushd "${BASH_IT?}" >/dev/null || return
pushd "${BASH_IT?}" > /dev/null || return
case $OSTYPE in
darwin*)
@ -513,8 +518,7 @@ _bash-it-reload() {
popd
}
_bash-it-describe ()
{
function _bash-it-describe() {
_about 'summarizes available bash_it components'
_param '1: subdirectory'
_param '2: preposition'
@ -530,8 +534,7 @@ _bash-it-describe ()
local f
local enabled
printf "%-20s%-10s%s\n" "$column_header" 'Enabled?' 'Description'
for f in "${BASH_IT}/$subdirectory/available"/*.bash
do
for f in "${BASH_IT}/$subdirectory/available"/*.bash; do
# Check for both the old format without the load priority, and the extended format with the priority
declare enabled_files enabled_file
enabled_file="${f##*/}"
@ -550,8 +553,7 @@ _bash-it-describe ()
printf '%s\n' "$ bash-it disable $file_type <$file_type name> [$file_type name]... -or- $ bash-it disable $file_type all"
}
_on-disable-callback()
{
function _on-disable-callback() {
_about 'Calls the disabled plugin destructor, if present'
_param '1: plugin name'
_example '$ _on-disable-callback gitstatus'
@ -561,8 +563,7 @@ _on-disable-callback()
_command_exists "$callback" && "$callback"
}
_disable-plugin ()
{
function _disable-plugin() {
_about 'disables bash_it plugin'
_param '1: plugin name'
_example '$ disable-plugin rvm'
@ -572,8 +573,7 @@ _disable-plugin ()
_on-disable-callback "$1"
}
_disable-alias ()
{
function _disable-alias() {
_about 'disables bash_it alias'
_param '1: alias name'
_example '$ disable-alias git'
@ -582,8 +582,7 @@ _disable-alias ()
_disable-thing "aliases" "alias" "$1"
}
_disable-completion ()
{
function _disable-completion() {
_about 'disables bash_it completion'
_param '1: completion name'
_example '$ disable-completion git'
@ -623,13 +622,13 @@ function _disable-thing() {
rm -f "$_bash_it_config_file"
done
else
plugin_global="$(command ls $ "${BASH_IT}/enabled"/[0-9]*"${BASH_IT_LOAD_PRIORITY_SEPARATOR}${file_entity}.${suffix}.bash" 2>/dev/null | head -1)"
plugin_global="$(command ls $ "${BASH_IT}/enabled"/[0-9]*"${BASH_IT_LOAD_PRIORITY_SEPARATOR}${file_entity}.${suffix}.bash" 2> /dev/null | head -1)"
if [[ -z "$plugin_global" ]]; then
# Use a glob to search for both possible patterns
# 250---node.plugin.bash
# node.plugin.bash
# Either one will be matched by this glob
plugin="$(command ls $ "${BASH_IT}/$subdirectory/enabled/"{[0-9]*"${BASH_IT_LOAD_PRIORITY_SEPARATOR}${file_entity}.${suffix}.bash","${file_entity}.${suffix}.bash"} 2>/dev/null | head -1)"
plugin="$(command ls $ "${BASH_IT}/$subdirectory/enabled/"{[0-9]*"${BASH_IT_LOAD_PRIORITY_SEPARATOR}${file_entity}.${suffix}.bash","${file_entity}.${suffix}.bash"} 2> /dev/null | head -1)"
if [[ -z "$plugin" ]]; then
printf '%s\n' "sorry, $file_entity does not appear to be an enabled $file_type."
return
@ -645,8 +644,7 @@ function _disable-thing() {
printf '%s\n' "$file_entity disabled."
}
_enable-plugin ()
{
function _enable-plugin() {
_about 'enables bash_it plugin'
_param '1: plugin name'
_example '$ enable-plugin rvm'
@ -655,8 +653,7 @@ _enable-plugin ()
_enable-thing "plugins" "plugin" "$1" "$BASH_IT_LOAD_PRIORITY_DEFAULT_PLUGIN"
}
_enable-alias ()
{
function _enable-alias() {
_about 'enables bash_it alias'
_param '1: alias name'
_example '$ enable-alias git'
@ -665,8 +662,7 @@ _enable-alias ()
_enable-thing "aliases" "alias" "$1" "$BASH_IT_LOAD_PRIORITY_DEFAULT_ALIAS"
}
_enable-completion ()
{
function _enable-completion() {
_about 'enables bash_it completion'
_param '1: completion name'
_example '$ enable-completion git'
@ -706,7 +702,7 @@ function _enable-thing() {
_enable-thing "$subdirectory" "$file_type" "$to_enable" "$load_priority"
done
else
to_enable="$(command ls "${BASH_IT}/$subdirectory/available/$file_entity".*.bash 2>/dev/null | head -1)"
to_enable="$(command ls "${BASH_IT}/$subdirectory/available/$file_entity".*.bash 2> /dev/null | head -1)"
if [[ -z "$to_enable" ]]; then
printf '%s\n' "sorry, $file_entity does not appear to be an available $file_type."
return
@ -714,13 +710,13 @@ function _enable-thing() {
to_enable="${to_enable##*/}"
# Check for existence of the file using a wildcard, since we don't know which priority might have been used when enabling it.
enabled_plugin="$(command ls "${BASH_IT}/$subdirectory/enabled"/{[0-9][0-9][0-9]"${BASH_IT_LOAD_PRIORITY_SEPARATOR}${to_enable}","${to_enable}"} 2>/dev/null | head -1)"
enabled_plugin="$(command ls "${BASH_IT}/$subdirectory/enabled"/{[0-9][0-9][0-9]"${BASH_IT_LOAD_PRIORITY_SEPARATOR}${to_enable}","${to_enable}"} 2> /dev/null | head -1)"
if [[ -n "$enabled_plugin" ]]; then
printf '%s\n' "$file_entity is already enabled."
return
fi
enabled_plugin_global="$(command compgen -G "${BASH_IT}/enabled/[0-9][0-9][0-9]${BASH_IT_LOAD_PRIORITY_SEPARATOR}${to_enable}" 2>/dev/null | head -1)"
enabled_plugin_global="$(command compgen -G "${BASH_IT}/enabled/[0-9][0-9][0-9]${BASH_IT_LOAD_PRIORITY_SEPARATOR}${to_enable}" 2> /dev/null | head -1)"
if [[ -n "$enabled_plugin_global" ]]; then
printf '%s\n' "$file_entity is already enabled."
return
@ -740,16 +736,14 @@ function _enable-thing() {
printf '%s\n' "$file_entity enabled with priority $use_load_priority."
}
_help-completions()
{
function _help-completions() {
_about 'summarize all completions available in bash-it'
_group 'lib'
_bash-it-completions
}
_help-aliases()
{
function _help-aliases() {
_about 'shows help for all aliases, or a specific alias group'
_param '1: optional alias group'
_example '$ alias-help'
@ -815,14 +809,14 @@ function _help-plugins() {
rm "$grouplist" 2> /dev/null
}
_help-update () {
function _help-update() {
_about 'help message for update command'
_group 'lib'
echo "Check for a new version of Bash-it and update it."
}
_help-migrate () {
function _help-migrate() {
_about 'help message for migrate command'
_group 'lib'
@ -843,8 +837,8 @@ function all_groups() {
rm "$file"
}
if ! _command_exists pathmunge
then function pathmunge () {
if ! _command_exists pathmunge; then
function pathmunge() {
about 'prevent duplicate directories in you PATH variable'
group 'helpers'
example 'pathmunge /path/to/dir is equivalent to PATH=/path/to/dir:$PATH'
@ -857,7 +851,7 @@ then function pathmunge () {
export PATH=$1:$PATH
fi
fi
}
}
fi
# `_bash-it-find-in-ancestor` uses the shell's ability to run a function in