Debian Codegraph @

etc/bin/shellchecker

raw
1 #!/usr/bin/env bash
2 #
3 # THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
4 # Copyright (C) 2008-2019 NIWA & British Crown (Met Office) & Contributors.
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 # Wrapper for running `shellcheck` over projects.
20
21 set -e
22 cd "$(dirname "$0")/../../"
23
24 # find shell files under the specified directory
25 find_files () {
26 SEARCH_PATH="$1"
27 grep --color=never -rl '^#\!.*bash' "${SEARCH_PATH}" | sed 's/^\.\///'
28 find "${SEARCH_PATH}" -name '*.sh' | sed 's/^\.\///'
29 }
30
31 main () {
32 # parse CLI
33 includes=()
34 excludes=()
35 args=()
36 while [[ "$#" -gt 0 ]]; do
37 if [[ ${1} == '--' ]]; then
38 shift
39 break
40 elif [[ ${1} == "--exclude" ]]; then
41 shift
42 excludes+=("$1")
43 elif [[ ${1} == "-*" ]]; then
44 args+=("$1")
45 else
46 mapfile -t files < <(find_files "${1}")
47 includes=("${includes[@]}" "${files[@]}")
48 fi
49 shift
50 done
51
52 # extract files list
53 files=()
54 for include in "${includes[@]}"; do
55 skip=0
56 for exclude in "${excludes[@]}"; do
57 if [[ "${include}" == "${exclude}"* ]]; then
58 skip=1
59 break
60 fi
61 done
62 if [[ $skip == 0 ]]; then
63 files+=("${include}")
64 fi
65 done
66
67 # sort $files removing duplicates
68 readarray -t files < <(for x in "${files[@]}"; do echo "$x"; done | sort -u)
69
70 # run shellcheck
71 shellcheck "$@" "${files[@]}"
72 }
73
74 # default configuration for linting cylc
75 default () {
76 # run a strict check on all "functional" scripts
77 main '.' \
78 --exclude '.git' \
79 -- \
80 -e SC1090 \
81 -e SC1091
82 }
83
84 if [[ $# -gt 0 ]]; then
85 main "$@"
86 else
87 default
88 fi