From 74d58d94f5a32c28958a3d755dafb5983e1e98d8 Mon Sep 17 00:00:00 2001 From: Maxim Likhachev Date: Wed, 11 May 2022 11:55:38 +0300 Subject: [PATCH] ++terraform-mermaid --- README.md | 1 + scripts/terraform-mermaid | 93 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100755 scripts/terraform-mermaid diff --git a/README.md b/README.md index d8a3588..900f2da 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,7 @@ will create the symbolic link for each script in the \ directory. - [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. - [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. - [utable](scripts/utable) shows a Unicode table. - [vault-kv-tree](scripts/vault-kv-tree) shows all HashiCorp Vault's paths and keys recursively. diff --git a/scripts/terraform-mermaid b/scripts/terraform-mermaid new file mode 100755 index 0000000..6c38628 --- /dev/null +++ b/scripts/terraform-mermaid @@ -0,0 +1,93 @@ +#!/usr/bin/env bash + +# +# Copyright (C) 2022, Maxim Lihachev, +# +# 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 . +# +# 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[@]}" +