lib/helpers: `shfmt`

My apologies to future `git blame` hunters ♥
pull/1934/head
John D Pell 2022-01-18 11:02:36 -08:00
parent 6ed006a167
commit 003b0ce802
2 changed files with 777 additions and 785 deletions

View File

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

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)'
@ -122,33 +121,46 @@ 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"
;;
profile)
func=_bash-it-profile-$component;;
func=_bash-it-profile-$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
@ -166,13 +178,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
@ -200,50 +210,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..."
@ -259,7 +265,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'
@ -271,7 +277,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
@ -341,7 +347,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])
@ -402,7 +408,7 @@ function _bash-it-migrate() {
fi
}
_bash-it-version() {
function _bash-it-version() {
_about 'shows current Bash-it version'
_group 'lib'
@ -440,7 +446,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'
@ -450,35 +456,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-profile-save() {
function _bash-it-profile-save() {
_about 'saves the current configuration to the "profile" directory'
_group 'lib'
@ -545,9 +551,9 @@ _bash-it-profile-save() {
echo ""
echo "Profile location: $profile_path"
echo "Load the profile by invoking \"bash-it profile load $name\""
}
}
_bash-it-profile-load-parse-profile() {
_bash-it-profile-load-parse-profile() {
_about 'Internal function used to parse the profile file'
_param '1: path to the profile file'
_param '2: dry run- only check integrity of the profile file'
@ -562,7 +568,7 @@ _bash-it-profile-save() {
local subdirectory=${line[0]}
local component=${line[1]}
local to_enable=$(command ls "${BASH_IT}/$subdirectory/available/$component".*bash 2>/dev/null | head -1)
local to_enable=$(command ls "${BASH_IT}/$subdirectory/available/$component".*bash 2> /dev/null | head -1)
# Ignore botched lines
if [[ -z "${to_enable}" ]]; then
echo -e "${echo_orange?}Bad line(#$num) in profile, aborting load...${echo_reset_color?}"
@ -577,9 +583,9 @@ _bash-it-profile-save() {
# Make sure to propagate the error
[[ -z $bad ]]
}
}
_bash-it-profile-list() {
_bash-it-profile-list() {
about 'lists all profiles from the "profiles" directory'
_group 'lib'
local profile
@ -589,9 +595,9 @@ _bash-it-profile-save() {
profile="${profile##*/}"
echo "${profile/.bash_it/}"
done
}
}
_bash-it-profile-rm() {
_bash-it-profile-rm() {
about 'Removes a profile from the "profiles" directory'
_group 'lib'
@ -615,9 +621,9 @@ _bash-it-profile-save() {
command rm "$profile_path"
echo "Removed profile \"$name\" successfully!"
}
}
_bash-it-profile-load() {
_bash-it-profile-load() {
_about 'loads a configuration from the "profiles" directory'
_group 'lib'
@ -644,9 +650,9 @@ _bash-it-profile-save() {
echo ""
echo "Profile \"$name\" enabled!"
fi
}
}
_bash-it-restart() {
function _bash-it-restart() {
_about 'restarts the shell in order to fully reload it'
_group 'lib'
@ -663,11 +669,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*)
@ -684,8 +690,7 @@ _bash-it-reload() {
popd
}
_bash-it-determine-component-status-from-path ()
{
_bash-it-determine-component-status-from-path() {
_about 'internal function used to process component name and check if its enabled'
_param '1: full path to available component file'
_example '$ _bash-it-determine-component-status-from-path "${BASH_IT}/plugins/available/git.plugin.bash'
@ -701,10 +706,9 @@ _bash-it-determine-component-status-from-path ()
else
enabled=' '
fi
}
}
_bash-it-describe ()
{
function _bash-it-describe() {
_about 'summarizes available bash_it components'
_param '1: subdirectory'
_param '2: preposition'
@ -720,8 +724,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
_bash-it-determine-component-status-from-path "$f"
printf "%-20s%-10s%s\n" "$enabled_file_clean" " [$enabled]" "$(metafor "about-$file_type" < "$f")"
done
@ -731,8 +734,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'
@ -742,8 +744,7 @@ _on-disable-callback()
_command_exists "$callback" && "$callback"
}
_disable-all ()
{
function _disable-all() {
_about 'disables all bash_it components'
_example '$ _disable-all'
_group 'lib'
@ -751,10 +752,9 @@ _disable-all ()
_disable-plugin "all"
_disable-alias "all"
_disable-completion "all"
}
}
_disable-plugin ()
{
function _disable-plugin() {
_about 'disables bash_it plugin'
_param '1: plugin name'
_example '$ disable-plugin rvm'
@ -764,8 +764,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'
@ -774,8 +773,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'
@ -815,13 +813,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
@ -841,8 +839,7 @@ function _disable-thing() {
fi
}
_enable-plugin ()
{
function _enable-plugin() {
_about 'enables bash_it plugin'
_param '1: plugin name'
_example '$ enable-plugin rvm'
@ -851,14 +848,12 @@ _enable-plugin ()
_enable-thing "plugins" "plugin" "$1" "$BASH_IT_LOAD_PRIORITY_DEFAULT_PLUGIN"
}
_enable-plugins ()
{
function _enable-plugins() {
_about 'alias of _enable-plugin'
_enable-plugin "$@"
}
}
_enable-alias ()
{
function _enable-alias() {
_about 'enables bash_it alias'
_param '1: alias name'
_example '$ enable-alias git'
@ -867,14 +862,12 @@ _enable-alias ()
_enable-thing "aliases" "alias" "$1" "$BASH_IT_LOAD_PRIORITY_DEFAULT_ALIAS"
}
_enable-aliases ()
{
function _enable-aliases() {
_about 'alias of _enable-alias'
_enable-alias "$@"
}
}
_enable-completion ()
{
function _enable-completion() {
_about 'enables bash_it completion'
_param '1: completion name'
_example '$ enable-completion git'
@ -914,7 +907,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
@ -922,13 +915,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
@ -948,16 +941,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'
@ -1023,7 +1014,7 @@ function _help-plugins() {
rm "$grouplist" 2> /dev/null
}
_help-profile () {
_help-profile() {
_about 'help message for profile command'
_group 'lib'
@ -1032,16 +1023,16 @@ _help-profile () {
echo "Use 'bash-it profile save foo' to save the current configuration into a profile named 'foo'."
echo "Use 'bash-it profile load foo' to load an existing profile named 'foo'."
echo "Use 'bash-it profile rm foo' to remove an existing profile named 'foo'."
}
}
_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'
@ -1056,8 +1047,8 @@ function all_groups() {
declare -f | metafor group | sort -u
}
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'
@ -1070,7 +1061,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