From 31e22e537806e7d2c95f93474386c1aff6a590aa Mon Sep 17 00:00:00 2001 From: k3y0708 Date: Mon, 8 May 2023 19:39:32 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=89=20Initial=20Commit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 38 +++++++++++++++++++ minify_tf | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 README.md create mode 100755 minify_tf diff --git a/README.md b/README.md new file mode 100644 index 0000000..b665606 --- /dev/null +++ b/README.md @@ -0,0 +1,38 @@ +# Minify TF + +This is a simple script to output minify the Terraform CLI output. The script is **only** able to plan (or plan a destruction) because you should always check the real plan before applying. + +## Usage + +Normal planning: + +```bash +minify_tf plan # Plan the Creation/Update of the infrastructure +minify_tf destroy # Plan the Destruction of the infrastructure +``` + +Regenerate the last plan: + +```bash +minify_tf regen # Regenerate the last plan with minimal output +minify_tf regenfull # Regenerate the last plan with full output +``` + +Other commands: + +```bash +minify_tf about # Show the about message +minify_tf version # Show the version of the script +``` + +## Installation + +There are two ways of installing this script: + +1. Put the script in your `$PATH` (e.g. `/usr/local/bin`) +2. Use Homebrew (or Linuxbrew) to install it: + +```bash +brew tap k3y0708/tap +brew install minify_tf +``` diff --git a/minify_tf b/minify_tf new file mode 100755 index 0000000..ff94bd9 --- /dev/null +++ b/minify_tf @@ -0,0 +1,110 @@ +#!/bin/bash + +# Constants ------------------------------------------------------------------- + +HISTORY_DIR="$HOME/.minify_tf_plan_history" +HISTORY_FILE="$HISTORY_DIR/latest" +STATUS_FILE="$HISTORY_DIR/status" + +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[0;33m' +BLUE='\033[0;34m' +NC='\033[0m' + +ADD_SIGN="${GREEN}+${NC}" +REMOVE_SIGN="${RED}-${NC}" +CHANGE_SIGN="${YELLOW}~${NC}" +REPLACE_SIGN="${RED}-${GREEN}+${NC}" +OTHER_SIGN="${BLUE}*${NC}" + +NAME="minify_tf" +VERSION="v0.0.1" + +# Command Line Interface ------------------------------------------------------- +if [[ "$1" == "regenfull" ]]; then + cat $HISTORY_FILE + exit 0 +elif [[ "$1" == "regen" ]]; then + plan=$(cat $HISTORY_FILE) + status=$(cat $STATUS_FILE) +elif [[ "$1" == "destroy" ]]; then + plan="$(terraform plan -destroy -detailed-exitcode -input=false)" + status=$? +elif [[ "$1" == "plan" ]]; then + plan="$(terraform plan -detailed-exitcode -input=false)" + status=$? +elif [[ "$1" == "about" ]]; then + NV="$NAME $VERSION" + NV_LEN=${#NV} + SPACES_LEN=$(( 26 - $NV_LEN )) + for (( i=0; i<$SPACES_LEN; i++ )); do + NV="$NV " + done + + echo "+----------------------------+" + echo "| $NV |" + echo "| by @k3y0708 |" + echo "| https://github.com/k3y0708 |" + echo "+----------------------------+" + exit 0 +elif [[ "$1" == "version" ]]; then + echo "$VERSION" + exit 0 +else + echo "Usage: $0 [plan|destroy|regen|regenfull|about|version]" + exit 1 +fi + +# Save history and status ----------------------------------------------------- +if [[ "$1" != *"regen"* ]]; then + mkdir -p "$HISTORY_DIR" + echo "$plan" > "$HISTORY_FILE" + echo "$status" > "$STATUS_FILE" +fi + +# Check if there are changes -------------------------------------------------- +if [[ "$status" == "0" ]]; then + echo "No changes. Infrastructure is up-to-date." + exit 0 +fi +if [[ "$status" == "1" ]]; then + echo "Error in terraform plan" + exit 1 +fi +if [[ "$status" != "2" ]]; then + echo "Unknown error in terraform plan" + echo "Please contact the developer ($NAME about)" + exit 1 +fi + +# Print changes --------------------------------------------------------------- +out="$(echo "$plan" | grep " # " | sed 's/ # //g')" + +add="$(echo "$out" | sed -e 's/\x1b\[[0-9;]*m//g' | grep ' will be created' | sed 's/ will be created//g')" +remove="$(echo "$out" | sed -e 's/\x1b\[[0-9;]*m//g' | grep ' will be destroyed' | sed 's/ will be destroyed//g')" +change="$(echo "$out" | sed -e 's/\x1b\[[0-9;]*m//g' | grep ' will be updated' | sed 's/ will be updated//g')" +replace="$(echo "$out" | sed -e 's/\x1b\[[0-9;]*m//g' | grep ' must be replaced' | sed 's/ must be replaced//g')" +other="$(echo "$out" | sed -e 's/\x1b\[[0-9;]*m//g'| grep -v ' will be created' | grep -v ' will be destroyed' | grep -v ' will be updated' | grep -v ' must be replaced' | grep -v '(because ' | grep -v '(depends '| tr ' ' '§§§')" + +for line in $add; do + echo -e "$ADD_SIGN $line" +done + +for line in $remove; do + echo -e "$REMOVE_SIGN $line" +done + +for line in $change; do + if [[ "$line" != *"in-place"* ]]; then + echo -e "$CHANGE_SIGN $line" + fi +done + +for line in $replace; do + echo -e "$REPLACE_SIGN $line" +done + +for line in $other; do + echo -e "$OTHER_SIGN $(echo "$line" | tr '§§§' ' ')" +done