Skip to content

Commit

Permalink
lib/helpers: lint
Browse files Browse the repository at this point in the history
Quote things, appease SC2268, SC2143, SC2181, SC2162, &c
  • Loading branch information
gaelicWizard committed Aug 15, 2021
1 parent be8366a commit 23ffbe6
Showing 1 changed file with 99 additions and 86 deletions.
185 changes: 99 additions & 86 deletions lib/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -102,40 +102,40 @@ 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";;
search)
_bash-it-search $component "$@"
_bash-it-search "$component" "$@"
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
reference "bash-it"
return;;
esac

# pluralize component if necessary
if ! _is_function $func; then
if _is_function ${func}s; then
func=${func}s
if ! _is_function "$func"; then
if _is_function "${func}s"; then
func="${func}s"
else
if _is_function ${func}es; then
func=${func}es
if _is_function "${func}es"; then
func="${func}es"
else
echo "oops! $component is not a valid option!"
reference bash-it
Expand All @@ -144,7 +144,8 @@ bash-it ()
fi
fi

if [ x"$verb" == x"enable" ] || [ x"$verb" == x"disable" ]; then
if [[ "$verb" == "enable" || "$verb" == "disable" ]]
then
# Automatically run a migration if required
_bash-it-migrate

Expand All @@ -157,7 +158,7 @@ bash-it ()
_bash-it-reload
fi
else
$func "$@"
"$func" "$@"
fi
}

Expand All @@ -166,7 +167,7 @@ _is_function ()
_about 'sets $? to true if parameter is the name of a function'
_param '1: name of alleged function'
_group 'lib'
[ -n "$(LANG=C type -t $1 2>/dev/null | grep 'function')" ]
LANG=C type -t "$1" | grep -q 'function'
}

_bash-it-aliases ()
Expand Down Expand Up @@ -208,8 +209,8 @@ _bash-it-update-stable() {
}

