|
2 | 2 | set -eo pipefail |
3 | 3 |
|
4 | 4 | # globals variables |
5 | | -# hook ID, see `- id` for details in .pre-commit-hooks.yaml file |
6 | | -# shellcheck disable=SC2034 # Unused var. |
7 | | -readonly HOOK_ID='terraform_docs' |
8 | 5 | # shellcheck disable=SC2155 # No way to assign to readonly variable in separate lines |
9 | 6 | readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" |
10 | 7 | # shellcheck source=_common.sh |
@@ -90,7 +87,7 @@ function terraform_docs_ { |
90 | 87 | function terraform_docs { |
91 | 88 | local -r terraform_docs_awk_file="$1" |
92 | 89 | local -r hook_config="$2" |
93 | | - local -r args="$3" |
| 90 | + local args="$3" |
94 | 91 | shift 3 |
95 | 92 | local -a -r files=("$@") |
96 | 93 |
|
@@ -136,10 +133,29 @@ function terraform_docs { |
136 | 133 | esac |
137 | 134 | done |
138 | 135 |
|
139 | | - # |
140 | 136 | # Override formatter if no config file set |
141 | | - # |
142 | | - [[ "$args" != *"--config="* ]] && local tf_docs_formatter="md" |
| 137 | + if [[ "$args" != *"--config"* ]]; then |
| 138 | + local tf_docs_formatter="md" |
| 139 | + |
| 140 | + # Suppress terraform_docs color |
| 141 | + else |
| 142 | + |
| 143 | + local config_file=${args#*--config} |
| 144 | + config_file=${config_file#*=} |
| 145 | + config_file=${config_file% *} |
| 146 | + |
| 147 | + local config_file_no_color |
| 148 | + config_file_no_color="$config_file$(date +%s).yml" |
| 149 | + |
| 150 | + if [ "$PRE_COMMIT_COLOR" = "never" ] && |
| 151 | + [[ $(grep -e '^formatter:' "$config_file") == *"pretty"* ]] && |
| 152 | + [[ $(grep ' color: ' "$config_file") != *"false"* ]]; then |
| 153 | + |
| 154 | + cp "$config_file" "$config_file_no_color" |
| 155 | + echo -e "settings:\n color: false" >> "$config_file_no_color" |
| 156 | + args=${args/$config_file/$config_file_no_color} |
| 157 | + fi |
| 158 | + fi |
143 | 159 |
|
144 | 160 | local dir_path |
145 | 161 | for dir_path in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do |
@@ -212,6 +228,9 @@ function terraform_docs { |
212 | 228 |
|
213 | 229 | popd > /dev/null |
214 | 230 | done |
| 231 | + |
| 232 | + # Cleanup |
| 233 | + [ -e "$config_file_no_color" ] && rm -f "$config_file_no_color" |
215 | 234 | } |
216 | 235 |
|
217 | 236 | ####################################################################### |
|
0 commit comments