forked from nhs-england-tools/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathshellscript-linter.sh
More file actions
executable file
·77 lines (58 loc) · 2.4 KB
/
Copy pathshellscript-linter.sh
File metadata and controls
executable file
·77 lines (58 loc) · 2.4 KB
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
#!/bin/bash
# WARNING: Please DO NOT edit this file! It is maintained in the Repository Template (https://github.com/nhs-england-tools/repository-template). Raise a PR instead.
set -euo pipefail
# ShellCheck command wrapper. It will run ShellCheck natively if it is
# installed, otherwise it will run it in a Docker container.
#
# Usage:
# $ [options] ./shellscript-linter.sh
#
# Arguments (provided as environment variables):
# file=shellscript # Path to the shell script to lint, relative to the project's top-level directory, default is itself
# FORCE_USE_DOCKER=true # If set to true the command is run in a Docker container, default is 'false'
# VERBOSE=true # Show all the executed commands, default is 'false'
# ==============================================================================
function main() {
cd "$(git rev-parse --show-toplevel)"
[ -z "${file:-}" ] && echo "WARNING: 'file' variable not set, defaulting to itself"
local file=${file:-scripts/shellscript-linter.sh}
if command -v shellcheck > /dev/null 2>&1 && ! is-arg-true "${FORCE_USE_DOCKER:-false}"; then
file="$file" run-shellcheck-natively
else
file="$file" run-shellcheck-in-docker
fi
}
# Run ShellCheck natively.
# Arguments (provided as environment variables):
# file=[path to the shell script to lint, relative to the project's top-level directory]
function run-shellcheck-natively() {
# shellcheck disable=SC2001
shellcheck "$(echo "$file" | sed "s#$PWD#.#")"
}
# Run ShellCheck in a Docker container.
# Arguments (provided as environment variables):
# file=[path to the shell script to lint, relative to the project's top-level directory]
function run-shellcheck-in-docker() {
# shellcheck disable=SC1091
source ./scripts/docker/docker.lib.sh
# shellcheck disable=SC2155
local image=$(name=koalaman/shellcheck docker-get-image-version-and-pull)
# shellcheck disable=SC2001
docker run --rm --platform linux/amd64 \
--volume "$PWD:/workdir" \
--workdir /workdir \
"$image" \
"/workdir/$(echo "$file" | sed "s#$PWD#.#")"
}
# ==============================================================================
function is-arg-true() {
if [[ "$1" =~ ^(true|yes|y|on|1|TRUE|YES|Y|ON)$ ]]; then
return 0
else
return 1
fi
}
# ==============================================================================
is-arg-true "${VERBOSE:-false}" && set -x
main "$@"
exit 0