Skip to content

Commit 64b81f4

Browse files
authored
fix(terraform_docs): Fix bug introduced in v1.97.2 (antonbabenko#801)
* Fix bug introduced via antonbabenko#796 by passing config file only when it is defined * While here make array declarations in `common::parse_cmdline` in `hooks/_common.sh` compliant with Bash v3 * While here suppress error outputs from `grep` for non-existing config file in `hooks/terraform_docs.sh` where error output makes no sense
1 parent 03d6270 commit 64b81f4

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

hooks/_common.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ function common::initialize {
5252
function common::parse_cmdline {
5353
# common global arrays.
5454
# Populated via `common::parse_cmdline` and can be used inside hooks' functions
55-
ARGS=() HOOK_CONFIG=() FILES=()
55+
ARGS=()
56+
HOOK_CONFIG=()
57+
FILES=()
5658
# Used inside `common::terraform_init` function
5759
TF_INIT_ARGS=()
5860
# Used inside `common::export_provided_env_vars` function

hooks/terraform_docs.sh

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ function terraform_docs {
160160
# `--hook-config=--path-to-file=` if it set
161161
local config_output_file
162162
# Get latest non-commented `output.file` from `.terraform-docs.yml`
163-
config_output_file=$(grep -A1000 -e '^output:$' "$config_file" | grep -E '^[[:space:]]+file:' | tail -n 1) || true
163+
config_output_file=$(grep -A1000 -e '^output:$' "$config_file" 2> /dev/null | grep -E '^[[:space:]]+file:' | tail -n 1) || true
164164

165165
if [[ $config_output_file ]]; then
166166
# Extract filename from `output.file` line
@@ -176,7 +176,7 @@ function terraform_docs {
176176

177177
# Use `.terraform-docs.yml` `output.mode` if it set
178178
local config_output_mode
179-
config_output_mode=$(grep -A1000 -e '^output:$' "$config_file" | grep -E '^[[:space:]]+mode:' | tail -n 1) || true
179+
config_output_mode=$(grep -A1000 -e '^output:$' "$config_file" 2> /dev/null | grep -E '^[[:space:]]+mode:' | tail -n 1) || true
180180
if [[ $config_output_mode ]]; then
181181
# Extract mode from `output.mode` line
182182
output_mode=$(echo "$config_output_mode" | awk -F':' '{print $2}' | tr -d '[:space:]"' | tr -d "'")
@@ -240,10 +240,21 @@ function terraform_docs {
240240
have_marker=$(grep -o "$insertion_marker_begin" "$output_file") || unset have_marker
241241
[[ ! $have_marker ]] && popd > /dev/null && continue
242242
fi
243-
local config_options
244-
[[ $have_config_flag == true ]] && config_options="--config=$config_file"
245-
# shellcheck disable=SC2086
246-
terraform-docs --output-mode="$output_mode" --output-file="$output_file" $tf_docs_formatter "$config_options" $args ./ > /dev/null
243+
244+
# shellcheck disable=SC2206
245+
# Need to pass $tf_docs_formatter and $args as separate arguments, not as single string
246+
local tfdocs_cmd=(
247+
terraform-docs
248+
--output-mode="$output_mode"
249+
--output-file="$output_file"
250+
$tf_docs_formatter
251+
$args
252+
)
253+
if [[ $have_config_flag == true ]]; then
254+
"${tfdocs_cmd[@]}" "--config=$config_file" ./ > /dev/null
255+
else
256+
"${tfdocs_cmd[@]}" ./ > /dev/null
257+
fi
247258

248259
popd > /dev/null
249260
done

0 commit comments

Comments
 (0)