-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathformat-hcl
executable file
·95 lines (87 loc) · 2.53 KB
/
format-hcl
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
# Simple wrapper script for both:
# - cytopia's terragrunt-fmt.sh script (https://github.com/cytopia/docker-terragrunt-fmt) for .hcl files
# - Terraform fmt for .tf and .tfvars
# Used for checking canonical form and style of HCL configuration files.
# And formatting them to such for if requested.
# Default action is to search recursively and modify all files.
RECURSIVE="-recursive"
WRITE="-write=true"
print_help() {
echo
echo "Usage: format-hcl [options] [DIR]"
echo " format-hcl --help"
echo
echo " Used for checking canonical form and style of HCL configuration files."
echo " And formatting them to such if requested."
echo
echo " When DIR is not specified current working directory is used."
echo
echo "Options:"
echo
echo " -list=true List files containing formatting inconsistencies."
echo
echo " -write=true Overwrite input files. Disabled if using -check."
echo
echo " -diff Display diffs of formatting changes"
echo
echo " -check Check if the input is formatted. Exit status will be 0 if all"
echo " input is properly formatted and non-zero otherwise."
echo
echo " -recursive Also process files in subdirectories. By default, only the"
echo " given directory (or current directory) is processed."
echo
echo " -ignore=a,b Comma separated list of paths to ignore. Only for .hcl files."
echo " The wildcard character '*' is supported."
}
for arg in "$@"; do
case $arg in
-help)
print_help
exit 0
;;
-list=*)
LIST="${arg#*}"
shift
;;
-write=*)
WRITE="${arg#*}"
shift
;;
-ignore=*)
IGNORE="${arg#*}"
shift
;;
-diff)
DIFF="-diff"
shift
;;
-check)
CHECK="-check"
shift
;;
-recursive)
RECURSIVE="-recursive"
shift
;;
esac
done
echo -e "\n=> Searching for .hcl files"
terragrunt-fmt.sh ${LIST} ${WRITE} ${DIFF} ${CHECK} ${RECURSIVE} ${IGNORE} "$@"
HCL_RET_CODE=$?
echo -e "\n=> Searching for .tf and .tfvars files"
if command -v terraform &> /dev/null; then
terraform fmt ${LIST} ${WRITE} ${DIFF} ${CHECK} ${RECURSIVE} "$@"
TF_RET_CODE=$?
elif command -v tofu &> /dev/null; then
tofu fmt ${LIST} ${WRITE} ${DIFF} ${CHECK} ${RECURSIVE} "$@"
TF_RET_CODE=$?
else
echo "Neither terraform nor opentofu (tofu) is installed."
exit 1
fi
if [[ "${HCL_RET_CODE}" != "0" ]] || [[ "${TF_RET_CODE}" != "0" ]]; then
exit 1
else
exit 0
fi