MON-259 improve readme generator

This commit is contained in:
Quentin Manfroi 2018-07-17 11:50:42 +02:00
parent 64e138e5b8
commit 975587e9d2
4 changed files with 90 additions and 40 deletions

View File

@ -1,18 +1,13 @@
#!/bin/bash #!/bin/bash
set -xeo pipefail set -xueo pipefail
script_dir=$(dirname $0) source "$(dirname $0)/utils.sh"
if [[ "$script_dir" == "." ]]; then goto_root
cd ..
else
cd "$(dirname $script_dir)"
fi
for file in $(find . -path ./incubator -prune -o -name 'monitors-*.tf' -print); do for path in $(find . -path ./incubator -prune -o -name 'monitors-*.tf' -print); do
cd $(dirname $file) cd $(dirname $path)
echo $file
> outputs.tf > outputs.tf
for monitor in $(grep 'resource "datadog_monitor"' $(basename $file) | awk '{print $3}' | tr -d '"' ); do for monitor in $(grep 'resource "datadog_monitor"' $(basename $path) | awk '{print $3}' | tr -d '"' ); do
echo $monitor echo $monitor
cat <<EOF >> outputs.tf cat <<EOF >> outputs.tf
output "${monitor}_id" { output "${monitor}_id" {

View File

@ -1,45 +1,72 @@
#!/bin/bash #!/bin/bash
set -xeo pipefail set -xueo pipefail
script_dir=$(dirname $0) source "$(dirname $0)/utils.sh"
if [[ "$script_dir" == "." ]]; then goto_root
cd ..
else
cd "$(dirname $script_dir)"
fi
sed -i '/### Monitors summary ###/q' README.md sed -i '/### Monitors summary ###/q' README.md
echo >> README.md echo >> README.md
for dir in $(find -mindepth 1 \( -path ./incubator -o -path ./scripts -o -path ./testing -o -path ./.git \) -prune -o -type d -print | sort); do for path in $(find -mindepth 1 \( -path './incubator' -o -path './scripts' -o -path './testing' -o -path '*/\.*' \) -prune -o -type d -print | sort); do
count=$(echo ${dir} | tr -cd '/' | wc -c) directories=($(list_dirs $path))
for i in $(seq 1 $((${count}-1))); do for i in $(seq 1 $((${#directories[@]}-1))); do
echo -en "\t" >> README.md echo -en "\t" >> README.md
done done
echo -en "- [$(basename ${dir})](https://bitbucket.org/morea/terraform.feature.datadog/src/master/" >> README.md echo -en "- [$(basename ${path})](https://bitbucket.org/morea/terraform.feature.datadog/src/master/" >> README.md
for i in $(seq 2 $((${count}+1))); do for directory in "${directories[@]}"; do
echo -en "$(echo ${dir} | cut -d'/' -f ${i})/" >> README.md echo -en "${directory}/" >> README.md
done done
echo ")" >> README.md echo ")" >> README.md
done done
PATTERN_DOC="Related documentation" PATTERN_DOC="Related documentation"
for dir in $(find -mindepth 2 -name README.md); do for path in $(find . -path ./incubator -prune -o -name 'monitors-*.tf' -print); do
cd $(dirname $dir) cd $(dirname $path)
pwd EXIST=0
cp README.md README.md.bak if [ -f README.md ]; then
sed -i '/Inputs/,$d' README.md mv README.md README.md.bak
terraform-docs md ./ | tail -n +2 >> README.md EXIST=1
sed -i 's/## Inputs/Inputs/g' README.md fi
sed -i '/Inputs/a ------' README.md module=$(list_dirs $(dirname ${path}))
sed -i 's/## Outputs/Outputs/g' README.md module_space=${module^^}
sed -i '/Outputs/a -------' README.md module_dash=${module//[ ]/-}
if ! grep "${PATTERN_DOC}" README.md.bak; then module_slash=${module//[ ]/\/}
echo "Error: missing documentation section in README" cat <<EOF > README.md
exit 1 # ${module_space} DataDog monitors
## How to use this module
\`\`\`
module "datadog-monitors-${module_dash}" {
source = "git::ssh://git@bitbucket.org/morea/terraform.feature.datadog.git//${module_slash}?ref={revision}"
environment = "\${var.environment}"
message = "\${module.datadog-message-alerting.alerting-message}"
}
\`\`\`
## Purpose
Creates DataDog monitors with the following checks :
EOF
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for match in $(grep name $(basename ${path})); do
echo "- $(get_name "${match}")" >> README.md
done
IFS=$SAVEIFS
terraform-docs md ./ >> README.md
if [[ $EXIST -eq 0 ]]; then
cat <<EOF >> README.md
## ${PATTERN_DOC}
EOF
else
grep -Pzo --color=never ".*${PATTERN_DOC}(.*\n)*" README.md.bak | head -n -1 >> README.md
rm README.md.bak
fi fi
grep -Pzo --color=never ".*${PATTERN_DOC}(.*\n)*" README.md.bak | head -n -1 >> README.md
dos2unix README.md dos2unix README.md
rm README.md.bak
cd - >> /dev/null cd - >> /dev/null
done done

View File

@ -1,5 +1,5 @@
#!/bin/bash #!/bin/bash
set -xeo pipefail set -xueo pipefail
cd $(dirname $0) cd $(dirname $0)
for script in [0-9][0-9]_*.sh; do for script in [0-9][0-9]_*.sh; do

28
scripts/utils.sh Executable file
View File

@ -0,0 +1,28 @@
#!/bin/bash
function goto_root {
script_dir=$(dirname $0)
if [[ "$script_dir" == "." ]]; then
cd ..
else
cd "$(dirname $script_dir)"
fi
}
function list_dirs {
echo ${1} | awk -F '/' '{$1=""; print $0}' | cut -c 2-
}
function get_name {
regex='^[[:space:]]+name[[:space:]]+=[[:space:]]+"\[.*\][[:space:]]+(.*)"$'
if [[ "${1}" =~ ${regex} ]]; then
name="${BASH_REMATCH[1]}"
else
return 42
fi
if [[ "${name}" =~ ^(.*)[[:space:]]\{\{#is_alert\}\}.*$ ]]; then
echo "${BASH_REMATCH[1]}"
else
echo $name
fi
}