Skip to content

Commit 1a1b4a3

Browse files
authored
feat(terraform-docs): Add support for replace mode for TF 0.12+; Use native saving to file for TF 0.12+. Both requires terraform-docs v0.12.0+ which released in 2021. (antonbabenko#705)
1 parent 9e4a82b commit 1a1b4a3

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

hooks/terraform_docs.sh

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,11 @@ function terraform_docs {
130130
((index += 1))
131131
done
132132

133-
local -r tmp_file=$(mktemp)
134-
135133
#
136134
# Get hook settings
137135
#
138136
local output_file="README.md"
137+
local output_mode="inject"
139138
local use_path_to_file=false
140139
local add_to_existing=false
141140
local create_if_not_exist=false
@@ -202,6 +201,14 @@ function terraform_docs {
202201
output_file=$config_output_file
203202
fi
204203

204+
# Use `.terraform-docs.yml` `output.mode` if it set
205+
local config_output_mode
206+
config_output_mode=$(grep -A1000 -e '^output:$' "$config_file" | grep -E '^[[:space:]]+mode:' | tail -n 1) || true
207+
if [[ $config_output_mode ]]; then
208+
# Extract mode from `output.mode` line
209+
output_mode=$(echo "$config_output_mode" | awk -F':' '{print $2}' | tr -d '[:space:]"' | tr -d "'")
210+
fi
211+
205212
# Suppress terraform_docs color
206213
local config_file_no_color
207214
config_file_no_color="$config_file$(date +%s).yml"
@@ -267,7 +274,7 @@ function terraform_docs {
267274

268275
if [[ "$terraform_docs_awk_file" == "0" ]]; then
269276
# shellcheck disable=SC2086
270-
terraform-docs --output-file="" $tf_docs_formatter $args ./ > "$tmp_file"
277+
terraform-docs --output-mode="$output_mode" --output-file="$output_file" $tf_docs_formatter $args ./ > /dev/null
271278
else
272279
# Can't append extension for mktemp, so renaming instead
273280
local tmp_file_docs
@@ -277,20 +284,22 @@ function terraform_docs {
277284
tmp_file_docs_tf="$tmp_file_docs.tf"
278285

279286
awk -f "$terraform_docs_awk_file" ./*.tf > "$tmp_file_docs_tf"
287+
288+
local -r tmp_file=$(mktemp)
280289
# shellcheck disable=SC2086
281290
terraform-docs --output-file="" $tf_docs_formatter $args "$tmp_file_docs_tf" > "$tmp_file"
282291
rm -f "$tmp_file_docs_tf"
283-
fi
284292

285-
# Use of insertion markers to insert the terraform-docs output between the markers
286-
# Replace content between markers with the placeholder - https://stackoverflow.com/questions/1212799/how-do-i-extract-lines-between-two-line-delimiters-in-perl#1212834
287-
perl_expression="if (/$insertion_marker_begin/../$insertion_marker_end/) { print \$_ if /$insertion_marker_begin/; print \"I_WANT_TO_BE_REPLACED\\n\$_\" if /$insertion_marker_end/;} else { print \$_ }"
288-
perl -i -ne "$perl_expression" "$output_file"
293+
# Use of insertion markers to insert the terraform-docs output between the markers
294+
# Replace content between markers with the placeholder - https://stackoverflow.com/questions/1212799/how-do-i-extract-lines-between-two-line-delimiters-in-perl#1212834
295+
perl_expression="if (/$insertion_marker_begin/../$insertion_marker_end/) { print \$_ if /$insertion_marker_begin/; print \"I_WANT_TO_BE_REPLACED\\n\$_\" if /$insertion_marker_end/;} else { print \$_ }"
296+
perl -i -ne "$perl_expression" "$output_file"
289297

290-
# Replace placeholder with the content of the file
291-
perl -i -e 'open(F, "'"$tmp_file"'"); $f = join "", <F>; while(<>){if (/I_WANT_TO_BE_REPLACED/) {print $f} else {print $_};}' "$output_file"
298+
# Replace placeholder with the content of the file
299+
perl -i -e 'open(F, "'"$tmp_file"'"); $f = join "", <F>; while(<>){if (/I_WANT_TO_BE_REPLACED/) {print $f} else {print $_};}' "$output_file"
292300

293-
rm -f "$tmp_file"
301+
rm -f "$tmp_file"
302+
fi
294303

295304
popd > /dev/null
296305
done

0 commit comments

Comments
 (0)