-
Notifications
You must be signed in to change notification settings - Fork 0
/
shellcheckit
executable file
·100 lines (79 loc) · 2.15 KB
/
shellcheckit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/bin/bash
set -eu
# needs to be specific to not include zsh and some people do odd /usr/bin/env bash things
SHEBANG_REGEX='^#\( *shellcheck \|!\(/bin/\|/usr/bin/env \)\(sh\|bash\|dash\|ksh\)\)'
WORKFLOW=.github/workflows/shellcheck.yml
if ! git grep "$SHEBANG_REGEX"; then
echo "No shell scripts found, no need"
exit 0
fi
source "$(readlink -f "$0" | xargs dirname)/common.sh"
function list() {
git grep -l "$SHEBANG_REGEX" | grep -v '\.\(txt\|md\)'
}
function doit() {
list | xargs shellcheck "$@"
}
function autopatch() {
list | xargs shellcheck -f diff | patch -p1
}
function generate_workflow() {
# we might need to overload branch if we already have it and just updating
if [ -e "$WORKFLOW" ]; then
branch_=$(sed -n -e '/branches:/s,.*\[\(.*\)\].*,\1,gp' "$WORKFLOW" | uniq)
if [ -n "$branch_" ]; then
branch="$branch_"
fi
fi
cat > "$WORKFLOW" << EOF
---
name: Shellcheck
on:
push:
branches: [$branch]
pull_request:
branches: [$branch]
permissions:
contents: read
jobs:
shellcheck:
name: Check shell scripts
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install dependencies
run: |
sudo apt update && sudo apt install -y shellcheck
- name: shellcheck
run: |
git grep -l '$SHEBANG_REGEX' | xargs shellcheck
EOF
}
# poor man shortcuts for just doing it using the regex etc
case "${1:-}" in
list) shift; list "$@"; exit $?;;
doit) shift; doit "$@"; exit $?;;
autopatch) shift; autopatch "$@"; exit $?;;
generate_workflow) generate_workflow; exit $?;;
esac
if [ -e "$WORKFLOW" ]; then
echo "$WORKFLOW already exists -- we must be good"
exit 0
fi
git checkout -b enh-shellcheck
generate_workflow
git add "$WORKFLOW"
git commit -m "Add github action to shellcheck $branch on push and PRs"
if [ -e .pre-commit-config.yaml ]; then
# add pre-commit configuration there
cat >> .pre-commit-config.yaml <<EOF
- repo: https://github.com/koalaman/shellcheck-precommit
rev: v0.9.0
hooks:
- id: shellcheck
EOF
git add .pre-commit-config.yaml
git commit -m 'Add pre-commit definition for shellcheck'
fi
doit