-
-
Notifications
You must be signed in to change notification settings - Fork 541
/
terrascan.sh
executable file
·95 lines (80 loc) · 2.29 KB
/
terrascan.sh
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
#!/usr/bin/env bash
set -eo pipefail
main() {
initialize_
parse_cmdline_ "$@"
terrascan_ "${ARGS[*]}" "${FILES[@]}"
}
terrascan_() {
local -r args="${1}"
shift 1
local -a -r files=("$@")
# consume modified files passed from pre-commit so that
# terrascan runs against only those relevant directories
for file_with_path in "${files[@]}"; do
file_with_path="${file_with_path// /__REPLACED__SPACE__}"
paths[index]=$(dirname "$file_with_path")
index=$((index + 1))
done
# allow terrascan to continue if exit_code is greater than 0
# preserve errexit status
shopt -qo errexit && ERREXIT_IS_SET=true
set +e
terrascan_final_exit_code=0
# for each path run terrascan
for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do
path_uniq="${path_uniq//__REPLACED__SPACE__/ }"
pushd "$path_uniq" > /dev/null
# pass the arguments to terrascan
# shellcheck disable=SC2086 # terrascan fails when quoting is used ("$arg" vs $arg)
terrascan scan -i terraform $args
local exit_code=$?
if [ $exit_code != 0 ]; then
terrascan_final_exit_code=$exit_code
fi
popd > /dev/null
done
# restore errexit if it was set before the "for" loop
[[ $ERREXIT_IS_SET ]] && set -e
# return the terrascan final exit_code
exit $terrascan_final_exit_code
}
initialize_() {
# get directory containing this script
local dir
local source
source="${BASH_SOURCE[0]}"
while [[ -L $source ]]; do # resolve $source until the file is no longer a symlink
dir="$(cd -P "$(dirname "$source")" > /dev/null && pwd)"
source="$(readlink "$source")"
# if $source was a relative symlink, we need to resolve it relative to the path where the symlink file was located
[[ $source != /* ]] && source="$dir/$source"
done
_SCRIPT_DIR="$(dirname "$source")"
# source getopt function
# shellcheck source=lib_getopt
. "$_SCRIPT_DIR/lib_getopt"
}
parse_cmdline_() {
declare argv
argv=$(getopt -o a: --long args: -- "$@") || return
eval "set -- $argv"
for argv; do
case $argv in
-a | --args)
shift
ARGS+=("$1")
shift
;;
--)
shift
FILES+=("$@")
break
;;
esac
done
}
# global arrays
declare -a ARGS=()
declare -a FILES=()
[[ ${BASH_SOURCE[0]} != "$0" ]] || main "$@"