Skip to content

Commit 86ec9b1

Browse files
committed
linting: add shelcheck for shell script lint
Add linting for shell scripts in the scipts directory as well as any *.sh file accross the project using shellcheck. Signed-off-by: Andreas Hatziiliou <andreas.hatziiliou@savoirfairelinux.com>
1 parent 0e242f8 commit 86ec9b1

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

scripts/lint

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,57 @@ gh_error_simple()
106106
fi
107107
}
108108

109+
# Identify shell scripts that should be linted with shellcheck.
110+
shellcheck-targets()
111+
{
112+
git ls-files -z -- "*.sh" "scripts/*" "scripts/**/*"
113+
}
114+
115+
run-shellcheck()
116+
{
117+
if ! command -v shellcheck >/dev/null; then
118+
gh_error_simple "Shellcheck missing" "shellcheck is not installed"
119+
error "Lint shellcheck"
120+
SUCCESS=false
121+
gh_summary_failure "Lint shellcheck"
122+
return 0
123+
fi
124+
125+
local file
126+
local -a files=()
127+
while IFS= read -r -d '' file; do
128+
if [[ -L $file || ! -f $file ]]; then
129+
continue
130+
fi
131+
case "$file" in
132+
*.sh)
133+
files+=("$file")
134+
continue
135+
;;
136+
esac
137+
if head -n1 "$file" | grep -qE '^#!.*/(env )?(ba|da|k|mk|z|)sh'; then
138+
files+=("$file")
139+
fi
140+
done < <(shellcheck-targets)
141+
142+
if [[ ${#files[@]} == 0 ]]; then
143+
info "Lint shellcheck (no scripts)"
144+
gh_summary_success "Lint shellcheck"
145+
return 0
146+
fi
147+
148+
local shellcheck_out
149+
if ! shellcheck_out=$(shellcheck --format=gcc "${files[@]}" 2>&1); then
150+
gh_error_simple "Shellcheck error" "$shellcheck_out"
151+
error "Lint shellcheck"
152+
SUCCESS=false
153+
gh_summary_failure "Lint shellcheck"
154+
else
155+
info "Lint shellcheck"
156+
gh_summary_success "Lint shellcheck"
157+
fi
158+
}
159+
109160
# Formatting
110161
SUCCESS=true
111162

@@ -117,6 +168,10 @@ gh_group_start "Linting shell scripts with shfmt"
117168
checkerr "Lint shell" "$(shfmt -s -l -i 2 -ci -fn $(shfmt -f $(git grep -l '' :/)))"
118169
gh_group_end
119170

171+
gh_group_start "Linting shell scripts with shellcheck"
172+
run-shellcheck
173+
gh_group_end
174+
120175
gh_group_start "Linting python scripts with black"
121176
if ! diff=$(black --check --diff -q --include "(scripts/tests|scripts/simpasm|scripts/cfify|scripts/autogen|scripts/check-namespace|\.py$)" "$ROOT"); then
122177
gh_error_simple "Format error" "$diff"

0 commit comments

Comments
 (0)