Skip to content

Commit 7d83911

Browse files
authored
fix(terraform_docs): Allow having whitespaces in path to .terraform-docs.yaml config file (#796)
1 parent 1d4507a commit 7d83911

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

hooks/terraform_docs.sh

+16-5
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ function terraform_docs {
8888
local add_to_existing=false
8989
local create_if_not_exist=false
9090
local use_standard_markers=true
91+
local have_config_flag=false
9192

9293
IFS=";" read -r -a configs <<< "$hook_config"
9394

@@ -142,10 +143,18 @@ function terraform_docs {
142143
local tf_docs_formatter="md"
143144

144145
else
145-
146-
local config_file=${args#*--config}
147-
config_file=${config_file#*=}
148-
config_file=${config_file% *}
146+
have_config_flag=true
147+
# Enable extended pattern matching operators
148+
shopt -qp extglob || EXTGLOB_IS_NOT_SET=true && shopt -s extglob
149+
# Trim any args before the `--config` arg value
150+
local config_file=${args##*--config@(+([[:space:]])|=)}
151+
# Trim any trailing spaces and args (if any)
152+
config_file="${config_file%%+([[:space:]])?(--*)}"
153+
# Trim `--config` arg and its value from original args as we will
154+
# pass `--config` separately to allow whitespaces in its value
155+
args=${args/--config@(+([[:space:]])|=)$config_file*([[:space:]])/}
156+
# Restore state of `extglob` if we changed it
157+
[[ $EXTGLOB_IS_NOT_SET ]] && shopt -u extglob
149158

150159
# Prioritize `.terraform-docs.yml` `output.file` over
151160
# `--hook-config=--path-to-file=` if it set
@@ -231,8 +240,10 @@ function terraform_docs {
231240
have_marker=$(grep -o "$insertion_marker_begin" "$output_file") || unset have_marker
232241
[[ ! $have_marker ]] && popd > /dev/null && continue
233242
fi
243+
local config_options
244+
[[ $have_config_flag == true ]] && config_options="--config=$config_file"
234245
# shellcheck disable=SC2086
235-
terraform-docs --output-mode="$output_mode" --output-file="$output_file" $tf_docs_formatter $args ./ > /dev/null
246+
terraform-docs --output-mode="$output_mode" --output-file="$output_file" $tf_docs_formatter "$config_options" $args ./ > /dev/null
236247

237248
popd > /dev/null
238249
done

0 commit comments

Comments
 (0)