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
set -xeo pipefail
set -xueo pipefail
script_dir=$(dirname $0)
if [[ "$script_dir" == "." ]]; then
cd ..
else
cd "$(dirname $script_dir)"
fi
source "$(dirname $0)/utils.sh"
goto_root
for file in $(find . -path ./incubator -prune -o -name 'monitors-*.tf' -print); do
cd $(dirname $file)
echo $file
for path in $(find . -path ./incubator -prune -o -name 'monitors-*.tf' -print); do
cd $(dirname $path)
> 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
cat <<EOF >> outputs.tf
output "${monitor}_id" {

View File

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

View File

@ -1,5 +1,5 @@
#!/bin/bash
set -xeo pipefail
set -xueo pipefail
cd $(dirname $0)
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
}