forked from antonbabenko/pre-commit-terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
terraform_tflint.sh
executable file
·81 lines (66 loc) · 1.78 KB
/
terraform_tflint.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
#!/usr/bin/env bash
set -eo pipefail
main() {
initialize_
parse_cmdline_ "$@"
tflint_
}
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
expanded_arg="${1//__GIT_WORKING_DIR__/$PWD}"
ARGS+=("$expanded_arg")
shift
;;
--)
shift
FILES=("$@")
break
;;
esac
done
}
tflint_() {
local index=0
for file_with_path in "${FILES[@]}"; do
file_with_path="${file_with_path// /__REPLACED__SPACE__}"
paths[index]=$(dirname "$file_with_path")
((index += 1))
done
for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do
path_uniq="${path_uniq//__REPLACED__SPACE__/ }"
pushd "$path_uniq" > /dev/null
# Print checked PATH **only** if TFLint have any messages
# shellcheck disable=SC2091 # Suppress error output
$(tflint "${ARGS[@]}" 2>&1) 2> /dev/null || {
echo >&2 -e "\033[1;31m\nERROR in $path_uniq/:\033[0m"
tflint "${ARGS[@]}"
}
popd > /dev/null
done
}
# global arrays
declare -a ARGS
declare -a FILES
[[ ${BASH_SOURCE[0]} != "$0" ]] || main "$@"