Skip to content

Commit

Permalink
lib/helpers: fix globbing with compgen
Browse files Browse the repository at this point in the history
Rewrite globbing per `shellcheck`’s SC2013, and alsö s/typeset/local/g
  • Loading branch information
gaelicWizard committed Sep 6, 2021
1 parent fa1a57b commit a53776f
Showing 1 changed file with 33 additions and 43 deletions.
76 changes: 33 additions & 43 deletions lib/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ bash-it ()
example '$ bash-it reload'
example '$ bash-it restart'
example '$ bash-it doctor errors|warnings|all'
typeset verb=${1:-}
local verb=${1:-}
shift
typeset component=${1:-}
local component=${1:-}
shift
typeset func
local func

case $verb in
show)
Expand Down Expand Up @@ -342,18 +342,14 @@ _bash-it-migrate() {

for file_type in "aliases" "plugins" "completion"
do
OLDIFS="$IFS"; IFS=$'\n'
_bash_it_config_files=( $(sort <(compgen -G "${BASH_IT}/$file_type/enabled/*.bash")) )
IFS="$OLDIFS"; unset OLDIFS

for f in "${_bash_it_config_files[@]}"
compgen -G "${BASH_IT}/$file_type/enabled/*.bash" | sort | while IFS= read -r _bash_it_config_file
do
typeset ff="${f##*/}"
local ff="${_bash_it_config_file##*/}"

# Get the type of component from the extension
typeset single_type="$(echo "$ff" | sed -e 's/.*\.\(.*\)\.bash/\1/g' | sed 's/aliases/alias/g')"
local single_type="$(echo "$ff" | sed -e 's/.*\.\(.*\)\.bash/\1/g' | sed 's/aliases/alias/g')"
# Cut off the optional "250---" prefix and the suffix
typeset component_name="$(echo "$ff" | sed -e 's/[0-9]*[-]*\(.*\)\..*\.bash/\1/g')"
local component_name="$(echo "$ff" | sed -e 's/[0-9]*[-]*\(.*\)\..*\.bash/\1/g')"

migrated_something=true

Expand All @@ -365,7 +361,7 @@ _bash-it-migrate() {
"$disable_func" "$component_name"
"$enable_func" "$component_name"
done
unset _bash_it_config_files
unset _bash_it_config_file
done

if [ -n "$BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE" ]; then
Expand Down Expand Up @@ -506,8 +502,8 @@ _bash-it-describe ()
file_type="$3"
column_header="$4"

typeset f
typeset enabled
local f
local enabled
printf "%-20s%-10s%s\n" "$column_header" 'Enabled?' 'Description'
for f in "${BASH_IT}/$subdirectory/available"/*.bash
do
Expand Down Expand Up @@ -588,37 +584,31 @@ _disable-thing ()
return
fi

typeset f suffix _bash_it_config_files
local f suffix _bash_it_config_files
suffix="${subdirectory/plugins/plugin}"

if [ "$file_entity" = "all" ]; then
# Disable everything that's using the old structure

OLDIFS="$IFS"; IFS=$'\n'
_bash_it_config_files=( $(compgen -G "${BASH_IT}/$subdirectory/enabled/*.${suffix}.bash") )
IFS="$OLDIFS"; unset OLDIFS
for f in "${_bash_it_config_files[@]}"
compgen -G "${BASH_IT}/$subdirectory/enabled/*.${suffix}.bash" | sort | while IFS= read -r _bash_it_config_file
do
rm "$f"
rm "$_bash_it_config_file"
done

OLDIFS="$IFS"; IFS=$'\n'
_bash_it_config_files=( $(compgen -G "${BASH_IT}/enabled/*.${suffix}.bash") )
IFS="$OLDIFS"; unset OLDIFS
compgen -G "${BASH_IT}/enabled/*.${suffix}.bash" | sort | while IFS= read -r _bash_it_config_file
# Disable everything in the global "enabled" directory
for f in "${_bash_it_config_files[@]}"
do
rm "$f"
rm "$_bash_it_config_file"
done
unset _bash_it_config_files
unset _bash_it_config_file
else
typeset plugin_global="$(command ls $ "${BASH_IT}/enabled"/[0-9]*"${BASH_IT_LOAD_PRIORITY_SEPARATOR}${file_entity}.${suffix}.bash" 2>/dev/null | head -1)"
local 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
typeset 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)"
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)"
if [ -z "$plugin" ]; then
printf '%s\n' "sorry, $file_entity does not appear to be an enabled $file_type."
return
Expand Down Expand Up @@ -685,7 +675,7 @@ _enable-thing ()
fi

if [ "$file_entity" = "all" ]; then
typeset f file_type
local f file_type
for f in "${BASH_IT}/$subdirectory/available"/*.bash
do
to_enable="$(basename "$f" ".$file_type.bash")"
Expand All @@ -695,21 +685,21 @@ _enable-thing ()
_enable-thing "$subdirectory" "$file_type" "$to_enable" "$load_priority"
done
else
typeset to_enable="$(command ls "${BASH_IT}/$subdirectory/available/$file_entity".*.bash 2>/dev/null | head -1)"
local 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
fi

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.
typeset 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)"
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)"
if [ -n "$enabled_plugin" ] ; then
printf '%s\n' "$file_entity is already enabled."
return
fi

typeset 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)"
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)"
if [ -n "$enabled_plugin_global" ] ; then
printf '%s\n' "$file_entity is already enabled."
return
Expand Down Expand Up @@ -756,7 +746,7 @@ _help-aliases()
esac
metafor alias < "${BASH_IT}/aliases/$alias_path" | sed "s/$/'/"
else
typeset f
local f

for f in $(sort <(compgen -G "${BASH_IT}/aliases/enabled/*") <(compgen -G "${BASH_IT}/enabled/*.aliases.bash"))
do
Expand All @@ -771,7 +761,7 @@ _help-aliases()

_help-list-aliases ()
{
typeset file="$(basename "$1" | sed -e 's/[0-9]*[-]*\(.*\)\.aliases\.bash/\1/g')"
local file="$(basename "$1" | sed -e 's/[0-9]*[-]*\(.*\)\.aliases\.bash/\1/g')"
printf '\n\n%s:\n' "${file}"
# metafor() strips trailing quotes, restore them with sed..
metafor alias < "$1" | sed "s/$/'/"
Expand All @@ -784,22 +774,22 @@ _help-plugins()

# display a brief progress message...
printf '%s' 'please wait, building help...'
typeset grouplist=$(mktemp -t grouplist.XXXXXX)
typeset func
local grouplist=$(mktemp -t grouplist.XXXXXX)
local func
for func in $(_typeset_functions)
do
typeset group="$(typeset -f "$func" | metafor group)"
local group="$(local -f "$func" | metafor group)"
if [ -z "$group" ]; then
group='misc'
fi
typeset about="$(typeset -f "$func" | metafor about)"
local about="$(local -f "$func" | metafor about)"
_letterpress "$about" "$func" >> "$grouplist.$group"
echo "$grouplist.$group" >> "$grouplist"
done
# clear progress message
printf '\r%s\n' ' '
typeset group
typeset gfile
local group
local gfile
for gfile in $(sort -u "$grouplist")
do
printf '%s\n' "${gfile##*.}:"
Expand Down Expand Up @@ -830,11 +820,11 @@ all_groups ()
about 'displays all unique metadata groups'
group 'lib'

typeset func
typeset file=$(mktemp -t composure.XXXX)
local func
local file=$(mktemp -t composure.XXXX)
for func in $(_typeset_functions)
do
typeset -f "$func" | metafor group >> "$file"
local -f "$func" | metafor group >> "$file"
done
sort -u "$file"
rm "$file"
Expand Down

0 comments on commit a53776f

Please sign in to comment.