Skip to content

Commit 561039a

Browse files
committed
Added info on shellcheck --enable arg to README
1 parent 3714dd2 commit 561039a

File tree

2 files changed

+43
-35
lines changed

2 files changed

+43
-35
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,19 @@ Now when the pre-commit hook runs, it will call `helm lint` with both `linter_va
125125
helm lint -f values.yaml -f linter_values.yaml .
126126
```
127127

128+
## Shellcheck Arguments
129+
130+
To enable optional shellcheck features you can use the `--enable` flag.
131+
Other shellcheck flags can not be passed through.
132+
133+
```yaml
134+
repos:
135+
- repo: https://github.com/gruntwork-io/pre-commit
136+
rev: <VERSION>
137+
hooks:
138+
- id: shellcheck
139+
args: ["--enable require-variable-braces,deprecate-which"]
140+
```
128141

129142

130143
## License

hooks/shellcheck.sh

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,42 @@ set -e
88
export PATH=$PATH:/usr/local/bin
99

1010
exit_status=0
11-
ENABLE_LIST=""
11+
enable_list=""
1212

13-
# Arguments
1413
parse_arguments() {
15-
while [ $# -gt 0 ]; do
16-
# Get param and value using parameter expansion, splitting on = or " "
17-
param="${1%[ =]*}"
18-
value="${1#*[ =]}"
19-
if [ "$param" = "$value" ]; then value="$2"; fi
20-
shift
21-
case "$param" in
22-
--enable)
23-
ENABLE_LIST="$ENABLE_LIST $value"
24-
;;
25-
-*)
26-
echo "Error: Unknown option: $param" >&2
27-
exit 1
28-
;;
29-
*)
30-
PARAMS="$PARAMS $param"
31-
;;
32-
esac
33-
done
34-
ENABLE_LIST="${ENABLE_LIST## }" # remove preceeding space
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
3534
}
3635

3736
parse_arguments "$@"
3837

39-
for file in $PARAMS; do
40-
if (head -1 "$file" | grep '^#!.*sh'>/dev/null); then
41-
SHELLCHECK_ARGS=""
42-
if [ "$ENABLE_LIST" != "" ]; then
43-
SHELLCHECK_ARGS+="--enable=\"$ENABLE_LIST\" "
44-
fi
45-
if ! eval "shellcheck $SHELLCHECK_ARGS\"$file\""; then
46-
exit_status=1
47-
fi
48-
elif [[ "$file" =~ \.sh$|bash$ ]]; then
49-
echo "$file: missing shebang"
50-
exit_status=1
51-
fi
38+
for FILE in $files; do
39+
if (head -1 "$FILE" | grep '^#!.*sh' >/dev/null); then
40+
if ! shellcheck ${enable_list:+ --enable="$enable_list"} "$FILE"; then
41+
exit_status=1
42+
fi
43+
elif [[ "$FILE" =~ \.sh$|bash$ ]]; then
44+
echo "$FILE: missing shebang"
45+
exit_status=1
46+
fi
5247
done
5348

5449
exit $exit_status

0 commit comments

Comments
 (0)