_bash-it_pull_and_update_inner() {
git checkout "$1" &> /dev/null
if [[ $? -eq 0 ]]; then
if git checkout "$1" &> /dev/null
then
echo "Bash-it successfully updated."
echo ""
echo "Migrating your installation to the latest $2 version now..."
Expand All @@ -228,8 +229,8 @@ _bash-it-update-() {
_group 'lib'

declare silent
for word in $@; do
if [[ ${word} == "--silent" || ${word} == "-s" ]]; then
for word in "$@"; do
if [[ "${word}" == "--silent" || "${word}" == "-s" ]]; then
silent=true
fi
done
Expand All @@ -244,7 +245,7 @@ _bash-it-update-() {
BASH_IT_REMOTE="origin"
fi

git fetch $BASH_IT_REMOTE --tags &> /dev/null
git fetch "$BASH_IT_REMOTE" --tags &> /dev/null

if [ -z "$BASH_IT_DEVELOPMENT_BRANCH" ]; then
BASH_IT_DEVELOPMENT_BRANCH="master"
Expand All @@ -260,48 +261,48 @@ _bash-it-update-() {
fi
else
version="dev"
TARGET=${BASH_IT_REMOTE}/${BASH_IT_DEVELOPMENT_BRANCH}
TARGET="${BASH_IT_REMOTE}/${BASH_IT_DEVELOPMENT_BRANCH}"
fi

declare revision
revision="HEAD..${TARGET}"
declare status
status="$(git rev-list ${revision} 2> /dev/null)"
status="$(git rev-list "${revision}" 2> /dev/null)"
declare revert

if [[ -z "${status}" && ${version} == "stable" ]]; then
if [[ -z "${status}" && "${version}" == "stable" ]]; then
revision="${TARGET}..HEAD"
status="$(git rev-list ${revision} 2> /dev/null)"
status="$(git rev-list "${revision}" 2> /dev/null)"
revert=true
fi

if [[ -n "${status}" ]]; then
if [[ $revert ]]; then
if [[ "$revert" ]]; then
echo "Your version is a more recent development version ($(git log -1 --format=%h HEAD))"
echo "You can continue in order to revert and update to the latest stable version"
echo ""
log_color="%Cred"
fi

for i in $(git rev-list --merges --first-parent ${revision}); do
num_of_lines=$(git log -1 --format=%B $i | awk 'NF' | wc -l)
if [ $num_of_lines -eq 1 ]; then
for i in $(git rev-list --merges --first-parent "${revision}"); do
num_of_lines=$(git log -1 --format=%B "$i" | awk 'NF' | wc -l)
if [ "$num_of_lines" -eq 1 ]; then
description="%s"
else
description="%b"
fi
git log --format="${log_color}%h: $description (%an)" -1 $i
git log --format="${log_color}%h: $description (%an)" -1 "$i"
done
echo ""

if [[ $silent ]]; then
if [[ "$silent" ]]; then
echo "Updating to ${TARGET}($(git log -1 --format=%h "${TARGET}"))..."
_bash-it_pull_and_update_inner $TARGET $version
_bash-it_pull_and_update_inner "$TARGET" "$version"
else
read -e -n 1 -p "Would you like to update to ${TARGET}($(git log -1 --format=%h "${TARGET}"))? [Y/n] " RESP
case $RESP in
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]|"")
_bash-it_pull_and_update_inner $TARGET $version
_bash-it_pull_and_update_inner "$TARGET" "$version"
;;
[nN])
echo "Not updating…"
Expand All @@ -312,7 +313,7 @@ _bash-it-update-() {
esac
fi
else
if [[ ${version} == "stable" ]]; then
if [[ "${version}" == "stable" ]]; then
echo "You're on the latest stable version. If you want to check out the latest 'dev' version, please run \"bash-it update dev\""
else
echo "Bash-it is up to date, nothing to do!"
Expand All @@ -330,14 +331,18 @@ _bash-it-migrate() {

for file_type in "aliases" "plugins" "completion"
do
for f in `sort <(compgen -G "${BASH_IT}/$file_type/enabled/*.bash")`
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[@]}"
do
typeset ff="${f##*/}"

# Get the type of component from the extension
typeset single_type=$(echo $ff | sed -e 's/.*\.\(.*\)\.bash/\1/g' | sed 's/aliases/alias/g')
typeset 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')
typeset component_name="$(echo $ff | sed -e 's/[0-9]*[-]*\(.*\)\..*\.bash/\1/g')"

migrated_something=true

Expand All @@ -346,9 +351,10 @@ _bash-it-migrate() {
disable_func="_disable-$single_type"
enable_func="_enable-$single_type"

$disable_func $component_name
$enable_func $component_name
$disable_func "$component_name"
$enable_func "$component_name"
done
unset _bash_it_config_files
done

if [ -n "$BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE" ]; then
Expand All @@ -367,28 +373,28 @@ _bash-it-version() {

cd "${BASH_IT}" || return

if [ -z $BASH_IT_REMOTE ]; then
if [ -z "${BASH_IT_REMOTE:-}" ]; then
BASH_IT_REMOTE="origin"
fi

BASH_IT_GIT_REMOTE=$(git remote get-url $BASH_IT_REMOTE)
BASH_IT_GIT_URL=${BASH_IT_GIT_REMOTE%.git}
BASH_IT_GIT_REMOTE="$(git remote get-url "$BASH_IT_REMOTE")"
BASH_IT_GIT_URL="${BASH_IT_GIT_REMOTE%.git}"
if [[ "$BASH_IT_GIT_URL" == *"git@"* ]]; then
# Fix URL in case it is ssh based URL
BASH_IT_GIT_URL=${BASH_IT_GIT_URL/://}
BASH_IT_GIT_URL=${BASH_IT_GIT_URL/git@/https://}
BASH_IT_GIT_URL="${BASH_IT_GIT_URL/://}"
BASH_IT_GIT_URL="${BASH_IT_GIT_URL/git@/https://}"
fi

current_tag=$(git describe --exact-match --tags 2> /dev/null)
current_tag="$(git describe --exact-match --tags 2> /dev/null)"

if [[ -z $current_tag ]]; then
if [[ -z "$current_tag" ]]; then
BASH_IT_GIT_VERSION_INFO="$(git log --pretty=format:'%h on %aI' -n 1)"
TARGET=${BASH_IT_GIT_VERSION_INFO%% *}
TARGET="${BASH_IT_GIT_VERSION_INFO%% *}"
echo "Version type: dev"
echo "Current git SHA: $BASH_IT_GIT_VERSION_INFO"
echo "Commit info: $BASH_IT_GIT_URL/commit/$TARGET"
else
TARGET=$current_tag
TARGET="$current_tag"
echo "Version type: stable"
echo "Current tag: $current_tag"
echo "Tag information: $BASH_IT_GIT_URL/releases/tag/$current_tag"
Expand Down Expand Up @@ -489,14 +495,14 @@ _bash-it-describe ()
typeset f
typeset enabled
printf "%-20s%-10s%s\n" "$column_header" 'Enabled?' 'Description'
for f in "${BASH_IT}/$subdirectory/available/"*.bash
for f in "${BASH_IT}/$subdirectory/available"/*.bash
do
# Check for both the old format without the load priority, and the extended format with the priority
declare enabled_files enabled_file
enabled_file="${f##*/}"
enabled_file="${f##*/}"
enabled_files=$(sort <(compgen -G "${BASH_IT}/enabled/*$BASH_IT_LOAD_PRIORITY_SEPARATOR${enabled_file}") <(compgen -G "${BASH_IT}/$subdirectory/enabled/${enabled_file}") <(compgen -G "${BASH_IT}/$subdirectory/enabled/*$BASH_IT_LOAD_PRIORITY_SEPARATOR${enabled_file}") | wc -l)

if [ $enabled_files -gt 0 ]; then
if [ "$enabled_files" -gt 0 ]; then
enabled='x'
else
enabled=' '
Expand Down Expand Up @@ -568,21 +574,29 @@ _disable-thing ()
return
fi

typeset f suffix
suffix=$(echo "$subdirectory" | sed -e 's/plugins/plugin/g')
typeset f suffix _bash_it_config_files
suffix="$(echo "$subdirectory" | sed -e 's/plugins/plugin/g')"

if [ "$file_entity" = "all" ]; then
# Disable everything that's using the old structure
for f in `compgen -G "${BASH_IT}/$subdirectory/enabled/*.${suffix}.bash"`

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[@]}"
do
rm "$f"
done

OLDIFS="$IFS"; IFS=$'\n'
_bash_it_config_files=( $(compgen -G "${BASH_IT}/enabled/*.${suffix}.bash") )
IFS="$OLDIFS"; unset OLDIFS
# Disable everything in the global "enabled" directory
for f in `compgen -G "${BASH_IT}/enabled/*.${suffix}.bash"`
for f in "${_bash_it_config_files[@]}"
do
rm "$f"
done
unset _bash_it_config_files
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)
if [ -z "$plugin_global" ]; then
Expand Down Expand Up @@ -658,30 +672,30 @@ _enable-thing ()

if [ "$file_entity" = "all" ]; then
typeset f $file_type
for f in "${BASH_IT}/$subdirectory/available/"*.bash
for f in "${BASH_IT}/$subdirectory/available"/*.bash
do
to_enable=$(basename $f .$file_type.bash)
to_enable="$(basename "$f" ".$file_type.bash")"
if [ "$file_type" = "alias" ]; then
to_enable=$(basename $f ".aliases.bash")
to_enable="$(basename "$f" ".aliases.bash")"
fi
_enable-thing $subdirectory $file_type $to_enable $load_priority
_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)
typeset 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##*/}
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)
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)"
if [ ! -z "$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)
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)"
if [ ! -z "$enabled_plugin_global" ] ; then
printf '%s\n' "$file_entity is already enabled."
return
Expand All @@ -691,10 +705,10 @@ _enable-thing ()

