Skip to content

Commit 0855c76

Browse files
committed
lib/helpers: lint
Quote things, appease SC2268, SC2143, SC2181, SC2162, &c
1 parent 6c0e3b6 commit 0855c76

File tree

1 file changed

+99
-86
lines changed

1 file changed

+99
-86
lines changed

lib/helpers.bash

Lines changed: 99 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -102,40 +102,40 @@ bash-it ()
102102

103103
case $verb in
104104
show)
105-
func=_bash-it-$component;;
105+
func="_bash-it-$component";;
106106
enable)
107-
func=_enable-$component;;
107+
func="_enable-$component";;
108108
disable)
109-
func=_disable-$component;;
109+
func="_disable-$component";;
110110
help)
111-
func=_help-$component;;
111+
func="_help-$component";;
112112
doctor)
113-
func=_bash-it-doctor-$component;;
113+
func="_bash-it-doctor-$component";;
114114
search)
115-
_bash-it-search $component "$@"
115+
_bash-it-search "$component" "$@"
116116
return;;
117117
update)
118-
func=_bash-it-update-$component;;
118+
func="_bash-it-update-$component";;
119119
migrate)
120-
func=_bash-it-migrate;;
120+
func="_bash-it-migrate";;
121121
version)
122-
func=_bash-it-version;;
122+
func="_bash-it-version";;
123123
restart)
124-
func=_bash-it-restart;;
124+
func="_bash-it-restart";;
125125
reload)
126-
func=_bash-it-reload;;
126+
func="_bash-it-reload";;
127127
*)
128-
reference bash-it
128+
reference "bash-it"
129129
return;;
130130
esac
131131

132132
# pluralize component if necessary
133-
if ! _is_function $func; then
134-
if _is_function ${func}s; then
135-
func=${func}s
133+
if ! _is_function "$func"; then
134+
if _is_function "${func}s"; then
135+
func="${func}s"
136136
else
137-
if _is_function ${func}es; then
138-
func=${func}es
137+
if _is_function "${func}es"; then
138+
func="${func}es"
139139
else
140140
echo "oops! $component is not a valid option!"
141141
reference bash-it
@@ -144,7 +144,8 @@ bash-it ()
144144
fi
145145
fi
146146

147-
if [ x"$verb" == x"enable" ] || [ x"$verb" == x"disable" ]; then
147+
if [[ "$verb" == "enable" || "$verb" == "disable" ]]
148+
then
148149
# Automatically run a migration if required
149150
_bash-it-migrate
150151

@@ -157,7 +158,7 @@ bash-it ()
157158
_bash-it-reload
158159
fi
159160
else
160-
$func "$@"
161+
"$func" "$@"
161162
fi
162163
}
163164

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

172173
_bash-it-aliases ()
@@ -208,8 +209,8 @@ _bash-it-update-stable() {
208209
}
209210

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

