lib/helpers: second `shellcheck` pass

lib/helpers: lint `_bash-it-migrate()`

lib/helpers: lint `_disable-thing()`

lib/helpers: lint `_enable-thing()`

lib/helpers: lint `_help-list-aliases()`

lib/helpers: lint `_help-plugins()`

lib/helpers: some SC2034 fixes

And SC2154 in `_make_reload_alias()`

lib/helpers: lint `all_groups()`
pull/1934/head
John D Pell 2022-01-03 09:07:32 -08:00 committed by John D Pell
parent 5eab3bd288
commit 6ed006a167
1 changed files with 49 additions and 56 deletions

View File

@ -3,9 +3,9 @@
# #
# A collection of reusable functions. # A collection of reusable functions.
BASH_IT_LOAD_PRIORITY_DEFAULT_ALIAS=${BASH_IT_LOAD_PRIORITY_DEFAULT_ALIAS:-150} : "${BASH_IT_LOAD_PRIORITY_DEFAULT_ALIAS:=150}"
BASH_IT_LOAD_PRIORITY_DEFAULT_PLUGIN=${BASH_IT_LOAD_PRIORITY_DEFAULT_PLUGIN:-250} : "${BASH_IT_LOAD_PRIORITY_DEFAULT_PLUGIN:=250}"
BASH_IT_LOAD_PRIORITY_DEFAULT_COMPLETION=${BASH_IT_LOAD_PRIORITY_DEFAULT_COMPLETION:-350} : "${BASH_IT_LOAD_PRIORITY_DEFAULT_COMPLETION:=350}"
BASH_IT_LOAD_PRIORITY_SEPARATOR="---" BASH_IT_LOAD_PRIORITY_SEPARATOR="---"
# Handle the different ways of running `sed` without generating a backup file based on OS # Handle the different ways of running `sed` without generating a backup file based on OS
@ -13,9 +13,10 @@ BASH_IT_LOAD_PRIORITY_SEPARATOR="---"
# - BSD sed (macOS) uses `-i ''` # - BSD sed (macOS) uses `-i ''`
# To use this in Bash-it for inline replacements with `sed`, use the following syntax: # To use this in Bash-it for inline replacements with `sed`, use the following syntax:
# sed "${BASH_IT_SED_I_PARAMETERS[@]}" -e "..." file # sed "${BASH_IT_SED_I_PARAMETERS[@]}" -e "..." file
BASH_IT_SED_I_PARAMETERS=(-i) BASH_IT_SED_I_PARAMETERS=('-i')
# shellcheck disable=SC2034 # expected for this case
case "$OSTYPE" in case "$OSTYPE" in
'darwin'*) BASH_IT_SED_I_PARAMETERS=(-i "") 'darwin'*) BASH_IT_SED_I_PARAMETERS=('-i' '') ;;
esac esac
function _command_exists() { function _command_exists() {
@ -80,7 +81,7 @@ function _bash_it_homebrew_check()
} }
function _make_reload_alias() { function _make_reload_alias() {
echo "source '${BASH_IT}/scripts/reloader.bash' '${1}' '${2}'" echo "source '${BASH_IT?}/scripts/reloader.bash' '${1?}' '${2?}'"
} }
# Alias for reloading aliases # Alias for reloading aliases
@ -362,27 +363,25 @@ _bash-it-update-() {
popd &> /dev/null popd &> /dev/null
} }
_bash-it-migrate() { function _bash-it-migrate() {
_about 'migrates Bash-it configuration from a previous format to the current one' _about 'migrates Bash-it configuration from a previous format to the current one'
_group 'lib' _group 'lib'
declare migrated_something local migrated_something component_type component_name single_type _bash_it_config_file
migrated_something=false migrated_something=false
for file_type in "aliases" "plugins" "completion" for file_type in "aliases" "plugins" "completion"; do
do for _bash_it_config_file in "${BASH_IT}/$file_type/enabled"/*.bash; do
for _bash_it_config_file in "${BASH_IT}/$file_type/enabled"/*.bash
do
[[ -f "$_bash_it_config_file" ]] || continue [[ -f "$_bash_it_config_file" ]] || continue
# Get the type of component from the extension # Get the type of component from the extension
local component_type="$(_bash-it-get-component-type-from-path "$_bash_it_config_file")" component_type="$(_bash-it-get-component-type-from-path "$_bash_it_config_file")"
# Cut off the optional "250---" prefix and the suffix # Cut off the optional "250---" prefix and the suffix
local component_name="$(_bash-it-get-component-name-from-path "$_bash_it_config_file")" component_name="$(_bash-it-get-component-name-from-path "$_bash_it_config_file")"
migrated_something=true migrated_something=true
local single_type="${component_type/aliases/aliass}" single_type="${component_type/aliases/aliass}"
echo "Migrating ${single_type%s} $component_name." echo "Migrating ${single_type%s} $component_name."
disable_func="_disable-${single_type%s}" disable_func="_disable-${single_type%s}"
@ -391,7 +390,6 @@ _bash-it-migrate() {
"$disable_func" "$component_name" "$disable_func" "$component_name"
"$enable_func" "$component_name" "$enable_func" "$component_name"
done done
unset _bash_it_config_file
done done
if [[ -n "$BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE" ]]; then if [[ -n "$BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE" ]]; then
@ -447,30 +445,30 @@ _bash-it-doctor() {
_param '1: BASH_IT_LOG_LEVEL argument: "errors" "warnings" "all"' _param '1: BASH_IT_LOG_LEVEL argument: "errors" "warnings" "all"'
_group 'lib' _group 'lib'
BASH_IT_LOG_LEVEL=$1 # shellcheck disable=SC2034 # expected for this case
local BASH_IT_LOG_LEVEL="${1?}"
_bash-it-reload _bash-it-reload
unset BASH_IT_LOG_LEVEL
} }
_bash-it-doctor-all() { _bash-it-doctor-all() {
_about 'reloads a profile file with error, warning and debug logs' _about 'reloads a profile file with error, warning and debug logs'
_group 'lib' _group 'lib'
_bash-it-doctor "$BASH_IT_LOG_LEVEL_ALL" _bash-it-doctor "${BASH_IT_LOG_LEVEL_ALL?}"
} }
_bash-it-doctor-warnings() { _bash-it-doctor-warnings() {
_about 'reloads a profile file with error and warning logs' _about 'reloads a profile file with error and warning logs'
_group 'lib' _group 'lib'
_bash-it-doctor "$BASH_IT_LOG_LEVEL_WARNING" _bash-it-doctor "${BASH_IT_LOG_LEVEL_WARNING?}"
} }
_bash-it-doctor-errors() { _bash-it-doctor-errors() {
_about 'reloads a profile file with error logs' _about 'reloads a profile file with error logs'
_group 'lib' _group 'lib'
_bash-it-doctor "$BASH_IT_LOG_LEVEL_ERROR" _bash-it-doctor "${BASH_IT_LOG_LEVEL_ERROR?}"
} }
_bash-it-doctor-() { _bash-it-doctor-() {
@ -786,24 +784,23 @@ _disable-completion ()
_disable-thing "completion" "completion" "$1" _disable-thing "completion" "completion" "$1"
} }
_disable-thing () function _disable-thing() {
{
_about 'disables a bash_it component' _about 'disables a bash_it component'
_param '1: subdirectory' _param '1: subdirectory'
_param '2: file_type' _param '2: file_type'
_param '3: file_entity' _param '3: file_entity'
_example '$ _disable-thing "plugins" "plugin" "ssh"' _example '$ _disable-thing "plugins" "plugin" "ssh"'
subdirectory="$1" local subdirectory="$1"
file_type="$2" local file_type="$2"
file_entity="$3" local file_entity="$3"
if [[ -z "$file_entity" ]]; then if [[ -z "$file_entity" ]]; then
reference "disable-$file_type" reference "disable-$file_type"
return return
fi fi
local f suffix _bash_it_config_file local f suffix _bash_it_config_file plugin_global plugin
suffix="${subdirectory/plugins/plugin}" suffix="${subdirectory/plugins/plugin}"
if [[ "$file_entity" = "all" ]]; then if [[ "$file_entity" = "all" ]]; then
@ -818,13 +815,13 @@ _disable-thing ()
rm -f "$_bash_it_config_file" rm -f "$_bash_it_config_file"
done done
else else
local 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 if [[ -z "$plugin_global" ]]; then
# Use a glob to search for both possible patterns # Use a glob to search for both possible patterns
# 250---node.plugin.bash # 250---node.plugin.bash
# node.plugin.bash # node.plugin.bash
# Either one will be matched by this glob # Either one will be matched by this glob
local 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 if [[ -z "$plugin" ]]; then
printf '%s\n' "sorry, $file_entity does not appear to be an enabled $file_type." printf '%s\n' "sorry, $file_entity does not appear to be an enabled $file_type."
return return
@ -886,8 +883,7 @@ _enable-completion ()
_enable-thing "completion" "completion" "$1" "$BASH_IT_LOAD_PRIORITY_DEFAULT_COMPLETION" _enable-thing "completion" "completion" "$1" "$BASH_IT_LOAD_PRIORITY_DEFAULT_COMPLETION"
} }
_enable-thing () function _enable-thing() {
{
cite _about _param _example cite _about _param _example
_about 'enables a bash_it component' _about 'enables a bash_it component'
_param '1: subdirectory' _param '1: subdirectory'
@ -896,10 +892,12 @@ _enable-thing ()
_param '4: load priority' _param '4: load priority'
_example '$ _enable-thing "plugins" "plugin" "ssh" "150"' _example '$ _enable-thing "plugins" "plugin" "ssh" "150"'
subdirectory="$1" local subdirectory="$1"
file_type="$2" local file_type="$2"
file_entity="$3" local file_entity="$3"
load_priority="$4" local load_priority="$4"
local _bash_it_config_file to_enable enabled_plugin enabled_plugin_global local_file_priority use_load_priority
if [[ -z "$file_entity" ]]; then if [[ -z "$file_entity" ]]; then
reference "enable-$file_type" reference "enable-$file_type"
@ -907,7 +905,7 @@ _enable-thing ()
fi fi
if [[ "$file_entity" == "all" ]]; then if [[ "$file_entity" == "all" ]]; then
local _bash_it_config_file _bash_it_config_file
for _bash_it_config_file in "${BASH_IT}/$subdirectory/available"/*.bash; do for _bash_it_config_file in "${BASH_IT}/$subdirectory/available"/*.bash; do
to_enable="$(basename "$_bash_it_config_file" ".$file_type.bash")" to_enable="$(basename "$_bash_it_config_file" ".$file_type.bash")"
if [[ "$file_type" == "alias" ]]; then if [[ "$file_type" == "alias" ]]; then
@ -916,7 +914,7 @@ _enable-thing ()
_enable-thing "$subdirectory" "$file_type" "$to_enable" "$load_priority" _enable-thing "$subdirectory" "$file_type" "$to_enable" "$load_priority"
done done
else else
local 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 if [[ -z "$to_enable" ]]; then
printf '%s\n' "sorry, $file_entity does not appear to be an available $file_type." printf '%s\n' "sorry, $file_entity does not appear to be an available $file_type."
return return
@ -924,13 +922,13 @@ _enable-thing ()
to_enable="${to_enable##*/}" 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. # Check for existence of the file using a wildcard, since we don't know which priority might have been used when enabling it.
local 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 if [[ -n "$enabled_plugin" ]]; then
printf '%s\n' "$file_entity is already enabled." printf '%s\n' "$file_entity is already enabled."
return return
fi fi
local 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 if [[ -n "$enabled_plugin_global" ]]; then
printf '%s\n' "$file_entity is already enabled." printf '%s\n' "$file_entity is already enabled."
return return
@ -939,7 +937,6 @@ _enable-thing ()
mkdir -p "${BASH_IT}/enabled" mkdir -p "${BASH_IT}/enabled"
# Load the priority from the file if it present there # Load the priority from the file if it present there
declare local_file_priority use_load_priority
local_file_priority="$(_bash-it-egrep "^# BASH_IT_LOAD_PRIORITY:" "${BASH_IT}/$subdirectory/available/$to_enable" | awk -F': ' '{ print $2 }')" local_file_priority="$(_bash-it-egrep "^# BASH_IT_LOAD_PRIORITY:" "${BASH_IT}/$subdirectory/available/$to_enable" | awk -F': ' '{ print $2 }')"
use_load_priority="${local_file_priority:-$load_priority}" use_load_priority="${local_file_priority:-$load_priority}"
@ -990,36 +987,33 @@ _help-aliases()
fi fi
} }
_help-list-aliases () function _help-list-aliases() {
{ local file
local file="$(basename "$1" | sed -e 's/[0-9]*[-]*\(.*\)\.aliases\.bash/\1/g')" file="$(_bash-it-get-component-name-from-path "${1?}")"
printf '\n\n%s:\n' "${file}" printf '\n\n%s:\n' "${file}"
# metafor() strips trailing quotes, restore them with sed.. # metafor() strips trailing quotes, restore them with sed..
metafor alias < "$1" | sed "s/$/'/" metafor alias < "$1" | sed "s/$/'/"
} }
_help-plugins() function _help-plugins() {
{
_about 'summarize all functions defined by enabled bash-it plugins' _about 'summarize all functions defined by enabled bash-it plugins'
_group 'lib' _group 'lib'
local grouplist func group about gfile
# display a brief progress message... # display a brief progress message...
printf '%s' 'please wait, building help...' printf '%s' 'please wait, building help...'
local grouplist=$(mktemp -t grouplist.XXXXXX) grouplist="$(mktemp -t grouplist.XXXXXX)"
local func
for func in $(_typeset_functions); do for func in $(_typeset_functions); do
local group="$(declare -f "$func" | metafor group)" group="$(declare -f "$func" | metafor group)"
if [[ -z "$group" ]]; then if [[ -z "$group" ]]; then
group='misc' group='misc'
fi fi
local about="$(declare -f "$func" | metafor about)" about="$(declare -f "$func" | metafor about)"
_letterpress "$about" "$func" >> "$grouplist.$group" _letterpress "$about" "$func" >> "$grouplist.$group"
echo "$grouplist.$group" >> "$grouplist" echo "$grouplist.$group" >> "$grouplist"
done done
# clear progress message # clear progress message
printf '\r%s\n' ' ' printf '\r%s\n' ' '
local group
local gfile
while IFS= read -r gfile; do while IFS= read -r gfile; do
printf '%s\n' "${gfile##*.}:" printf '%s\n' "${gfile##*.}:"
cat "$gfile" cat "$gfile"
@ -1055,8 +1049,7 @@ _help-migrate () {
echo "The 'migrate' command is run automatically when calling 'update', 'enable' or 'disable'." echo "The 'migrate' command is run automatically when calling 'update', 'enable' or 'disable'."
} }
all_groups () function all_groups() {
{
about 'displays all unique metadata groups' about 'displays all unique metadata groups'
group 'lib' group 'lib'