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
2022-10-06 19:16:29 +03:00
terraform_docs_ " ${ HOOK_CONFIG [*] } " " ${ ARGS [*] } " " ${ FILES [@] } "
2020-08-27 20:55:28 +01:00
}
2022-01-11 15:54:42 +02:00
#######################################################################
2024-01-17 01:10:38 +01:00
# TODO Function which prepares hacks for old versions of `terraform` and
2022-01-11 15:54:42 +02:00
# `terraform-docs` that them call `terraform_docs`
# 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 01:10:38 +01:00
function tofu_docs_ {
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 "
2024-01-17 01:10:38 +01:00
local hack_tofu_docs
2024-01-17 01:15:52 +01:00
hack_terraform_docs = $( tofu version | sed -n 1p | grep -c 0.12) || true
2019-06-17 12:47:06 +02:00
2020-01-21 05:19:46 -05:00
if [ [ ! $( command -v terraform-docs) ] ] ; then
echo "ERROR: terraform-docs is required by terraform_docs pre-commit hook but is not installed or in the system's PATH."
exit 1
fi
local is_old_terraform_docs
2020-09-24 14:44:05 -05:00
is_old_terraform_docs = $( terraform-docs version | grep -o "v0.[1-7]\." | tail -1) || true
2020-01-21 05:19:46 -05:00
2020-01-21 11:54:13 +01:00
if [ [ -z " $is_old_terraform_docs " ] ] ; then # Using terraform-docs 0.8+ (preferred)
2020-01-21 05:19:46 -05:00
2022-01-06 13:21:52 +02:00
terraform_docs "0" " ${ configs [*] } " " $args " " ${ files [@] } "
2020-01-21 05:19:46 -05:00
elif [ [ " $hack_terraform_docs " = = "1" ] ] ; then # Using awk script because terraform-docs is older than 0.8 and terraform 0.12 is used
if [ [ ! $( command -v awk) ] ] ; then
2024-01-17 00:48:28 +01:00
# TODO: pls check it
2020-01-21 05:19:46 -05:00
echo "ERROR: awk is required for terraform-docs hack to work with Terraform 0.12."
exit 1
fi
2019-06-17 12:47:06 +02:00
2020-08-27 20:55:28 +01:00
local tmp_file_awk
2024-01-17 00:48:28 +01:00
tmp_file_awk = $( mktemp " ${ TMPDIR :- /tmp } /tofu-docs-XXXXXXXXXX " )
tofu_docs_awk " $tmp_file_awk "
tofu_docs " $tmp_file_awk " " ${ configs [*] } " " $args " " ${ files [@] } "
2019-06-18 13:58:49 +02:00
rm -f " $tmp_file_awk "
2020-01-21 05:19:46 -05:00
else # Using terraform 0.11 and no awk script is needed for that
2024-01-17 00:48:28 +01:00
# TODO: should be deleted for OpenTofu.
tofu_docs "0" " ${ configs [*] } " " $args " " ${ files [@] } "
2019-06-17 12:47:06 +02:00
2020-01-21 05:19:46 -05:00
fi
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:
# terraform_docs_awk_file (string) filename where awk hack for old
# `terraform-docs` was written. Needed for TF 0.12+.
2024-01-17 00:48:28 +01:00
# Hack skipped when `tofu_docs_awk_file == "0"`
2022-01-11 15:54:42 +02:00
# 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 {
local -r tofu_docs_awk_file = " $1 "
2021-10-15 15:26:23 +03:00
local -r hook_config = " $2 "
2022-07-06 15:34:13 +03:00
local args = " $3 "
2021-10-15 15:26:23 +03:00
shift 3
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 = " $(
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 "
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
2020-01-21 11:54:13 +01:00
if [ [ " $terraform_docs_awk_file " = = "0" ] ] ; then
2020-04-04 06:55:01 +01:00
# shellcheck disable=SC2086
2022-05-25 13:31:24 +01:00
terraform-docs $tf_docs_formatter $args ./ > " $tmp_file "
2020-01-21 11:54:13 +01:00
else
# Can't append extension for mktemp, so renaming instead
2020-08-27 20:55:28 +01:00
local tmp_file_docs
2024-01-17 00:48:28 +01:00
tmp_file_docs = $( mktemp " ${ TMPDIR :- /tmp } /tofu-docs-XXXXXXXXXX " )
2020-01-21 11:54:13 +01:00
mv " $tmp_file_docs " " $tmp_file_docs .tf "
2020-08-27 20:55:28 +01:00
local tmp_file_docs_tf
2020-01-21 11:54:13 +01:00
tmp_file_docs_tf = " $tmp_file_docs .tf "
awk -f " $terraform_docs_awk_file " ./*.tf > " $tmp_file_docs_tf "
2020-04-04 06:55:01 +01:00
# shellcheck disable=SC2086
2022-05-25 13:31:24 +01:00
terraform-docs $tf_docs_formatter $args " $tmp_file_docs_tf " > " $tmp_file "
2020-01-21 11:54:13 +01:00
rm -f " $tmp_file_docs_tf "
fi
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-11 15:54:42 +02:00
#######################################################################
# Function which creates file with `awk` hacks for old versions of
# `terraform-docs`
# Arguments:
# output_file (string) filename where hack will be written to
#######################################################################
2024-01-17 00:48:28 +01:00
function tofu_docs_awk {
2020-08-27 20:55:28 +01:00
local -r output_file = $1
2019-06-17 12:47:06 +02:00
2020-01-21 11:54:13 +01:00
cat << "EOF" > " $output_file "
2019-06-17 12:47:06 +02:00
# This script converts Terraform 0.12 variables/outputs to something suitable for `terraform-docs`
# As of terraform-docs v0.6.0, HCL2 is not supported. This script is a *dirty hack* to get around it.
2020-08-19 06:06:55 -04:00
# https://github.com/terraform-docs/terraform-docs/
# https://github.com/terraform-docs/terraform-docs/issues/62
2019-06-18 13:58:49 +02:00
# Script was originally found here: https://github.com/cloudposse/build-harness/blob/master/bin/terraform-docs.awk
2019-06-17 12:47:06 +02:00
{
2019-06-18 13:58:49 +02:00
if ( $0 ~ /\{ / ) {
2019-06-17 12:47:06 +02:00
braceCnt++
}
2019-06-18 13:58:49 +02:00
if ( $0 ~ /\} / ) {
2019-06-17 12:47:06 +02:00
braceCnt--
}
2019-11-02 12:15:33 +01:00
# ----------------------------------------------------------------------------------------------
# variable|output "..." {
# ----------------------------------------------------------------------------------------------
# [END] variable/output block
if ( blockCnt > 0 && blockTypeCnt = = 0 && blockDefaultCnt = = 0) {
if ( braceCnt = = 0 && blockCnt > 0) {
blockCnt--
print $0
}
}
2019-06-17 12:47:06 +02:00
# [START] variable or output block started
2019-06-18 13:58:49 +02:00
if ( $0 ~ /^[ [ :space:] ] *( variable| output) [ [ :space:] ] [ [ :space:] ] *"(.*?)" /) {
2019-11-02 12:15:33 +01:00
# Normalize the braceCnt and block (should be 1 now)
2019-06-18 13:58:49 +02:00
braceCnt = 1
2019-11-02 12:15:33 +01:00
blockCnt = 1
# [CLOSE] "default" and "type" block
blockDefaultCnt = 0
blockTypeCnt = 0
# Print variable|output line
2019-06-17 12:47:06 +02:00
print $0
}
2019-11-02 12:15:33 +01:00
# ----------------------------------------------------------------------------------------------
# default = ...
# ----------------------------------------------------------------------------------------------
# [END] multiline "default" continues/ends
if ( blockCnt > 0 && blockTypeCnt = = 0 && blockDefaultCnt > 0) {
print $0
# Count opening blocks
blockDefaultCnt += gsub( /\( /, "" )
blockDefaultCnt += gsub( /\[ /, "" )
blockDefaultCnt += gsub( /\{ /, "" )
# Count closing blocks
blockDefaultCnt -= gsub( /\) /, "" )
blockDefaultCnt -= gsub( /\] /, "" )
blockDefaultCnt -= gsub( /\} /, "" )
}
# [START] multiline "default" statement started
if ( blockCnt > 0 && blockTypeCnt = = 0 && blockDefaultCnt = = 0) {
2019-06-18 13:58:49 +02:00
if ( $0 ~ /^[ [ :space:] ] [ [ :space:] ] *( default) [ [ :space:] ] [ [ :space:] ] *= /) {
if ( $3 ~ "null" ) {
print " default = \"null\""
} else {
print $0
2019-11-02 12:15:33 +01:00
# Count opening blocks
blockDefaultCnt += gsub( /\( /, "" )
blockDefaultCnt += gsub( /\[ /, "" )
blockDefaultCnt += gsub( /\{ /, "" )
# Count closing blocks
blockDefaultCnt -= gsub( /\) /, "" )
blockDefaultCnt -= gsub( /\] /, "" )
blockDefaultCnt -= gsub( /\} /, "" )
2019-06-17 12:47:06 +02:00
}
}
}
2019-11-02 12:15:33 +01:00
# ----------------------------------------------------------------------------------------------
# type = ...
# ----------------------------------------------------------------------------------------------
# [END] multiline "type" continues/ends
if ( blockCnt > 0 && blockTypeCnt > 0 && blockDefaultCnt = = 0) {
# The following 'print $0' would print multiline type definitions
#print $0
# Count opening blocks
blockTypeCnt += gsub( /\( /, "" )
blockTypeCnt += gsub( /\[ /, "" )
blockTypeCnt += gsub( /\{ /, "" )
# Count closing blocks
blockTypeCnt -= gsub( /\) /, "" )
blockTypeCnt -= gsub( /\] /, "" )
blockTypeCnt -= gsub( /\} /, "" )
}
# [START] multiline "type" statement started
if ( blockCnt > 0 && blockTypeCnt = = 0 && blockDefaultCnt = = 0) {
if ( $0 ~ /^[ [ :space:] ] [ [ :space:] ] *( type ) [ [ :space:] ] [ [ :space:] ] *= / ) {
if ( $3 ~ "object" ) {
2019-06-17 12:47:06 +02:00
print " type = \"object\""
} else {
2019-11-02 12:15:33 +01:00
# Convert multiline stuff into single line
if ( $3 ~ /^[ [ :space:] ] *list[ [ :space:] ] *\( [ [ :space:] ] *$/) {
type = "list"
} else if ( $3 ~ /^[ [ :space:] ] *string[ [ :space:] ] *\( [ [ :space:] ] *$/) {
type = "string"
} else if ( $3 ~ /^[ [ :space:] ] *map[ [ :space:] ] *\( [ [ :space:] ] *$/) {
type = "map"
} else {
type = $3
}
2019-06-25 14:34:46 +02:00
# legacy quoted types: "string", "list", and "map"
2019-11-02 12:15:33 +01:00
if ( type ~ /^[ [ :space:] ] *"(.*?)" [ [ :space:] ] *$/) {
print " type = " type
2019-06-25 14:34:46 +02:00
} else {
2019-11-02 12:15:33 +01:00
print " type = \"" type "\""
2019-06-25 14:34:46 +02:00
}
2019-06-17 12:47:06 +02:00
}
2019-11-02 12:15:33 +01:00
# Count opening blocks
blockTypeCnt += gsub( /\( /, "" )
blockTypeCnt += gsub( /\[ /, "" )
blockTypeCnt += gsub( /\{ /, "" )
# Count closing blocks
blockTypeCnt -= gsub( /\) /, "" )
blockTypeCnt -= gsub( /\] /, "" )
blockTypeCnt -= gsub( /\} /, "" )
2019-06-17 12:47:06 +02:00
}
}
2019-11-02 12:15:33 +01:00
# ----------------------------------------------------------------------------------------------
# description = ...
# ----------------------------------------------------------------------------------------------
# [PRINT] single line "description"
if ( blockCnt > 0 && blockTypeCnt = = 0 && blockDefaultCnt = = 0) {
if ( $0 ~ /^[ [ :space:] ] [ [ :space:] ] *description[ [ :space:] ] [ [ :space:] ] *= /) {
2019-06-17 12:47:06 +02:00
print $0
}
}
2019-11-02 12:15:33 +01:00
# ----------------------------------------------------------------------------------------------
# value = ...
# ----------------------------------------------------------------------------------------------
## [PRINT] single line "value"
#if (blockCnt > 0 && blockTypeCnt == 0 && blockDefaultCnt == 0) {
# if ($0 ~ /^[[:space:]][[:space:]]*value[[:space:]][[:space:]]*=/) {
# print $0
# }
#}
# ----------------------------------------------------------------------------------------------
# Newlines, comments, everything else
# ----------------------------------------------------------------------------------------------
#if (blockTypeCnt == 0 && blockDefaultCnt == 0) {
# Comments with '#'
if ( $0 ~ /^[ [ :space:] ] *#/) {
print $0
}
# Comments with '//'
if ( $0 ~ /^[ [ :space:] ] *\/ \/ /) {
print $0
}
# Newlines
if ( $0 ~ /^[ [ :space:] ] *$/) {
print $0
2019-06-17 12:47:06 +02:00
}
2019-11-02 12:15:33 +01:00
#}
2019-06-17 12:47:06 +02:00
}
EOF
}
2022-01-06 13:21:52 +02:00
[ " ${ BASH_SOURCE [0] } " != " $0 " ] || main " $@ "