230231
declare silent
231-
for word in $@; do
232-
if [[ ${word} == "--silent" || ${word} == "-s" ]]; then
232+
for word in "$@"; do
233+
if [[ "${word}" == "--silent" || "${word}" == "-s" ]]; then
233234
silent=true
234235
fi
235236
done
@@ -244,7 +245,7 @@ _bash-it-update-() {
244245
BASH_IT_REMOTE="origin"
245246
fi
246247

247-
git fetch $BASH_IT_REMOTE --tags &> /dev/null
248+
git fetch "$BASH_IT_REMOTE" --tags &> /dev/null
248249

249250
if [ -z "$BASH_IT_DEVELOPMENT_BRANCH" ]; then
250251
BASH_IT_DEVELOPMENT_BRANCH="master"
@@ -260,48 +261,48 @@ _bash-it-update-() {
260261
fi
261262
else
262263
version="dev"
263-
TARGET=${BASH_IT_REMOTE}/${BASH_IT_DEVELOPMENT_BRANCH}
264+
TARGET="${BASH_IT_REMOTE}/${BASH_IT_DEVELOPMENT_BRANCH}"
264265
fi
265266

266267
declare revision
267268
revision="HEAD..${TARGET}"
268269
declare status
269-
status="$(git rev-list ${revision} 2> /dev/null)"
270+
status="$(git rev-list "${revision}" 2> /dev/null)"
270271
declare revert
271272

272-
if [[ -z "${status}" && ${version} == "stable" ]]; then
273+
if [[ -z "${status}" && "${version}" == "stable" ]]; then
273274
revision="${TARGET}..HEAD"
274-
status="$(git rev-list ${revision} 2> /dev/null)"
275+
status="$(git rev-list "${revision}" 2> /dev/null)"
275276
revert=true
276277
fi
277278

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

286-
for i in $(git rev-list --merges --first-parent ${revision}); do
287-
num_of_lines=$(git log -1 --format=%B $i | awk 'NF' | wc -l)
288-
if [ $num_of_lines -eq 1 ]; then
287+
for i in $(git rev-list --merges --first-parent "${revision}"); do
288+
num_of_lines=$(git log -1 --format=%B "$i" | awk 'NF' | wc -l)
289+
if [ "$num_of_lines" -eq 1 ]; then
289290
description="%s"
290291
else
291292
description="%b"
292293
fi
293-
git log --format="${log_color}%h: $description (%an)" -1 $i
294+
git log --format="${log_color}%h: $description (%an)" -1 "$i"
294295
done
295296
echo ""
296297

297-
if [[ $silent ]]; then
298+
if [[ "$silent" ]]; then
298299
echo "Updating to ${TARGET}($(git log -1 --format=%h "${TARGET}"))..."
299-
_bash-it_pull_and_update_inner $TARGET $version
300+
_bash-it_pull_and_update_inner "$TARGET" "$version"
300301
else
301-
read -e -n 1 -p "Would you like to update to ${TARGET}($(git log -1 --format=%h "${TARGET}"))? [Y/n] " RESP
302-
case $RESP in
302+
read -r -e -n 1 -p "Would you like to update to ${TARGET}($(git log -1 --format=%h "${TARGET}"))? [Y/n] " RESP
303+
case "$RESP" in
303304
[yY]|"")
304-
_bash-it_pull_and_update_inner $TARGET $version
305+
_bash-it_pull_and_update_inner "$TARGET" "$version"
305306
;;
306307
[nN])
307308
echo "Not updating…"
@@ -312,7 +313,7 @@ _bash-it-update-() {
312313
esac
313314
fi
314315
else
315-
if [[ ${version} == "stable" ]]; then
316+
if [[ "${version}" == "stable" ]]; then
316317
echo "You're on the latest stable version. If you want to check out the latest 'dev' version, please run \"bash-it update dev\""
317318
else
318319
echo "Bash-it is up to date, nothing to do!"
@@ -330,14 +331,18 @@ _bash-it-migrate() {
330331

331332
for file_type in "aliases" "plugins" "completion"
332333
do
333-
for f in `sort <(compgen -G "${BASH_IT}/$file_type/enabled/*.bash")`
334+
OLDIFS="$IFS"; IFS=$'\n'
335+
_bash_it_config_files=( $(sort <(compgen -G "${BASH_IT}/$file_type/enabled/*.bash")) )
336+
IFS="$OLDIFS"; unset OLDIFS
337+
338+
for f in "${_bash_it_config_files[@]}"
334339
do
335340
typeset ff="${f##*/}"
336341

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

342347
migrated_something=true
343348

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

349-
$disable_func $component_name
350-
$enable_func $component_name
354+
$disable_func "$component_name"
355+
$enable_func "$component_name"
351356
done
357+
unset _bash_it_config_files
352358
done
353359

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

368374
cd "${BASH_IT}" || return
369375

370-
if [ -z $BASH_IT_REMOTE ]; then
376+
if [ -z "${BASH_IT_REMOTE:-}" ]; then
371377
BASH_IT_REMOTE="origin"
372378
fi
373379

374-
BASH_IT_GIT_REMOTE=$(git remote get-url $BASH_IT_REMOTE)
375-
BASH_IT_GIT_URL=${BASH_IT_GIT_REMOTE%.git}
380+
BASH_IT_GIT_REMOTE="$(git remote get-url "$BASH_IT_REMOTE")"
381+
BASH_IT_GIT_URL="${BASH_IT_GIT_REMOTE%.git}"
376382
if [[ "$BASH_IT_GIT_URL" == *"git@"* ]]; then
377383
# Fix URL in case it is ssh based URL
378-
BASH_IT_GIT_URL=${BASH_IT_GIT_URL/://}
379-
BASH_IT_GIT_URL=${BASH_IT_GIT_URL/git@/https://}
384+
BASH_IT_GIT_URL="${BASH_IT_GIT_URL/://}"
385+
BASH_IT_GIT_URL="${BASH_IT_GIT_URL/git@/https://}"
380386
fi
381387

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

384-
if [[ -z $current_tag ]]; then
390+
if [[ -z "$current_tag" ]]; then
385391
BASH_IT_GIT_VERSION_INFO="$(git log --pretty=format:'%h on %aI' -n 1)"
386-
TARGET=${BASH_IT_GIT_VERSION_INFO%% *}
392+
TARGET="${BASH_IT_GIT_VERSION_INFO%% *}"
387393
echo "Version type: dev"
388394
echo "Current git SHA: $BASH_IT_GIT_VERSION_INFO"
389395
echo "Commit info: $BASH_IT_GIT_URL/commit/$TARGET"
390396
else
391-
TARGET=$current_tag
397+
TARGET="$current_tag"
392398
echo "Version type: stable"
393399
echo "Current tag: $current_tag"
394400
echo "Tag information: $BASH_IT_GIT_URL/releases/tag/$current_tag"
@@ -489,14 +495,14 @@ _bash-it-describe ()
489495
typeset f
490496
typeset enabled
491497
printf "%-20s%-10s%s\n" "$column_header" 'Enabled?' 'Description'
492-
for f in "${BASH_IT}/$subdirectory/available/"*.bash
498+
for f in "${BASH_IT}/$subdirectory/available"/*.bash
493499
do
494500
# Check for both the old format without the load priority, and the extended format with the priority
495501
declare enabled_files enabled_file
496-
enabled_file="${f##*/}"
502+
enabled_file="${f##*/}"
497503
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)
498504

499-
if [ $enabled_files -gt 0 ]; then
505+
if [ "$enabled_files" -gt 0 ]; then
500506
enabled='x'
501507
else
502508
enabled=' '
@@ -568,21 +574,29 @@ _disable-thing ()
568574
return
569575
fi
570576

571-
typeset f suffix
572-
suffix=$(echo "$subdirectory" | sed -e 's/plugins/plugin/g')
577+
typeset f suffix _bash_it_config_files
578+
suffix="$(echo "$subdirectory" | sed -e 's/plugins/plugin/g')"
573579

574580
if [ "$file_entity" = "all" ]; then
575581
# Disable everything that's using the old structure
576-
for f in `compgen -G "${BASH_IT}/$subdirectory/enabled/*.${suffix}.bash"`
582+
583+
OLDIFS="$IFS"; IFS=$'\n'
584+
_bash_it_config_files=( $(compgen -G "${BASH_IT}/$subdirectory/enabled/*.${suffix}.bash") )
585+
IFS="$OLDIFS"; unset OLDIFS
586+
for f in "${_bash_it_config_files[@]}"
577587
do
578588
rm "$f"
579589
done
580590

591+
OLDIFS="$IFS"; IFS=$'\n'
592+
_bash_it_config_files=( $(compgen -G "${BASH_IT}/enabled/*.${suffix}.bash") )
593+
IFS="$OLDIFS"; unset OLDIFS
581594
# Disable everything in the global "enabled" directory
582-
for f in `compgen -G "${BASH_IT}/enabled/*.${suffix}.bash"`
595+
for f in "${_bash_it_config_files[@]}"
583596
do
584597
rm "$f"
585598
done
599+
unset _bash_it_config_files
586600
else
587601
typeset plugin_global=$(command ls $ "${BASH_IT}/enabled/"[0-9]*$BASH_IT_LOAD_PRIORITY_SEPARATOR$file_entity.$suffix.bash 2>/dev/null | head -1)
588602
if [ -z "$plugin_global" ]; then
@@ -658,30 +672,30 @@ _enable-thing ()
658672

659673
if [ "$file_entity" = "all" ]; then
660674
typeset f $file_type
661-
for f in "${BASH_IT}/$subdirectory/available/"*.bash
675+
for f in "${BASH_IT}/$subdirectory/available"/*.bash
662676
do
663-
to_enable=$(basename $f .$file_type.bash)
677+
to_enable="$(basename "$f" ".$file_type.bash")"
664678
if [ "$file_type" = "alias" ]; then
665-
to_enable=$(basename $f ".aliases.bash")
679+
to_enable="$(basename "$f" ".aliases.bash")"
666680
fi
667-
_enable-thing $subdirectory $file_type $to_enable $load_priority
681+
_enable-thing "$subdirectory" "$file_type" "$to_enable" $load_priority
668682
done
669683
else
670-
typeset to_enable=$(command ls "${BASH_IT}/$subdirectory/available/"$file_entity.*bash 2>/dev/null | head -1)
684+
typeset to_enable="$(command ls "${BASH_IT}/$subdirectory/available"/$file_entity.*bash 2>/dev/null | head -1)"
671685
if [ -z "$to_enable" ]; then
672686
printf '%s\n' "sorry, $file_entity does not appear to be an available $file_type."
673687
return
674688
fi
675689

676-
to_enable=${to_enable##*/}
690+
to_enable="${to_enable##*/}"
677691
# Check for existence of the file using a wildcard, since we don't know which priority might have been used when enabling it.
678-
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)
692+
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)"
679693
if [ ! -z "$enabled_plugin" ] ; then
680694
printf '%s\n' "$file_entity is already enabled."
681695
return
682696
fi
683697

