Skip to content

Commit 9d6a22b

Browse files
fix(terraform_docs): Fix issue and prioritize output.file setting from .terraform-docs.yml config over --hook-config=--path-to-file= (antonbabenko#698)
--------- Co-authored-by: MaxymVlasov <MaxymVlasov@users.noreply.github.com>
1 parent 8970461 commit 9d6a22b

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

hooks/terraform_docs.sh

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ function terraform_docs {
121121
# Get hook settings
122122
#
123123
local text_file="README.md"
124+
local use_path_to_file=false
124125
local add_to_existing=false
125126
local create_if_not_exist=false
126127
local use_standard_markers=false
@@ -136,6 +137,7 @@ function terraform_docs {
136137
case $key in
137138
--path-to-file)
138139
text_file=$value
140+
use_path_to_file=true
139141
;;
140142
--add-to-existing-file)
141143
add_to_existing=$value
@@ -159,13 +161,29 @@ function terraform_docs {
159161
if [[ "$args" != *"--config"* ]]; then
160162
local tf_docs_formatter="md"
161163

162-
# Suppress terraform_docs color
163164
else
164165

165166
local config_file=${args#*--config}
166167
config_file=${config_file#*=}
167168
config_file=${config_file% *}
168169

170+
# Prioritize `.terraform-docs.yml` `output.file` over
171+
# `--hook-config=--path-to-file=` if it set
172+
local output_file
173+
# Get latest non-commented `output.file` from `.terraform-docs.yml`
174+
output_file=$(grep -A1000 -e '^output:$' "$config_file" | grep -E '^[[:space:]]+file:' | tail -n 1) || true
175+
176+
if [[ $output_file ]]; then
177+
# Extract filename from `output.file` line
178+
text_file=$(echo "$output_file" | awk -F':' '{print $2}' | tr -d '[:space:]"' | tr -d "'")
179+
180+
if [[ $use_path_to_file ]]; then
181+
common::colorify "yellow" "NOTE: You set both '--hook-config=--path-to-file=' and 'output.file' in '$config_file'"
182+
common::colorify "yellow" " 'output.file' from '$config_file' will be used."
183+
fi
184+
fi
185+
186+
# Suppress terraform_docs color
169187
local config_file_no_color
170188
config_file_no_color="$config_file$(date +%s).yml"
171189

@@ -228,7 +246,7 @@ function terraform_docs {
228246

229247
if [[ "$terraform_docs_awk_file" == "0" ]]; then
230248
# shellcheck disable=SC2086
231-
terraform-docs $tf_docs_formatter $args ./ > "$tmp_file"
249+
terraform-docs --output-file="" $tf_docs_formatter $args ./ > "$tmp_file"
232250
else
233251
# Can't append extension for mktemp, so renaming instead
234252
local tmp_file_docs
@@ -239,7 +257,7 @@ function terraform_docs {
239257

240258
awk -f "$terraform_docs_awk_file" ./*.tf > "$tmp_file_docs_tf"
241259
# shellcheck disable=SC2086
242-
terraform-docs $tf_docs_formatter $args "$tmp_file_docs_tf" > "$tmp_file"
260+
terraform-docs --output-file="" $tf_docs_formatter $args "$tmp_file_docs_tf" > "$tmp_file"
243261
rm -f "$tmp_file_docs_tf"
244262
fi
245263

0 commit comments

Comments
 (0)