Skip to content

Commit 0fdadbe

Browse files
authored
Merge pull request gruntwork-io#25 from 06kellyjac/shellcheck_optionals
Shellcheck optionals
2 parents 0e0fb57 + e7a9af9 commit 0fdadbe

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
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: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,43 @@ set -e
88
export PATH=$PATH:/usr/local/bin
99

1010
exit_status=0
11+
enable_list=""
1112

12-
for file in "$@"; do
13-
if (head -1 "$file" |grep '^#!.*sh'>/dev/null); then
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
34+
}
1435

15-
if ! shellcheck "$file"; then
16-
exit_status=1
17-
fi
18-
elif [[ "$file" =~ \.sh$|bash$ ]]; then
19-
echo "$file: missing shebang"
20-
exit_status=1
21-
fi
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
2248
done
2349

2450
exit $exit_status

0 commit comments

Comments
 (0)