Browse Source

++terraform-mermaid

master
Maxim Likhachev 3 years ago
parent
commit
74d58d94f5
  1. 1
      README.md
  2. 93
      scripts/terraform-mermaid

1
README.md

@ -101,6 +101,7 @@ will create the symbolic link for each script in the \<path> directory.
- [how](scripts/how) shows dev cheat sheets using [cheat.sh](https://cheat.sh). - [how](scripts/how) shows dev cheat sheets using [cheat.sh](https://cheat.sh).
- [robodoc2html](scripts/robodoc2html) converts a documentation in the robodoc format into html. - [robodoc2html](scripts/robodoc2html) converts a documentation in the robodoc format into html.
- [terraform-compliance-generator](scripts/terraform-compliance-generator) makes a [terraform-compliance](https://terraform-compliance.com/) compatible BDD testing scenario. - [terraform-compliance-generator](scripts/terraform-compliance-generator) makes a [terraform-compliance](https://terraform-compliance.com/) compatible BDD testing scenario.
- [terraform-mermaid](scripts/terraform-mermaid) converts `terraform graph` output to Mermaid diagram.
- [todolist](scripts/todolist) shows all `TODO` notes in current git repository. - [todolist](scripts/todolist) shows all `TODO` notes in current git repository.
- [utable](scripts/utable) shows a Unicode table. - [utable](scripts/utable) shows a Unicode table.
- [vault-kv-tree](scripts/vault-kv-tree) shows all HashiCorp Vault's paths and keys recursively. - [vault-kv-tree](scripts/vault-kv-tree) shows all HashiCorp Vault's paths and keys recursively.

93
scripts/terraform-mermaid

@ -0,0 +1,93 @@
#!/usr/bin/env bash
#
# Copyright (C) 2022, Maxim Lihachev, <envrm@yandex.ru>
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, version 3.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <https://www.gnu.org/licenses/>.
#
# terraform-mermaid can be used to convert `terraform graph` command output
# from Graphviz to Mermaid format.
# TODO: -s simple
set -e
set -o errtrace
\unalias -a
IFS=$' \t\n'
show_help() {
echo -e "$(basename "$0") can be used to convert \`terraform graph' command output from Graphviz to Mermaid format.\n"
echo -e "USAGE: $(basename "$0") ?-m? ?-d DIRECTION??\n"
echo -e "Command line arguments:"
echo -e " -h --help Show this help"
echo -e " -m --markdown Put the output in triple back quotes (\`\`\`)"
echo -e " -d --direction Specify the direction of the graph (TB | BT | LR | RL; TB by default)"
echo
exit "${1:-0}"
}
UNKNOWN_ARGS=()
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-h|--help) show_help;;
-d|--direction) DIRECTION="$2"; shift; shift;;
-m|--markdown) MARKDOWN="true"; shift;;
*) UNKNOWN_ARGS+=("$1"); shift;;
esac
done
[ ${#UNKNOWN_ARGS} -gt 0 ] && echo "Unknown parameters: ${UNKNOWN_ARGS[*]}" >&2 && show_help 1
declare -a MERMAID
[ -n "$MARKDOWN" ] && MERMAID+=('```mermaid')
MERMAID+=("graph ${DIRECTION:-TB}")
GRAPH=$(terraform graph)
mapfile -t MERMAID_GRAPH < <(
echo "$GRAPH" \
| grep -v 'meta\.count' \
| grep -- '->' \
| sed -E 's/"//g;
s/^[[:space:]]*|\\]|\[root\] | \((expand|close)\)//g;
s!provider.*/!provider:!g;
s/helm_release/helm/g;
s/null_resource\.//g;
s/kubernetes/k8s/g;
s/ -> / --> /g;
s/^(.*) --> (.*)$/\2 --> \1/;
s/(provider:[[:alnum:]_]*)/\1((\1))/;
s/((var|local).[[:alnum:]_]*)/\1{{\1}}/;
s/(helm\.[[:alnum:]_]*)/\1{{\1}}/;
s/(local_file\.[[:alnum:]_]*)/\1>\1]/;
s/default/defaulṭ/;
s/class/clasṣ/g;
/^provider:[[:alnum:]_]*/d;' \
| sed 's/^/ /'
)
MERMAID=("${MERMAID[@]}" "${MERMAID_GRAPH[@]}")
[ -n "$MARKDOWN" ] && MERMAID+=('```')
printf "%s\n" "${MERMAID[@]}"
Loading…
Cancel
Save