Skip to content

Commit 2d7203b

Browse files
committed
fix(terraform_docs): apply yermulnik suggestions
1 parent a6bf17a commit 2d7203b

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

hooks/terraform_docs.sh

+13-13
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,17 @@ function terraform_docs {
143143

144144
else
145145
have_config_flag=true
146-
local config_file=${args#*--config}
147-
config_file=${config_file#*=}
148-
# If there are more parameters after config path, trim until --
149-
if [[ $config_file == *" --"* ]]; then
150-
config_file=${config_file%% --*}
151-
fi
152-
# Trim trailing whitespace but preserve internal spaces
153-
154-
config_file="${config_file%"${config_file##*[![:space:]]}"}" args=${args/--config=$config_file/}
146+
# Enable extended pattern matching operators
147+
shopt -qp extglob || EXTGLOB_IS_NOT_SET=true && shopt -s extglob
148+
# Trim any args before the `--config` arg value
149+
local config_file=${args##*--config(+([[:space:]])|=)}
150+
# Trim any trailing spaces and args (if any)
151+
config_file="${config_file%%+([[:space:]])?(--*)}"
152+
# Trim `--config` arg and its value from original args as we will
153+
# pass `--config` separately to allow whitespaces in its value
154+
args=${args/--config@(+([[:space:]])|=)$config_file*([[:space:]])/}
155+
# Restore state of `extglob` if we changed it
156+
[[ $EXTGLOB_IS_NOT_SET ]] && shopt -u extglob
155157

156158
# Prioritize `.terraform-docs.yml` `output.file` over
157159
# `--hook-config=--path-to-file=` if it set
@@ -237,10 +239,8 @@ function terraform_docs {
237239
have_marker=$(grep -o "$insertion_marker_begin" "$output_file") || unset have_marker
238240
[[ ! $have_marker ]] && popd > /dev/null && continue
239241
fi
240-
local config_options=""
241-
if [[ $have_config_flag == true ]]; then
242-
config_options="--config=$config_file"
243-
fi
242+
local config_options
243+
[[ $have_config_flag == true ]] && config_options="--config=$config_file"
244244
# shellcheck disable=SC2086
245245
terraform-docs --output-mode="$output_mode" --output-file="$output_file" $tf_docs_formatter "$config_options" $args ./ > /dev/null
246246

0 commit comments

Comments
 (0)