Skip to content

Commit 3eed0f0

Browse files
authored
Lint: prepare lib/utilities for shellcheck (#1933)
* lib/utilities: shellcheck SC2059 * lib/utilities: fix `_bash-it-get-component-type-from-path()` Account for plugins with names that contain periods. * lib/utilities: fix `_bash-it-array-dedup()` Use fewer subprocesses and newline-delimited not space-delimited. * lib/utilities: fix `_bash-it-component-list()` Use fewer subprocesses and return newline-delimited, not space-delimited. * lib/utilities: fix `_bash-it-component-list-matching()` Use `sort -u` instead of `sort | uniq` * lib/utilities: fix `_bash-it-component-list-enabled()` Use fewer subprocesses, return newline-delimited instead of space-delimited, and use `sort -u` instead of `uniq | sort` * lib/utilities: fix `_bash-it-component-list-disabled()` Use fewer subprocesses, return newline-delimited instead of space-delimited, and use `sort -u` instead of `uniq | sort` * lib/utilities: fix `_bash-it-grep()` 1. Executing `'/usr/bin/grep'` does *not* return the path to grep... 2. use `type -p` instead of external binary `which`. 3. Simplify parameter definition. 4. Why was there a space after `%s`? * lib/utilities: use `_bash-it-grep` Alsö, lose a spurious `cat` * lib/utilities: lint `_bash-it-component-help` * lib/utilities: lint `_bash-it-component-cache-file()` * lib/utilities: `shfmt` My apologies to future `git blame` hunters ♥ * lib/helpers: fix `_bash-it-get-component-name-from-path()` Use `${BASH_IT_LOAD_PRIORITY_SEPARATOR}`
1 parent 8a1dc96 commit 3eed0f0

File tree

2 files changed

+92
-85
lines changed

2 files changed

+92
-85
lines changed

Diff for: clean_files.txt

+3
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ completion/available/vault.completion.bash
7575
completion/available/vuejs.completion.bash
7676
completion/available/wpscan.completion.bash
7777

78+
# libraries
79+
lib/utilities.bash
80+
7881
# plugins
7982
#
8083
plugins/available/alias-completion.plugin.bash

Diff for: lib/utilities.bash

+89-85
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
1-
#!/usr/bin/env bash
1+
# shellcheck shell=bash
22
#
33
# A collection of reusable functions.
44

55
###########################################################################
66
# Generic utilies
77
###########################################################################
88

9-
_bash-it-get-component-name-from-path() {
10-
# filename without path
11-
filename=${1##*/}
12-
# filename without path or priority
13-
filename=${filename##*---}
14-
# filename without path, priority or extension
15-
echo ${filename%.*.bash}
9+
function _bash-it-get-component-name-from-path() {
10+
local filename
11+
# filename without path
12+
filename="${1##*/}"
13+
# filename without path or priority
14+
filename="${filename##*${BASH_IT_LOAD_PRIORITY_SEPARATOR?}}"
15+
# filename without path, priority or extension
16+
echo "${filename%.*.bash}"
1617
}
1718

18-
_bash-it-get-component-type-from-path() {
19-
# filename without path
20-
filename=${1##*/}
21-
# filename without path or priority
22-
filename=${filename##*---}
23-
# extension
24-
echo ${filename} | cut -d '.' -f 2
19+
function _bash-it-get-component-type-from-path() {
20+
local filename
21+
# filename without path
22+
filename="${1##*/}"
23+
# filename without extension
24+
filename="${filename%.bash}"
25+
# extension without priority or name
26+
filename="${filename##*.}"
27+
echo "${filename}"
2528
}
2629

2730
# This function searches an array for an exact match against the term passed
@@ -43,96 +46,97 @@ _bash-it-get-component-type-from-path() {
4346
# contains pear!
4447
#
4548
#
46-
_bash-it-array-contains-element() {
47-
local e
48-
for e in "${@:2}"; do
49-
[[ "$e" == "$1" ]] && return 0
50-
done
51-
return 1
49+
function _bash-it-array-contains-element() {
50+
local e
51+
for e in "${@:2}"; do
52+
[[ "$e" == "$1" ]] && return 0
53+
done
54+
return 1
5255
}
5356

5457
# Dedupe a simple array of words without spaces.
55-
_bash-it-array-dedup() {
56-
echo "$*" | tr ' ' '\n' | sort -u | tr '\n' ' '
58+
function _bash-it-array-dedup() {
59+
local IFS=$'\n'
60+
echo "$@" | tr ' ' '\n' | sort -u
5761
}
5862

5963
# Outputs a full path of the grep found on the filesystem
60-
_bash-it-grep() {
61-
if [[ -z "${BASH_IT_GREP:-}" ]] ; then
62-
export BASH_IT_GREP="$(which egrep || which grep || '/usr/bin/grep')"
63-
fi
64-
printf "%s " "${BASH_IT_GREP}"
64+
function _bash-it-grep() {
65+
: "${BASH_IT_GREP:=$(type -p egrep || type -p grep)}"
66+
printf "%s" "${BASH_IT_GREP:-'/usr/bin/grep'}"
6567
}
6668

67-
6869
###########################################################################
6970
# Component-specific functions (component is either an alias, a plugin, or a
7071
# completion).
7172
###########################################################################
7273

73-
_bash-it-component-help() {
74-
local component="$(_bash-it-pluralize-component "${1}")"
75-
local file="$(_bash-it-component-cache-file "${component}")"
76-
if [[ ! -s "${file}" || -z $(find "${file}" -mmin -300) ]] ; then
77-
rm -f "${file}" 2>/dev/null
78-
local func="_bash-it-${component}"
79-
"${func}" | $(_bash-it-grep) -E ' \[' | cat > "${file}"
80-
fi
81-
cat "${file}"
74+
function _bash-it-component-help() {
75+
local component file func
76+
component="$(_bash-it-pluralize-component "${1}")"
77+
file="$(_bash-it-component-cache-file "${component}")"
78+
if [[ ! -s "${file}" || -z "$(find "${file}" -mmin -300)" ]]; then
79+
rm -f "${file}" 2> /dev/null
80+
func="_bash-it-${component}"
81+
"${func}" | ${BASH_IT_GREP:-$(_bash-it-grep)} -E ' \[' > "${file}"
82+
fi
83+
cat "${file}"
8284
}
8385

84-
_bash-it-component-cache-file() {
85-
local component=$(_bash-it-pluralize-component "${1}")
86-
local file="${BASH_IT}/tmp/cache/${component}"
87-
[[ -f "${file}" ]] || mkdir -p "${file%/*}"
88-
printf "${file}"
86+
function _bash-it-component-cache-file() {
87+
local component file
88+
component="$(_bash-it-pluralize-component "${1}")"
89+
file="${BASH_IT?}/tmp/cache/${component}"
90+
[[ -f "${file}" ]] || mkdir -p "${file%/*}"
91+
printf '%s' "${file}"
8992
}
9093

91-
_bash-it-pluralize-component() {
92-
local component="${1}"
93-
local len=$(( ${#component} - 1 ))
94-
# pluralize component name for consistency
95-
[[ ${component:${len}:1} != 's' ]] && component="${component}s"
96-
[[ ${component} == "alias" ]] && component="aliases"
97-
printf ${component}
94+
function _bash-it-pluralize-component() {
95+
local component="${1}"
96+
local -i len=$((${#component} - 1))
97+
# pluralize component name for consistency
98+
[[ "${component:${len}:1}" != 's' ]] && component="${component}s"
99+
[[ "${component}" == "alias" ]] && component="aliases"
100+
printf '%s' "${component}"
98101
}
99102

100-
_bash-it-clean-component-cache() {
101-
local component="$1"
102-
local cache
103-
local -a BASH_IT_COMPONENTS=(aliases plugins completions)
104-
if [[ -z ${component} ]] ; then
105-
for component in "${BASH_IT_COMPONENTS[@]}" ; do
106-
_bash-it-clean-component-cache "${component}"
107-
done
108-
else
109-
cache="$(_bash-it-component-cache-file ${component})"
110-
if [[ -f "${cache}" ]] ; then
111-
rm -f "${cache}"
112-
fi
113-
fi
103+
function _bash-it-clean-component-cache() {
104+
local component="$1"
105+
local cache
106+
local -a BASH_IT_COMPONENTS=(aliases plugins completions)
107+
if [[ -z "${component}" ]]; then
108+
for component in "${BASH_IT_COMPONENTS[@]}"; do
109+
_bash-it-clean-component-cache "${component}"
110+
done
111+
else
112+
cache="$(_bash-it-component-cache-file "${component}")"
113+
if [[ -f "${cache}" ]]; then
114+
rm -f "${cache}"
115+
fi
116+
fi
114117
}
115118

116119
# Returns an array of items within each compoenent.
117-
_bash-it-component-list() {
118-
local component="$1"
119-
_bash-it-component-help "${component}" | awk '{print $1}' | uniq | sort | tr '\n' ' '
120+
function _bash-it-component-list() {
121+
local IFS=$'\n' component="$1"
122+
_bash-it-component-help "${component}" | awk '{print $1}' | sort -u
120123
}
121124

122-
_bash-it-component-list-matching() {
123-
local component="$1"; shift
124-
local term="$1"
125-
_bash-it-component-help "${component}" | $(_bash-it-grep) -E -- "${term}" | awk '{print $1}' | sort | uniq
125+
function _bash-it-component-list-matching() {
126+
local component="$1"
127+
shift
128+
local term="$1"
129+
_bash-it-component-help "${component}" | ${BASH_IT_GREP:-$(_bash-it-grep)} -E -- "${term}" | awk '{print $1}' | sort -u
126130
}
127131

128-
_bash-it-component-list-enabled() {
129-
local component="$1"
130-
_bash-it-component-help "${component}" | $(_bash-it-grep) -E '\[x\]' | awk '{print $1}' | uniq | sort | tr '\n' ' '
132+
function _bash-it-component-list-enabled() {
133+
local IFS=$'\n' component="$1"
134+
_bash-it-component-help "${component}" | ${BASH_IT_GREP:-$(_bash-it-grep)} -E '\[x\]' | awk '{print $1}' | sort -u
131135
}
132136

133-
_bash-it-component-list-disabled() {
134-
local component="$1"
135-
_bash-it-component-help "${component}" | $(_bash-it-grep) -E -v '\[x\]' | awk '{print $1}' | uniq | sort | tr '\n' ' '
137+
function _bash-it-component-list-disabled() {
138+
local IFS=$'\n' component="$1"
139+
_bash-it-component-help "${component}" | ${BASH_IT_GREP:-$(_bash-it-grep)} -E -v '\[x\]' | awk '{print $1}' | sort -u
136140
}
137141

138142
# Checks if a given item is enabled for a particular component/file-type.
@@ -143,10 +147,10 @@ _bash-it-component-list-disabled() {
143147
#
144148
# Examples:
145149
# _bash-it-component-item-is-enabled alias git && echo "git alias is enabled"
146-
_bash-it-component-item-is-enabled() {
147-
local component="$1"
148-
local item="$2"
149-
_bash-it-component-help "${component}" | $(_bash-it-grep) -E '\[x\]' | $(_bash-it-grep) -E -q -- "^${item}\s"
150+
function _bash-it-component-item-is-enabled() {
151+
local component="$1"
152+
local item="$2"
153+
_bash-it-component-help "${component}" | ${BASH_IT_GREP:-$(_bash-it-grep)} -E '\[x\]' | ${BASH_IT_GREP:-$(_bash-it-grep)} -E -q -- "^${item}\s"
150154
}
151155

152156
# Checks if a given item is disabled for a particular component/file-type.
@@ -157,8 +161,8 @@ _bash-it-component-item-is-enabled() {
157161
#
158162
# Examples:
159163
# _bash-it-component-item-is-disabled alias git && echo "git aliases are disabled"
160-
_bash-it-component-item-is-disabled() {
161-
local component="$1"
162-
local item="$2"
163-
_bash-it-component-help "${component}" | $(_bash-it-grep) -E -v '\[x\]' | $(_bash-it-grep) -E -q -- "^${item}\s"
164+
function _bash-it-component-item-is-disabled() {
165+
local component="$1"
166+
local item="$2"
167+
_bash-it-component-help "${component}" | ${BASH_IT_GREP:-$(_bash-it-grep)} -E -v '\[x\]' | ${BASH_IT_GREP:-$(_bash-it-grep)} -E -q -- "^${item}\s"
164168
}

0 commit comments

Comments
 (0)