Skip to content

Commit 559c42f

Browse files
committed
Add shell script linter
This repo is heavy on shell scripts. Linting could be a nice addition. Therefore this commit adds: - linting via shellcheck - an example .shellcheckrc file for global shellcheck control - a shellcheck.sh helper script to run it on specific shell files - a .shellcheck configuration for the helper script
1 parent dbbce0f commit 559c42f

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

.github/workflows/lint.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: Lint
2+
on:
3+
push:
4+
branches:
5+
- '**'
6+
7+
jobs:
8+
shellcheck:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: 'Install shellcheck'
12+
run: sudo apt-get install -y shellcheck
13+
- name: Checkout
14+
uses: actions/checkout@v3
15+
- name: 'Run shellcheck'
16+
run: ./utils/scripts/shellcheck.sh

.shellcheck

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# These files have not been fixed yet
2+
TODO=(
3+
utils/scripts/load-binary.sh
4+
utils/scripts/install_mitm_certificate.sh
5+
utils/scripts/update_dashboard_CI_visibility.sh
6+
utils/scripts/slack/report.sh
7+
utils/scripts/docker_base_image.sh
8+
utils/scripts/upload_results_CI_visibility.sh
9+
utils/build/docker/postgres-init-db.sh
10+
utils/build/docker/nodejs/app.sh
11+
utils/build/docker/nodejs/install_ddtrace.sh
12+
utils/build/docker/set-uds-transport.sh
13+
utils/build/docker/python/flask/app.sh
14+
utils/build/docker/python/install_ddtrace.sh
15+
utils/build/docker/python/django/app.sh
16+
utils/build/docker/golang/app.sh
17+
utils/build/docker/golang/install_ddtrace.sh
18+
utils/build/docker/weblog-cmd.sh
19+
utils/build/docker/java/app.sh
20+
utils/build/docker/java/install_ddtrace.sh
21+
utils/build/docker/java/spring-boot/app.sh
22+
utils/build/docker/php/apache-mod/build.sh
23+
utils/build/docker/php/apache-mod/entrypoint.sh
24+
utils/build/docker/php/php-fpm/build.sh
25+
utils/build/docker/php/php-fpm/entrypoint.sh
26+
utils/build/docker/php/common/install_ddtrace.sh
27+
utils/build/docker/dotnet/app.sh
28+
utils/build/docker/dotnet/install_ddtrace.sh
29+
utils/build/docker/cpp/install_ddprof.sh
30+
utils/build/docker/cpp/nginx/app.sh
31+
utils/build/docker/cpp/nginx/install_ddtrace.sh
32+
utils/build/docker/ruby/install_ddtrace.sh
33+
utils/build/build.sh
34+
utils/interfaces/schemas/serve.sh
35+
build.sh
36+
format.sh
37+
run.sh
38+
)

.shellcheckrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Add globally disabled checks here, e.g:
2+
#
3+
# SC1091: Not following <file>
4+
# https://github.com/koalaman/shellcheck/wiki/SC1091
5+
# disable=SC1091

utils/scripts/shellcheck.sh

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
set -u
5+
set -o pipefail
6+
7+
function has() {
8+
local needle="${1}"
9+
shift
10+
11+
local e
12+
for e; do [[ "${e}" == "${needle}" ]] && return 0; done
13+
14+
return 1
15+
}
16+
17+
function walk() {
18+
local candidate="${1}"
19+
shift
20+
21+
local names=( "${@}" )
22+
23+
while [[ -n "${candidate}" ]]; do
24+
for file in "${names[@]}"; do
25+
if [[ -e "${candidate}/${file}" ]]; then
26+
echo "${candidate}"
27+
return
28+
fi
29+
done
30+
candidate="${candidate%/*}"
31+
done
32+
return 1
33+
}
34+
35+
function lint() {
36+
local files=()
37+
38+
while read -r f; do
39+
if has "${f}" "${TODO[@]}"; then
40+
continue
41+
fi
42+
43+
files+=("$f")
44+
done < <( find utils -name '*.sh'; ls -1 -- *.sh )
45+
46+
shellcheck "${files[@]}"
47+
}
48+
49+
function root() {
50+
walk "${PWD}" '.shellcheck' '.git'
51+
}
52+
53+
function load_config() {
54+
# defaults
55+
TODO=()
56+
57+
local root
58+
root="$(root)"
59+
60+
local config="${root}/.shellcheck"
61+
62+
if [[ -f "${config}" ]]; then
63+
# shellcheck disable=SC1090
64+
source "${config}"
65+
fi
66+
}
67+
68+
function main() {
69+
load_config
70+
lint
71+
}
72+
73+
if [[ "${0}" == "${BASH_SOURCE[0]}" ]]; then
74+
main "${@}"
75+
fi

0 commit comments

Comments
 (0)