Skip to content

Commit 7b11401

Browse files
authored
feat: Add terraform_docs hook settings (antonbabenko#245)
1 parent 3f66432 commit 7b11401

File tree

2 files changed

+103
-19
lines changed

2 files changed

+103
-19
lines changed

README.md

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,33 @@ For [checkov](https://github.com/bridgecrewio/checkov) you need to specify each
225225
226226
2. It is possible to pass additional arguments to shell scripts when using `terraform_docs` and `terraform_docs_without_aggregate_type_defaults`. Send pull-request with the new hook if something is missing.
227227
228-
For these hooks, you need to specify all arguments as one:
228+
3. It is possible to automatically:
229+
* create docfile (and PATH to it)
230+
* extend exiting docs files, by appending markers to the end of file (see p.1)
231+
* use different than `README.md` docfile name.
229232
230-
```yaml
231-
- id: terraform_docs
232-
args:
233-
- tfvars hcl --output-file terraform.tfvars.model .
234-
```
233+
```yaml
234+
- id: terraform_docs
235+
args:
236+
- --hook-config=--path-to-file=README.md # Valid UNIX path. I.e. ../TFDOC.md or docs/README.md etc.
237+
- --hook-config=--add-to-exiting-file=true # Boolean. true or false
238+
- --hook-config=--create-file-if-not-exist=true # Boolean. true or false
239+
```
240+
241+
4. You can provide arguments to terraform_doc. Eg. for [configuration](https://github.com/terraform-docs/terraform-docs/blob/master/docs/user-guide/configuration.md#usage):
242+
243+
```yaml
244+
- id: terraform_docs
245+
args:
246+
- --args=--config=.terraform-docs.yml
247+
248+
5. If you need some exotic settings, it can be be done too. I.e. this one generates HCL files:
249+
250+
```yaml
251+
- id: terraform_docs
252+
args:
253+
- tfvars hcl --output-file terraform.tfvars.model .
254+
```
235255
236256
### terraform_docs_replace
237257

terraform_docs.sh

Lines changed: 77 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ main() {
66
parse_cmdline_ "$@"
77
# Support for setting relative PATH to .terraform-docs.yml config.
88
ARGS=${ARGS/--config=/--config=$(pwd)\/}
9-
terraform_docs_ "${ARGS[*]}" "${FILES[@]}"
9+
terraform_docs_ "${HOOK_CONFIG[*]}" "${ARGS[*]}" "${FILES[@]}"
1010
}
1111

1212
initialize_() {
@@ -29,7 +29,7 @@ initialize_() {
2929

3030
parse_cmdline_() {
3131
declare argv
32-
argv=$(getopt -o a: --long args: -- "$@") || return
32+
argv=$(getopt -o a: --long args:,hook-config: -- "$@") || return
3333
eval "set -- $argv"
3434

3535
for argv; do
@@ -39,6 +39,11 @@ parse_cmdline_() {
3939
ARGS+=("$1")
4040
shift
4141
;;
42+
--hook-config)
43+
shift
44+
HOOK_CONFIG+=("$1")
45+
shift
46+
;;
4247
--)
4348
shift
4449
FILES=("$@")
@@ -49,8 +54,9 @@ parse_cmdline_() {
4954
}
5055

5156
terraform_docs_() {
52-
local -r args="$1"
53-
shift
57+
local -r hook_config="$1"
58+
local -r args="$2"
59+
shift 2
5460
local -a -r files=("$@")
5561

5662
local hack_terraform_docs
@@ -66,7 +72,7 @@ terraform_docs_() {
6672

6773
if [[ -z "$is_old_terraform_docs" ]]; then # Using terraform-docs 0.8+ (preferred)
6874

69-
terraform_docs "0" "$args" "${files[@]}"
75+
terraform_docs "0" "$hook_config" "$args" "${files[@]}"
7076

7177
elif [[ "$hack_terraform_docs" == "1" ]]; then # Using awk script because terraform-docs is older than 0.8 and terraform 0.12 is used
7278

@@ -78,20 +84,21 @@ terraform_docs_() {
7884
local tmp_file_awk
7985
tmp_file_awk=$(mktemp "${TMPDIR:-/tmp}/terraform-docs-XXXXXXXXXX")
8086
terraform_docs_awk "$tmp_file_awk"
81-
terraform_docs "$tmp_file_awk" "$args" "${files[@]}"
87+
terraform_docs "$tmp_file_awk" "$hook_config" "$args" "${files[@]}"
8288
rm -f "$tmp_file_awk"
8389

8490
else # Using terraform 0.11 and no awk script is needed for that
8591

86-
terraform_docs "0" "$args" "${files[@]}"
92+
terraform_docs "0" "$hook_config" "$args" "${files[@]}"
8793

8894
fi
8995
}
9096

9197
terraform_docs() {
9298
local -r terraform_docs_awk_file="$1"
93-
local -r args="$2"
94-
shift 2
99+
local -r hook_config="$2"
100+
local -r args="$3"
101+
shift 3
95102
local -a -r files=("$@")
96103

97104
declare -a paths
@@ -107,17 +114,73 @@ terraform_docs() {
107114
done
108115

109116
local -r tmp_file=$(mktemp)
110-
local -r text_file="README.md"
117+
118+
#
119+
# Get hook settings
120+
#
121+
local text_file="README.md"
122+
local add_to_exiting=false
123+
local create_if_not_exist=false
124+
125+
configs=($hook_config)
126+
for c in "${configs[@]}"; do
127+
config=(${c//=/ })
128+
key=${config[0]}
129+
value=${config[1]}
130+
131+
case $key in
132+
--path-to-file)
133+
text_file=$value
134+
;;
135+
--add-to-exiting-file)
136+
add_to_exiting=$value
137+
;;
138+
--create-file-if-not-exist)
139+
create_if_not_exist=$value
140+
;;
141+
esac
142+
done
111143

112144
local path_uniq
113145
for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do
114146
path_uniq="${path_uniq//__REPLACED__SPACE__/ }"
115147

116148
pushd "$path_uniq" > /dev/null
117149

118-
if [[ ! -f "$text_file" ]]; then
119-
popd > /dev/null
120-
continue
150+
#
151+
# Create file if it not exist and `--create-if-not-exist=true` provided
152+
#
153+
if $create_if_not_exist && [[ ! -f "$text_file" ]]; then
154+
dir_have_tf_files="$(
155+
find . -maxdepth 1 -type f | sed 's|.*\.||' | sort -u | grep -oE '^tf$|^tfvars$' ||
156+
exit 0
157+
)"
158+
159+
# if no TF files - skip dir
160+
[ ! "$dir_have_tf_files" ] && popd > /dev/null && continue
161+
162+
dir="$(dirname "$text_file")"
163+
164+
mkdir -p "$dir"
165+
echo -e "# ${PWD##*/}\n" >> "$text_file"
166+
echo "<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->" >> "$text_file"
167+
echo "<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->" >> "$text_file"
168+
fi
169+
170+
# If file still not exist - skip dir
171+
[[ ! -f "$text_file" ]] && popd > /dev/null && continue
172+
173+
#
174+
# If `--add-to-exiting-file=true` set, check is in file exist "hook markers",
175+
# and if not - append "hook markers" to the end of file.
176+
#
177+
if $add_to_exiting; then
178+
HAVE_MARKER=$(grep -o '<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->' "$text_file" || exit 0)
179+
180+
if [ ! "$HAVE_MARKER" ]; then
181+
echo "<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->" >> "$text_file"
182+
echo "<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->" >> "$text_file"
183+
fi
121184
fi
122185

123186
if [[ "$terraform_docs_awk_file" == "0" ]]; then
@@ -311,5 +374,6 @@ EOF
311374
# global arrays
312375
declare -a ARGS=()
313376
declare -a FILES=()
377+
declare -a HOOK_CONFIG=()
314378

315379
[[ ${BASH_SOURCE[0]} != "$0" ]] || main "$@"

0 commit comments

Comments
 (0)