2018-05-16 20:04:48 +02:00
#!/usr/bin/env bash
2020-08-27 20:55:28 +01:00
set -eo pipefail
2018-05-16 20:04:48 +02:00
2022-02-10 16:53:37 +01:00
# globals variables
2022-01-06 17:09:51 +02:00
# shellcheck disable=SC2155 # No way to assign to readonly variable in separate lines
2022-04-18 19:17:15 +03:00
readonly SCRIPT_DIR = " $( cd " $( dirname " ${ BASH_SOURCE [0] } " ) " && pwd -P) "
2022-01-06 17:09:51 +02:00
# shellcheck source=_common.sh
. " $SCRIPT_DIR /_common.sh "
2023-12-21 20:00:20 +00:00
# set up default insertion markers. These will be changed to the markers used by
# terraform-docs if the hook config contains `--use-standard-markers=true`
2024-01-17 00:44:43 +01:00
insertion_marker_begin = "<!-- BEGINNING OF PRE-COMMIT-OPENTOFU DOCS HOOK -->"
insertion_marker_end = "<!-- END OF PRE-COMMIT-OPENTOFU DOCS HOOK -->"
2023-12-21 20:00:20 +00:00
# these are the standard insertion markers used by terraform-docs
readonly standard_insertion_marker_begin = "<!-- BEGIN_TF_DOCS -->"
readonly standard_insertion_marker_end = "<!-- END_TF_DOCS -->"
2022-01-06 13:21:52 +02:00
function main {
2022-01-06 17:09:51 +02:00
common::initialize " $SCRIPT_DIR "
2022-01-06 13:21:52 +02:00
common::parse_cmdline " $@ "
2022-07-06 15:41:28 +03:00
common::export_provided_env_vars " ${ ENV_VARS [@] } "
2022-04-26 13:33:58 +03:00
common::parse_and_export_env_vars
2021-10-15 15:19:41 +03:00
# Support for setting relative PATH to .terraform-docs.yml config.
2022-10-06 19:16:29 +03:00
for i in " ${ !ARGS[@] } " ; do
ARGS[ i] = ${ ARGS [i]/--config=/--config= $( pwd ) \/ }
done
2022-01-06 17:09:51 +02:00
# shellcheck disable=SC2153 # False positive
2024-03-08 09:23:05 +00:00
tofu_check_ " ${ HOOK_CONFIG [*] } " " ${ ARGS [*] } " " ${ FILES [@] } "
2020-08-27 20:55:28 +01:00
}
2022-01-11 15:54:42 +02:00
#######################################################################
2024-03-08 09:23:05 +00:00
# TODO Function which checks `terraform-docs` exists
2022-01-11 15:54:42 +02:00
# Arguments:
# hook_config (string with array) arguments that configure hook behavior
# args (string with array) arguments that configure wrapped tool behavior
# files (array) filenames to check
#######################################################################
2024-03-08 09:23:05 +00:00
function tofu_check_ {
2021-10-15 15:26:23 +03:00
local -r hook_config = " $1 "
local -r args = " $2 "
shift 2
2020-09-07 14:50:57 +01:00
local -a -r files = ( " $@ " )
2018-11-13 12:30:06 +01:00
2022-01-06 13:21:52 +02:00
# Get hook settings
IFS = ";" read -r -a configs <<< " $hook_config "
2020-01-21 05:19:46 -05:00
if [ [ ! $( command -v terraform-docs) ] ] ; then
2024-03-08 09:23:05 +00:00
echo "ERROR: terraform-docs is required by tofu_docs pre-commit hook but is not installed or in the system's PATH."
2020-01-21 05:19:46 -05:00
exit 1
fi
2024-03-08 09:23:05 +00:00
tofu_docs " ${ configs [*] } " " ${ args [*] } " " ${ files [@] } "
2018-11-13 12:30:06 +01:00
}
2022-01-11 15:54:42 +02:00
#######################################################################
# Wrapper around `terraform-docs` tool that check and change/create
2024-01-17 00:48:28 +01:00
# (depends on provided hook_config) OpenTofu documentation in
2022-01-11 15:54:42 +02:00
# markdown format
# Arguments:
# hook_config (string with array) arguments that configure hook behavior
# args (string with array) arguments that configure wrapped tool behavior
# files (array) filenames to check
#######################################################################
2024-01-17 00:48:28 +01:00
function tofu_docs {
2024-03-08 09:23:05 +00:00
local -r hook_config = " $1 "
local -r args = " $2 "
shift 2
2020-09-07 14:50:57 +01:00
local -a -r files = ( " $@ " )
2018-11-13 12:30:06 +01:00
2022-10-06 19:16:29 +03:00
local -a paths
2018-11-13 12:30:06 +01:00
2020-08-27 20:55:28 +01:00
local index = 0
local file_with_path
2020-09-07 14:50:57 +01:00
for file_with_path in " ${ files [@] } " ; do
2018-11-13 12:30:06 +01:00
file_with_path = " ${ file_with_path // /__REPLACED__SPACE__ } "
2018-05-16 20:04:48 +02:00
2018-11-13 12:30:06 +01:00
paths[ index] = $( dirname " $file_with_path " )
2018-05-16 20:04:48 +02:00
2020-01-21 11:54:13 +01:00
( ( index += 1) )
2018-11-13 12:30:06 +01:00
done
2018-05-16 20:04:48 +02:00
2020-08-27 20:55:28 +01:00
local -r tmp_file = $( mktemp)
2021-10-15 15:26:23 +03:00
#
# Get hook settings
#
local text_file = "README.md"
2021-11-17 19:16:38 +01:00
local add_to_existing = false
2021-10-15 15:26:23 +03:00
local create_if_not_exist = false
2023-12-21 20:00:20 +00:00
local use_standard_markers = false
2021-10-15 15:26:23 +03:00
2022-01-06 15:08:18 +02:00
read -r -a configs <<< " $hook_config "
2021-10-15 15:26:23 +03:00
for c in " ${ configs [@] } " ; do
2022-01-06 15:08:18 +02:00
IFS = "=" read -r -a config <<< " $c "
2021-10-15 15:26:23 +03:00
key = ${ config [0] }
value = ${ config [1] }
case $key in
--path-to-file)
text_file = $value
; ;
2021-11-17 19:16:38 +01:00
--add-to-existing-file)
add_to_existing = $value
2021-10-15 15:26:23 +03:00
; ;
--create-file-if-not-exist)
create_if_not_exist = $value
; ;
2023-12-21 20:00:20 +00:00
--use-standard-markers)
use_standard_markers = $value
; ;
2021-10-15 15:26:23 +03:00
esac
done
2018-05-16 20:04:48 +02:00
2023-12-21 20:00:20 +00:00
if [ " $use_standard_markers " = true ] ; then
# update the insertion markers to those used by terraform-docs
insertion_marker_begin = " $standard_insertion_marker_begin "
insertion_marker_end = " $standard_insertion_marker_end "
fi
2022-05-25 13:31:24 +01:00
# Override formatter if no config file set
2022-07-06 15:34:13 +03:00
if [ [ " $args " != *"--config" * ] ] ; then
local tf_docs_formatter = "md"
# Suppress terraform_docs color
else
local config_file = ${ args #*--config }
config_file = ${ config_file #*= }
config_file = ${ config_file % * }
local config_file_no_color
config_file_no_color = " $config_file $( date +%s) .yml "
if [ " $PRE_COMMIT_COLOR " = "never" ] &&
[ [ $( grep -e '^formatter:' " $config_file " ) = = *"pretty" * ] ] &&
[ [ $( grep ' color: ' " $config_file " ) != *"false" * ] ] ; then
cp " $config_file " " $config_file_no_color "
echo -e "settings:\n color: false" >> " $config_file_no_color "
args = ${ args / $config_file / $config_file_no_color }
fi
fi
2022-05-25 13:31:24 +01:00
2022-01-06 13:21:52 +02:00
local dir_path
for dir_path in $( echo " ${ paths [*] } " | tr ' ' '\n' | sort -u) ; do
dir_path = " ${ dir_path //__REPLACED__SPACE__/ } "
2018-05-16 20:04:48 +02:00
2022-01-06 13:21:52 +02:00
pushd " $dir_path " > /dev/null || continue
2018-05-16 20:04:48 +02:00
2021-10-15 15:26:23 +03:00
#
# 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 = " $(
2024-10-02 15:05:21 +02:00
find . -maxdepth 1 -type f | sed 's|.*\.||' | sort -u | grep -oE '^tofu|^tf$|^tfvars$' ||
2021-10-15 15:26:23 +03:00
exit 0
) "
# if no TF files - skip dir
[ ! " $dir_have_tf_files " ] && popd > /dev/null && continue
dir = " $( dirname " $text_file " ) "
mkdir -p " $dir "
2023-12-21 20:00:20 +00:00
# Use of insertion markers, where there is no existing README file
2022-01-06 15:08:18 +02:00
{
echo -e " # ${ PWD ##*/ } \n "
2023-12-21 20:00:20 +00:00
echo " $insertion_marker_begin "
echo " $insertion_marker_end "
2022-01-06 15:08:18 +02:00
} >> " $text_file "
2021-10-15 15:26:23 +03:00
fi
# If file still not exist - skip dir
[ [ ! -f " $text_file " ] ] && popd > /dev/null && continue
#
2021-11-17 19:16:38 +01:00
# If `--add-to-existing-file=true` set, check is in file exist "hook markers",
2021-10-15 15:26:23 +03:00
# and if not - append "hook markers" to the end of file.
#
2021-11-17 19:16:38 +01:00
if $add_to_existing ; then
2023-12-21 20:00:20 +00:00
HAVE_MARKER = $( grep -o " $insertion_marker_begin " " $text_file " || exit 0)
2021-10-15 15:26:23 +03:00
if [ ! " $HAVE_MARKER " ] ; then
2023-12-21 20:00:20 +00:00
# Use of insertion markers, where addToExisting=true, with no markers in the existing file
echo " $insertion_marker_begin " >> " $text_file "
echo " $insertion_marker_end " >> " $text_file "
2021-10-15 15:26:23 +03:00
fi
2018-11-13 12:30:06 +01:00
fi
2018-05-16 20:04:48 +02:00
2024-03-08 09:23:05 +00:00
# shellcheck disable=SC2086
terraform-docs $tf_docs_formatter $args ./ > " $tmp_file "
2018-11-13 12:30:06 +01:00
2023-12-21 20:00:20 +00:00
# Use of insertion markers to insert the terraform-docs output between the markers
2018-11-13 12:30:06 +01:00
# Replace content between markers with the placeholder - https://stackoverflow.com/questions/1212799/how-do-i-extract-lines-between-two-line-delimiters-in-perl#1212834
2023-12-21 20:00:20 +00:00
perl_expression = " if (/ $insertion_marker_begin /../ $insertion_marker_end /) { print \$_ if / $insertion_marker_begin /; print \"I_WANT_TO_BE_REPLACED\\n\$_\" if / $insertion_marker_end /;} else { print \$_ } "
perl -i -ne " $perl_expression " " $text_file "
2018-11-13 12:30:06 +01:00
# Replace placeholder with the content of the file
perl -i -e 'open(F, "' " $tmp_file " '"); $f = join "", <F>; while(<>){if (/I_WANT_TO_BE_REPLACED/) {print $f} else {print $_};}' " $text_file "
rm -f " $tmp_file "
2018-05-16 20:04:48 +02:00
2018-05-16 21:58:40 +02:00
popd > /dev/null
2018-11-13 12:30:06 +01:00
done
2022-07-06 15:34:13 +03:00
# Cleanup
2022-07-13 16:41:50 +03:00
rm -f " $config_file_no_color "
2018-11-13 12:30:06 +01:00
}
2022-01-06 13:21:52 +02:00
[ " ${ BASH_SOURCE [0] } " != " $0 " ] || main " $@ "