From 68009c4229f84ba2f402b688c7712a522fa697aa Mon Sep 17 00:00:00 2001 From: Clement Date: Fri, 3 Nov 2023 00:42:23 +0800 Subject: [PATCH] Updates --- tplan | 53 +++++++++++++++++++++++++---------------------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/tplan b/tplan index 46aa26d..a0f3986 100755 --- a/tplan +++ b/tplan @@ -1,19 +1,27 @@ -#!/bin/bash +#!/usr/bin/env bash set -e # In case plans directory is not there -[[ -d plans ]] || mkdir plans +PLAN_FOLDER=".plans" +[[ -d $PLAN_FOLDER ]] || mkdir -p "${PLAN_FOLDER}" # Prepare plan variables -PLAN_NAME="$( gdate --iso-8601=minutes )-plan" -PLAN_FILE="plans/${PLAN_NAME}" +if type -t gdate >/dev/null ; then + # GNU date on MacOS is gdate + PLAN_NAME="$( gdate --iso-8601=minutes )-plan" +else + # Fall back to Linux date + PLAN_NAME="$( date --iso-8601=minutes )-plan" +fi +PLAN_FILE="${PLAN_FOLDER}/${PLAN_NAME}" # Compress old plans -find plans -type f ! -name "*.gz" -execdir gzip -f "{}" \; +find "${PLAN_FOLDER}" -type f ! -name "*.gz" -execdir gzip -f "{}" \; || exit # Pick tfvars file -TFVAR_COUNT=$( find . -type f -name "*.tfvars" | wc -l ) +readarray -t FILELIST <<<$( find . -type f -name "*.tfvars" ) +TFVAR_COUNT=${#FILELIST[@]} if (( TFVAR_COUNT <= 0 )); then printf "No tfvars file found.\n" exit 1 @@ -23,56 +31,45 @@ FILELIST_OUTPUT=$(find . -type f -name "*.tfvars") printf "${FILELIST_OUTPUT}\n" | nl read -rp "Pick file number (1): " TFVAR_PICK if (( TFVAR_PICK > 0 )) && (( TFVAR_PICK <= TFVAR_COUNT )); then - : + TFVAR_PICK_FILE=${FILELIST[$(( TFVAR_PICK++ ))]} else - printf "Pick is invalid\n" && exit 1 + printf "Pick is invalid.\n" + exit 1 fi -# Get picked file -TFVAR_PICK_FILE=$( printf "${FILELIST_OUTPUT}\n" | head -n "${TFVAR_PICK}" | tail -1 ) - # Autoformat before execution terraform fmt && find . -type f \( -name "*.tf" -o -name "*.tfvars" \) -exec terraform fmt "{}" \; || exit # Validate configuration -terraform validate . +terraform validate . || exit 1 # Upgrade first to handle module changes terraform init -upgrade || exit 1 -if (( TFVAR_COUNT > 0 ));then - terraform plan -out "${PLAN_FILE}" -var-file="${TFVAR_PICK_FILE}" -else - terraform plan -out "${PLAN_FILE}" -fi - -# Check if Terraform plan failed +# Start Terraform plan with selected var file +terraform plan -out "${PLAN_FILE}" -var-file="${TFVAR_PICK_FILE}" if ! (( $? == 0 )); then - printf "Please check your Terraform files.\n" + printf "Terraform plan failed. Please check your Terraform files.\n" exit 1 fi -printf "Apply? terraform apply \"${PLAN_FILE}\"\n" - apply_plan_file_auto_approve () { - mv "${PLAN_FILE}" "${PLAN_FILE}-applied" - PLAN_FILE="${PLAN_FILE}-applied" terraform apply -auto-approve "${PLAN_FILE}" + mv "${PLAN_FILE}" "${PLAN_FILE}-applied" } apply_plan_file_approve () { - mv "${PLAN_FILE}" "${PLAN_FILE}-applied" - PLAN_FILE="${PLAN_FILE}-applied" terraform apply "${PLAN_FILE}" + mv "${PLAN_FILE}" "${PLAN_FILE}-applied" } if [[ $1 == '-y' ]]; then apply_plan_file_auto_approve exit fi - -read -rp "(y/n): " ANSWER +read -rp "Apply?\n terraform apply \"${PLAN_FILE}\" (y/n): " ANSWER if [[ $ANSWER == 'y' ]]; then apply_plan_file_approve + exit fi