-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·67 lines (50 loc) · 1.85 KB
/
pre-commit
File metadata and controls
executable file
·67 lines (50 loc) · 1.85 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
#!/usr/bin/env bash
#
# Pre-commit hook to run lightweight checks and auto-format the code. It's designed
# to be blazingly fast, so it checks only changed files. Run the following command
# to install this hook for yourself. It's a symlink, to make sure it stays always
# up-to-date.
#
# ```bash
# ln -s ../../.githooks/pre-commit .git/hooks/pre-commit
# ```
set -euo pipefail
script_dir=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")
tf_modules=$("$script_dir/../.github/scripts/collect-modules.sh")
function command_exists() {
bin_name=$(basename "$1")
if command -v "$1" &> /dev/null; then
printf "\e[0;32m[INFO] Using %s...\e[0m\n" "$bin_name"
return 0
fi
printf "\e[0;33m[WARN] %s CLI was not found. Ignoring it...\e[0m\n" "$bin_name" >&2
return 1
}
files=$(git diff --cached --name-only --diff-filter=ACMR | sed 's| |\\ |g')
if command_exists typos; then
echo "$files" | xargs typos
fi
if command_exists terraform; then
# `terraform fmt` doesn't ignore non-tf files automatically
tf_files=$(echo "$files" | { grep -E '\.tf$' || true; })
echo "$tf_files" | xargs terraform fmt
fi
if command_exists terraform-docs; then
for tf_module in $tf_modules; do
terraform-docs markdown "$tf_module" --output-file "README.md"
# Extend `$files` to make `prettier` format the generated README.md
files="$files $tf_module/README.md"
done
fi
if command_exists npm; then
npm run codegen
files="$files $script_dir/../iam-policies/terraform/policies"
fi
if command_exists ./node_modules/.bin/prettier; then
echo "$files" | xargs ./node_modules/.bin/prettier --ignore-unknown --write
fi
# We don't have `tflint` in pre-commit hook because it doesn't ignore files
# ignored in `.gitignore`. More details: https://github.com/terraform-linters/tflint/issues/2283
# Add the modified/prettified files to staging
echo "$files" | xargs git add
exit 0