Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add terraform_docs hook settings #245

Merged
merged 3 commits into from
Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,33 @@ For [checkov](https://github.com/bridgecrewio/checkov) you need to specify each

2. It is possible to pass additional arguments to shell scripts when using `terraform_docs` and `terraform_docs_without_aggregate_type_defaults`. Send pull-request with the new hook if something is missing.

For these hooks, you need to specify all arguments as one:
3. It is possible to automatically:
* create docfile (and PATH to it)
* extend exiting docs files, by appending markers to the end of file (see p.1)
* use different than `README.md` docfile name.

```yaml
- id: terraform_docs
args:
- tfvars hcl --output-file terraform.tfvars.model .
```
```yaml
- id: terraform_docs
args:
- --hook-config=--path-to-file=README.md # Valid UNIX path. I.e. ../TFDOC.md or docs/README.md etc.
- --hook-config=--add-to-exiting-file=true # Boolean. true or false
- --hook-config=--create-file-if-not-exist=true # Boolean. true or false
```

4. You can provide arguments to terraform_doc. Eg. for [configuration](https://github.com/terraform-docs/terraform-docs/blob/master/docs/user-guide/configuration.md#usage):

```yaml
- id: terraform_docs
args:
- --args=--config=.terraform-docs.yml

5. If you need some exotic settings, it can be be done too. I.e. this one generates HCL files:

```yaml
- id: terraform_docs
args:
- tfvars hcl --output-file terraform.tfvars.model .
```

### terraform_docs_replace

Expand Down
90 changes: 77 additions & 13 deletions terraform_docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ main() {
parse_cmdline_ "$@"
# Support for setting relative PATH to .terraform-docs.yml config.
ARGS=${ARGS/--config=/--config=$(pwd)\/}
terraform_docs_ "${ARGS[*]}" "${FILES[@]}"
terraform_docs_ "${HOOK_CONFIG[*]}" "${ARGS[*]}" "${FILES[@]}"
}

initialize_() {
Expand All @@ -29,7 +29,7 @@ initialize_() {

parse_cmdline_() {
declare argv
argv=$(getopt -o a: --long args: -- "$@") || return
argv=$(getopt -o a: --long args:,hook-config: -- "$@") || return
eval "set -- $argv"

for argv; do
Expand All @@ -39,6 +39,11 @@ parse_cmdline_() {
ARGS+=("$1")
shift
;;
--hook-config)
shift
HOOK_CONFIG+=("$1")
shift
;;
--)
shift
FILES=("$@")
Expand All @@ -49,8 +54,9 @@ parse_cmdline_() {
}

terraform_docs_() {
local -r args="$1"
shift
local -r hook_config="$1"
local -r args="$2"
shift 2
local -a -r files=("$@")

local hack_terraform_docs
Expand All @@ -66,7 +72,7 @@ terraform_docs_() {

if [[ -z "$is_old_terraform_docs" ]]; then # Using terraform-docs 0.8+ (preferred)

terraform_docs "0" "$args" "${files[@]}"
terraform_docs "0" "$hook_config" "$args" "${files[@]}"

elif [[ "$hack_terraform_docs" == "1" ]]; then # Using awk script because terraform-docs is older than 0.8 and terraform 0.12 is used

Expand All @@ -78,20 +84,21 @@ terraform_docs_() {
local tmp_file_awk
tmp_file_awk=$(mktemp "${TMPDIR:-/tmp}/terraform-docs-XXXXXXXXXX")
terraform_docs_awk "$tmp_file_awk"
terraform_docs "$tmp_file_awk" "$args" "${files[@]}"
terraform_docs "$tmp_file_awk" "$hook_config" "$args" "${files[@]}"
rm -f "$tmp_file_awk"

else # Using terraform 0.11 and no awk script is needed for that

terraform_docs "0" "$args" "${files[@]}"
terraform_docs "0" "$hook_config" "$args" "${files[@]}"

fi
}

terraform_docs() {
local -r terraform_docs_awk_file="$1"
local -r args="$2"
shift 2
local -r hook_config="$2"
local -r args="$3"
shift 3
local -a -r files=("$@")

declare -a paths
Expand All @@ -107,17 +114,73 @@ terraform_docs() {
done

local -r tmp_file=$(mktemp)
local -r text_file="README.md"

#
# Get hook settings
#
local text_file="README.md"
local add_to_exiting=false
local create_if_not_exist=false

configs=($hook_config)
for c in "${configs[@]}"; do
config=(${c//=/ })
key=${config[0]}
value=${config[1]}

case $key in
--path-to-file)
text_file=$value
;;
--add-to-exiting-file)
add_to_exiting=$value
;;
--create-file-if-not-exist)
create_if_not_exist=$value
;;
esac
done

local path_uniq
for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do
path_uniq="${path_uniq//__REPLACED__SPACE__/ }"

pushd "$path_uniq" > /dev/null

if [[ ! -f "$text_file" ]]; then
popd > /dev/null
continue
#
# Create file if it not exist and `--create-if-not-exist=true` provided
#
if $create_if_not_exist && [[ ! -f "$text_file" ]]; then
dir_have_tf_files="$(
find . -maxdepth 1 -type f | sed 's|.*\.||' | sort -u | grep -oE '^tf$|^tfvars$' ||
exit 0
)"

# if no TF files - skip dir
[ ! "$dir_have_tf_files" ] && popd > /dev/null && continue

dir="$(dirname "$text_file")"

mkdir -p "$dir"
echo -e "# ${PWD##*/}\n" >> "$text_file"
echo "<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->" >> "$text_file"
echo "<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->" >> "$text_file"
fi

# If file still not exist - skip dir
[[ ! -f "$text_file" ]] && popd > /dev/null && continue

#
# If `--add-to-exiting-file=true` set, check is in file exist "hook markers",
# and if not - append "hook markers" to the end of file.
#
if $add_to_exiting; then
HAVE_MARKER=$(grep -o '<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->' "$text_file" || exit 0)

if [ ! "$HAVE_MARKER" ]; then
echo "<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->" >> "$text_file"
echo "<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->" >> "$text_file"
fi
fi

if [[ "$terraform_docs_awk_file" == "0" ]]; then
Expand Down Expand Up @@ -311,5 +374,6 @@ EOF
# global arrays
declare -a ARGS=()
declare -a FILES=()
declare -a HOOK_CONFIG=()

[[ ${BASH_SOURCE[0]} != "$0" ]] || main "$@"