# Load the priority from the file if it present there
declare local_file_priority use_load_priority
local_file_priority=$(grep -E "^# BASH_IT_LOAD_PRIORITY:" "${BASH_IT}/$subdirectory/available/$to_enable" | awk -F': ' '{ print $2 }')
use_load_priority=${local_file_priority:-$load_priority}
local_file_priority="$(grep -E "^# BASH_IT_LOAD_PRIORITY:" "${BASH_IT}/$subdirectory/available/$to_enable" | awk -F': ' '{ print $2 }')"
use_load_priority="${local_file_priority:-$load_priority}"

ln -s ../$subdirectory/available/$to_enable "${BASH_IT}/enabled/${use_load_priority}${BASH_IT_LOAD_PRIORITY_SEPARATOR}${to_enable}"
ln -s "../$subdirectory/available/$to_enable" "${BASH_IT}/enabled/${use_load_priority}${BASH_IT_LOAD_PRIORITY_SEPARATOR}${to_enable}"
fi

_bash-it-clean-component-cache "${file_type}"
Expand Down Expand Up @@ -813,19 +827,18 @@ all_groups ()
}

if ! type pathmunge > /dev/null 2>&1
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'
example 'pathmunge /path/to/dir after is equivalent to PATH=$PATH:/path/to/dir'

if ! [[ $PATH =~ (^|:)$1($|:) ]] ; then
if [ "$2" = "after" ] ; then
export PATH=$PATH:$1
else
export PATH=$1:$PATH
fi
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'
example 'pathmunge /path/to/dir after is equivalent to PATH=$PATH:/path/to/dir'

if [[ -d "${1:-}" && ! $PATH =~ (^|:)$1($|:) ]] ; then
if [ "${2:-}" = "after" ] ; then
export PATH=$PATH:$1
else
export PATH=$1:$PATH
fi
}
fi
}
fi

0 comments on commit 23ffbe6

Please sign in to comment.