|
1 | 1 | #!/usr/bin/env bash |
2 | 2 | set -eo pipefail |
| 3 | + |
3 | 4 | # globals variables |
4 | 5 | SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" |
5 | 6 | readonly SCRIPT_DIR |
@@ -143,15 +144,17 @@ function terraform_docs { |
143 | 144 |
|
144 | 145 | else |
145 | 146 | 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/} |
| 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 |
155 | 158 |
|
156 | 159 | # Prioritize `.terraform-docs.yml` `output.file` over |
157 | 160 | # `--hook-config=--path-to-file=` if it set |
@@ -237,10 +240,8 @@ function terraform_docs { |
237 | 240 | have_marker=$(grep -o "$insertion_marker_begin" "$output_file") || unset have_marker |
238 | 241 | [[ ! $have_marker ]] && popd > /dev/null && continue |
239 | 242 | fi |
240 | | - local config_options="" |
241 | | - if [[ $have_config_flag == true ]]; then |
242 | | - config_options="--config=$config_file" |
243 | | - fi |
| 243 | + local config_options |
| 244 | + [[ $have_config_flag == true ]] && config_options="--config=$config_file" |
244 | 245 | # shellcheck disable=SC2086 |
245 | 246 | terraform-docs --output-mode="$output_mode" --output-file="$output_file" $tf_docs_formatter "$config_options" $args ./ > /dev/null |
246 | 247 |
|
|
0 commit comments