Various Linux scripts.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

93 lines
2.4 KiB

#!/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[@]}"