684-
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)
698+
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)"
685699
if [ ! -z "$enabled_plugin_global" ] ; then
686700
printf '%s\n' "$file_entity is already enabled."
687701
return
@@ -691,10 +705,10 @@ _enable-thing ()
691705

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

697-
ln -s ../$subdirectory/available/$to_enable "${BASH_IT}/enabled/${use_load_priority}${BASH_IT_LOAD_PRIORITY_SEPARATOR}${to_enable}"
711+
ln -s "../$subdirectory/available/$to_enable" "${BASH_IT}/enabled/${use_load_priority}${BASH_IT_LOAD_PRIORITY_SEPARATOR}${to_enable}"
698712
fi
699713

700714
_bash-it-clean-component-cache "${file_type}"
@@ -813,19 +827,18 @@ all_groups ()
813827
}
814828

815829
if ! type pathmunge > /dev/null 2>&1
816-
then
817-
function pathmunge () {
818-
about 'prevent duplicate directories in you PATH variable'
819-
group 'helpers'
820-
example 'pathmunge /path/to/dir is equivalent to PATH=/path/to/dir:$PATH'
821-
example 'pathmunge /path/to/dir after is equivalent to PATH=$PATH:/path/to/dir'
822-
823-
if ! [[ $PATH =~ (^|:)$1($|:) ]] ; then
824-
if [ "$2" = "after" ] ; then
825-
export PATH=$PATH:$1
826-
else
827-
export PATH=$1:$PATH
828-
fi
830+
then function pathmunge () {
831+
about 'prevent duplicate directories in you PATH variable'
832+
group 'helpers'
833+
example 'pathmunge /path/to/dir is equivalent to PATH=/path/to/dir:$PATH'
834+
example 'pathmunge /path/to/dir after is equivalent to PATH=$PATH:/path/to/dir'
835+
836+
if [[ -d "${1:-}" && ! $PATH =~ (^|:)$1($|:) ]] ; then
837+
if [ "${2:-}" = "after" ] ; then
838+
export PATH=$PATH:$1
839+
else
840+
export PATH=$1:$PATH
829841
fi
830-
}
842+
fi
843+
}
831844
fi

0 commit comments

Comments
 (0)