Skip to content

Commit

Permalink
fix(terraform_docs): Fix bug introduced in v1.97.2
Browse files Browse the repository at this point in the history
* Fix bug introduced via #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
  • Loading branch information
yermulnik committed Feb 3, 2025
1 parent 03d6270 commit b0573e7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
4 changes: 3 additions & 1 deletion hooks/_common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ function common::initialize {
function common::parse_cmdline {
# common global arrays.
# Populated via `common::parse_cmdline` and can be used inside hooks' functions
ARGS=() HOOK_CONFIG=() FILES=()
ARGS=()
HOOK_CONFIG=()
FILES=()
# Used inside `common::terraform_init` function
TF_INIT_ARGS=()
# Used inside `common::export_provided_env_vars` function
Expand Down
21 changes: 15 additions & 6 deletions hooks/terraform_docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ function terraform_docs {
# `--hook-config=--path-to-file=` if it set
local config_output_file
# Get latest non-commented `output.file` from `.terraform-docs.yml`
config_output_file=$(grep -A1000 -e '^output:$' "$config_file" | grep -E '^[[:space:]]+file:' | tail -n 1) || true
config_output_file=$(grep -A1000 -e '^output:$' "$config_file" 2> /dev/null | grep -E '^[[:space:]]+file:' | tail -n 1) || true

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

# Use `.terraform-docs.yml` `output.mode` if it set
local config_output_mode
config_output_mode=$(grep -A1000 -e '^output:$' "$config_file" | grep -E '^[[:space:]]+mode:' | tail -n 1) || true
config_output_mode=$(grep -A1000 -e '^output:$' "$config_file" 2> /dev/null | grep -E '^[[:space:]]+mode:' | tail -n 1) || true
if [[ $config_output_mode ]]; then
# Extract mode from `output.mode` line
output_mode=$(echo "$config_output_mode" | awk -F':' '{print $2}' | tr -d '[:space:]"' | tr -d "'")
Expand Down Expand Up @@ -240,10 +240,19 @@ function terraform_docs {
have_marker=$(grep -o "$insertion_marker_begin" "$output_file") || unset have_marker
[[ ! $have_marker ]] && popd > /dev/null && continue
fi
local config_options
[[ $have_config_flag == true ]] && config_options="--config=$config_file"
# shellcheck disable=SC2086
terraform-docs --output-mode="$output_mode" --output-file="$output_file" $tf_docs_formatter "$config_options" $args ./ > /dev/null

local tfdocs_cmd=(
terraform-docs
--output-mode="$output_mode"
--output-file="$output_file"
$tf_docs_formatter
$args
)
if [[ $have_config_flag == true ]]; then
${tfdocs_cmd[@]} "--config=$config_file" ./ > /dev/null
else
${tfdocs_cmd[@]} ./ > /dev/null
fi

popd > /dev/null
done
Expand Down

0 comments on commit b0573e7

Please sign in to comment.