lib/helpers: remove weird non-globs

Replace weird non-globs with array and loop, as suggested by `shellcheck`. Alsö, simplify several constructs to eliminate external binaries.

Alsö, see mvdan/sh issue 558

lib/helpers: unbound positional parameters
pull/1934/head
John D Pell 2022-01-03 16:58:48 -08:00 committed by John D Pell
parent 251e23a3fa
commit 4719e43d0b
1 changed files with 50 additions and 61 deletions

View File

@ -557,6 +557,7 @@ _bash-it-profile-load-parse-profile() {
_example '$ _bash-it-profile-load-parse-profile "profile.bash_it" "dry"' _example '$ _bash-it-profile-load-parse-profile "profile.bash_it" "dry"'
local -i num=0 local -i num=0
local line
while read -r -a line; do while read -r -a line; do
((++num)) ((++num))
# Ignore comments and empty lines # Ignore comments and empty lines
@ -565,11 +566,10 @@ _bash-it-profile-load-parse-profile() {
local subdirectory=${line[0]} local subdirectory=${line[0]}
local component=${line[1]} local component=${line[1]}
local to_enable local to_enable=("${BASH_IT}/$subdirectory/available/$component.${subdirectory%s}"*.bash)
to_enable=$(command ls "${BASH_IT}/$subdirectory/available/$component".*bash 2> /dev/null | head -1)
# Ignore botched lines # Ignore botched lines
if [[ -z "${to_enable}" ]]; then if [[ ! -e "${to_enable[0]}" ]]; then
echo -e "${echo_orange?}Bad line(#$num) in profile, aborting load...${echo_reset_color?}" echo -e "${echo_orange?}Bad line(#$num) in profile, aborting load...${line[*]}${echo_reset_color?}"
local bad="bad line" local bad="bad line"
break break
fi fi
@ -613,12 +613,12 @@ _bash-it-profile-rm() {
local profile_path="${BASH_IT}/profiles/$name.bash_it" local profile_path="${BASH_IT}/profiles/$name.bash_it"
if [[ ! -f "$profile_path" ]]; then if [[ ! -f "$profile_path" ]]; then
echo -e "${echo_orange?}Could not find profile \"$name\"...${echo_reset_color?}" echo -e "${echo_orange?}Could not find profile '$name'...${echo_reset_color?}"
return 1 return 1
fi fi
command rm "$profile_path" command rm "$profile_path"
echo "Removed profile \"$name\" successfully!" echo "Removed profile '$name' successfully!"
} }
_bash-it-profile-load() { _bash-it-profile-load() {
@ -633,20 +633,22 @@ _bash-it-profile-load() {
local profile_path="${BASH_IT}/profiles/$name.bash_it" local profile_path="${BASH_IT}/profiles/$name.bash_it"
if [[ ! -f "$profile_path" ]]; then if [[ ! -f "$profile_path" ]]; then
echo -e "${echo_orange?}Could not find profile \"$name\", not changing configuration...${echo_reset_color?}" echo -e "${echo_orange?}Could not find profile '$name', not changing configuration...${echo_reset_color?}"
return 1 return 1
fi fi
echo "Trying to parse profile \"$name\"..." echo "Trying to parse profile '$name'..."
if _bash-it-profile-load-parse-profile "$profile_path" "dry"; then if _bash-it-profile-load-parse-profile "$profile_path" "dry"; then
echo "Profile \"$name\" parsed successfully!" echo "Profile '$name' parsed successfully!"
echo "Disabling current configuration..." echo "Disabling current configuration..."
_disable-all _disable-all
echo "" echo ""
echo "Enabling configuration based on profile..." echo "Enabling configuration based on profile..."
_bash-it-profile-load-parse-profile "$profile_path" _bash-it-profile-load-parse-profile "$profile_path"
echo "" echo ""
echo "Profile \"$name\" enabled!" echo "Profile '$name' enabled!"
else
false # failure
fi fi
} }
@ -776,45 +778,39 @@ function _disable-thing() {
_param '3: file_entity' _param '3: file_entity'
_example '$ _disable-thing "plugins" "plugin" "ssh"' _example '$ _disable-thing "plugins" "plugin" "ssh"'
local subdirectory="$1" local subdirectory="${1?}"
local file_type="$2" local file_type="${2?}"
local 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 plugin_global plugin local f suffix _bash_it_config_file plugin
suffix="${subdirectory/plugins/plugin}" suffix="${subdirectory/plugins/plugin}"
if [[ "$file_entity" = "all" ]]; then if [[ "$file_entity" == "all" ]]; then
# Disable everything that's using the old structure # Disable everything that's using the old structure and everything in the global "enabled" directory.
for _bash_it_config_file in "${BASH_IT}/$subdirectory/enabled"/*."${suffix}.bash" "${BASH_IT}/enabled"/*".${suffix}.bash"; do
for _bash_it_config_file in "${BASH_IT}/$subdirectory/enabled"/*."${suffix}.bash"; do
rm -f "$_bash_it_config_file"
done
for _bash_it_config_file in "${BASH_IT}/enabled"/*".${suffix}.bash"; do
# Disable everything in the global "enabled" directory
rm -f "$_bash_it_config_file" rm -f "$_bash_it_config_file"
done done
else else
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 # 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
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)" for plugin in "${BASH_IT}/enabled"/[[:digit:]][[:digit:]][[:digit:]]"${BASH_IT_LOAD_PRIORITY_SEPARATOR}${file_entity}.${suffix}.bash" "${BASH_IT}/$subdirectory/enabled/"{[[:digit:]][[:digit:]][[:digit:]]"${BASH_IT_LOAD_PRIORITY_SEPARATOR}${file_entity}.${suffix}.bash","${file_entity}.${suffix}.bash"}; do
if [[ -z "$plugin" ]]; then if [[ -e "${plugin}" ]]; then
rm "${plugin}"
plugin=
break
fi
done
if [[ -n "${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
fi fi
rm "${BASH_IT}/$subdirectory/enabled/${plugin##*/}"
else
rm "${BASH_IT}/enabled/${plugin_global##*/}"
fi
fi fi
_bash-it-clean-component-cache "${file_type}" _bash-it-clean-component-cache "${file_type}"
@ -872,46 +868,39 @@ function _enable-thing() {
_param '4: load priority' _param '4: load priority'
_example '$ _enable-thing "plugins" "plugin" "ssh" "150"' _example '$ _enable-thing "plugins" "plugin" "ssh" "150"'
local subdirectory="$1" local subdirectory="${1?}"
local file_type="$2" local file_type="${2?}"
local file_entity="$3" local file_entity="${3:-}"
local load_priority="$4" local load_priority="${4:-500}"
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"
return return
fi fi
local _bash_it_config_file to_enable to_enables enabled_plugin local_file_priority use_load_priority
local suffix="${subdirectory/plugins/plugin}"
if [[ "$file_entity" == "all" ]]; then if [[ "$file_entity" == "all" ]]; then
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="${_bash_it_config_file##*/}"
if [[ "$file_type" == "alias" ]]; then _enable-thing "$subdirectory" "$file_type" "${to_enable%."${file_type/alias/aliases}".bash}" "$load_priority"
to_enable="$(basename "$_bash_it_config_file" ".aliases.bash")"
fi
_enable-thing "$subdirectory" "$file_type" "$to_enable" "$load_priority"
done done
else else
to_enable="$(command ls "${BASH_IT}/$subdirectory/available/$file_entity".*.bash 2> /dev/null | head -1)" to_enables=("${BASH_IT}/$subdirectory/available/$file_entity.${suffix}.bash")
if [[ -z "$to_enable" ]]; then if [[ ! -e "${to_enables[0]}" ]]; 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
fi fi
to_enable="${to_enable##*/}" to_enable="${to_enables[0]##*/}"
# 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.
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)" for enabled_plugin in "${BASH_IT}/$subdirectory/enabled"/{[[:digit:]][[:digit:]][[:digit:]]"${BASH_IT_LOAD_PRIORITY_SEPARATOR}${to_enable}","${to_enable}"} "${BASH_IT}/enabled"/[[:digit:]][[:digit:]][[:digit:]]"${BASH_IT_LOAD_PRIORITY_SEPARATOR?}${to_enable}"; do
if [[ -n "$enabled_plugin" ]]; then if [[ -e "${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)"
if [[ -n "$enabled_plugin_global" ]]; then
printf '%s\n' "$file_entity is already enabled." printf '%s\n' "$file_entity is already enabled."
return return
fi fi
done
mkdir -p "${BASH_IT}/enabled" mkdir -p "${BASH_IT}/enabled"