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'
@ -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'
@ -646,7 +652,7 @@ _bash-it-profile-save() {
fi
}
_bash-it-restart() {
function _bash-it-restart() {
_about 'restarts the shell in order to fully reload it'
_group 'lib'
@ -663,7 +669,7 @@ _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'
@ -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'
@ -703,8 +708,7 @@ _bash-it-determine-component-status-from-path ()
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'
@ -753,8 +754,7 @@ _disable-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'
@ -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'
@ -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'
@ -1034,14 +1025,14 @@ _help-profile () {
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'