Skip to content

Commit 1f136bc

Browse files
committed
Reformat shellcheck.sh to style guide
1 parent 0fdadbe commit 1f136bc

File tree

1 file changed

+55
-39
lines changed

1 file changed

+55
-39
lines changed

hooks/shellcheck.sh

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,60 @@ set -e
77
# workaround to allow GitHub Desktop to work, add this (hopefully harmless) setting here.
88
export PATH=$PATH:/usr/local/bin
99

10-
exit_status=0
11-
enable_list=""
12-
13-
parse_arguments() {
14-
while (($# > 0)); do
15-
# Grab param and value splitting on " " or "=" with parameter expansion
16-
local PARAMETER="${1%[ =]*}"
17-
local VALUE="${1#*[ =]}"
18-
if [[ "$PARAMETER" == "$VALUE" ]]; then VALUE="$2"; fi
19-
shift
20-
case "$PARAMETER" in
21-
--enable)
22-
enable_list="$enable_list $VALUE"
23-
;;
24-
-*)
25-
echo "Error: Unknown option: $PARAMETER" >&2
26-
exit 1
27-
;;
28-
*)
29-
files="$files $PARAMETER"
30-
;;
31-
esac
32-
done
33-
enable_list="${enable_list## }" # remove preceeding space
10+
readonly SHEBANG_REGEX='^#!\(/\|/.*/\|/.* \)\(\(ba\|da\|k\|a\)*sh\|bats\)$'
11+
12+
function shellcheck_files {
13+
local -r enable_list="$1"
14+
local -r files="$2"
15+
16+
local exit_status=0
17+
18+
for file in $files; do
19+
if (head -1 "$file" | grep "$SHEBANG_REGEX" >/dev/null); then
20+
if ! shellcheck ${enable_list:+ --enable="$enable_list"} "$file"; then
21+
exit_status=1
22+
fi
23+
elif [[ "$file" =~ .+\.(sh|bash|dash|ksh|ash|bats)$ ]]; then
24+
echo "$file: missing shebang"
25+
exit_status=1
26+
fi
27+
done
28+
29+
exit $exit_status
30+
}
31+
32+
function run {
33+
local enable_list=""
34+
local files=""
35+
36+
local parameter=""
37+
local value=""
38+
39+
while [[ $# -gt 0 ]]; do
40+
# Grab param and value splitting on " " or "=" with parameter expansion
41+
parameter="${1%[ =]*}"
42+
value="${1#*[ =]}"
43+
if [[ "$parameter" == "$value" ]]; then
44+
value="$2"
45+
fi
46+
shift
47+
48+
case "$parameter" in
49+
--enable)
50+
enable_list="$value"
51+
shift
52+
;;
53+
-*)
54+
echo "Error: Unknown option: $parameter" >&2
55+
exit 1
56+
;;
57+
*)
58+
files="$files $parameter"
59+
;;
60+
esac
61+
done
62+
63+
shellcheck_files "$enable_list" "$files"
3464
}
3565

36-
parse_arguments "$@"
37-
38-
for FILE in $files; do
39-
SHEBANG_REGEX='^#!\(/\|/.*/\|/.* \)\(\(ba\|da\|k\|a\)*sh\|bats\)$'
40-
if (head -1 "$FILE" | grep "$SHEBANG_REGEX" >/dev/null); then
41-
if ! shellcheck ${enable_list:+ --enable="$enable_list"} "$FILE"; then
42-
exit_status=1
43-
fi
44-
elif [[ "$FILE" =~ .+\.(sh|bash|dash|ksh|ash|bats)$ ]]; then
45-
echo "$FILE: missing shebang"
46-
exit_status=1
47-
fi
48-
done
49-
50-
exit $exit_status
66+
run "$@"

0 commit comments

Comments
 (0)