Skip to content

Instantly share code, notes, and snippets.

@dex4er
Last active October 11, 2022 23:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dex4er/bb84fe5932c51dc87b6d38d0ea39b666 to your computer and use it in GitHub Desktop.
Save dex4er/bb84fe5932c51dc87b6d38d0ea39b666 to your computer and use it in GitHub Desktop.
Terraform less verbose and more bash friendly
#!/bin/bash
trap 'rm -rf terraform.tfplan' EXIT
trap '' INT
function filter_manifest_short() {
grep --line-buffered -v -P '\(known after apply\)|\(\d+ unchanged \w+ hidden\)'
}
function filter_manifest_compact() {
grep --line-buffered -v -P '^\s\s[\s+~-]'
}
function filter_terraform_status() {
declare -A statusline
declare -A progress
progress=(["-"]="\\\\" ["\\\\"]="|" ["|"]="/" ["/"]="-" [r]="R" [R]="r" [a]="A" [A]="a" [c]="C" [C]="c" [d]="D" [D]="d")
ignore=no
IFS=''
while read line; do
test "$line" != "$prev" || continue
case "$line" in
*'Warning:'*'Applied changes may be incomplete'*|*'Warning:'*'Resource targeting is in effect'*|'This plan was saved to: terraform.tfplan')
ignore=yes
;;
*'suggests to use it as part of an error message'*|*'exceptional situations such as recovering from errors or mistakes'*|*'terraform apply "terraform.tfplan"')
ignore=no
continue
;;
esac
test $ignore = yes && continue
case "$line" in
*': Refreshing state...'*)
key="-"
currentstate="${statusline[$key]:-/}"
statusline[$key]="${progress[$currentstate]}"
echo "${statusline[*]}" | xargs printf "%s"
printf "\r"
;;
*': Reading...'*)
statusline["-"]="*"
key="${line%: Reading...*}"
statusline[$key]="r"
echo "${statusline[*]}" | xargs printf "%s"
printf "\r"
;;
*': Creating...'*)
statusline["-"]="*"
key="${line%: Creating...*}"
statusline[$key]="a"
echo "${statusline[*]}" | xargs printf "%s"
printf "\r"
;;
*': Modifying...'*)
statusline["-"]="*"
key="${line%: Modifying...*}"
statusline[$key]="c"
echo "${statusline[*]}" | xargs printf "%s"
printf "\r"
;;
*': Destroying...'*)
statusline["-"]="*"
key="${line%: Destroying...*}"
key="${key% (* ????????)}"
statusline[$key]="d"
echo "${statusline[*]}" | xargs printf "%s"
printf "\r"
;;
*': Still '*'ing... '*)
statusline["-"]="*"
key="${line%: Still *}"
statusline[$key]="${progress[${statusline[$key]}]}"
echo "${statusline[*]}" | xargs printf "%s"
printf "\r"
;;
*': '*' complete after '*)
statusline["-"]="*"
key="${line%: * complete after *}"
statusline[$key]="*"
echo "${statusline[*]}" | xargs printf "%s"
printf "\r"
;;
*)
echo "$line"
prev="$line"
esac
done
}
filter="filter_manifest_short | filter_terraform_status"
args=()
for arg in "$@"; do
case "$arg" in
-compact) filter="filter_manifest_compact | filter_terraform_status";;
-short) ;;
-full) filter="cat";;
-*) args+=("$arg");;
*) args+=("-target=$arg")
esac
done
terraform plan -detailed-exitcode ${args[*]} -out=terraform.tfplan | eval $filter
test ${PIPESTATUS[0]} = 2 || exit $?
echo "Do you want to perform these actions?"
echo " Terraform will perform the actions described above."
echo " Only 'yes' will be accepted to approve.:"
echo ""
read -p " Enter a value: " VALUE
test "$VALUE" = "yes" || exit 0
echo ""
terraform apply -auto-approve -refresh=false ${args[*]} terraform.tfplan | eval $filter
status=${PIPESTATUS[0]}
exit $status
#!/bin/bash
trap 'rm -rf terraform.tfplan' EXIT
trap '' INT
function filter_manifest_short() {
grep --line-buffered -v -P '\(known after apply\)|\(\d+ unchanged \w+ hidden\)'
}
function filter_manifest_compact() {
grep --line-buffered -v -P '^\s\s[\s+~-]'
}
function filter_terraform_status() {
declare -A statusline
declare -A progress
progress=(["-"]="\\\\" ["\\\\"]="|" ["|"]="/" ["/"]="-" [r]="R" [R]="r" [a]="A" [A]="a" [c]="C" [C]="c" [d]="D" [D]="d")
IFS=''
while read line; do
test "$line" != "$prev" || continue
case "$line" in
*': Refreshing state...'*)
key="-"
currentstate="${statusline[$key]:-/}"
statusline[$key]="${progress[$currentstate]}"
echo "${statusline[*]}" | xargs printf "%s"
printf "\r"
;;
*': Reading...'*)
statusline["-"]="*"
key="${line%: Reading...*}"
statusline[$key]="r"
echo "${statusline[*]}" | xargs printf "%s"
printf "\r"
;;
*': Creating...'*)
statusline["-"]="*"
key="${line%: Creating...*}"
statusline[$key]="a"
echo "${statusline[*]}" | xargs printf "%s"
printf "\r"
;;
*': Modifying...'*)
statusline["-"]="*"
key="${line%: Modifying...*}"
statusline[$key]="c"
echo "${statusline[*]}" | xargs printf "%s"
printf "\r"
;;
*': Destroying...'*)
statusline["-"]="*"
key="${line%: Destroying...*}"
key="${key% (* ????????)}"
statusline[$key]="d"
echo "${statusline[*]}" | xargs printf "%s"
printf "\r"
;;
*': Still '*'ing... '*)
statusline["-"]="*"
key="${line%: Still *}"
statusline[$key]="${progress[${statusline[$key]}]}"
echo "${statusline[*]}" | xargs printf "%s"
printf "\r"
;;
*': '*' complete after '*)
statusline["-"]="*"
key="${line%: * complete after *}"
statusline[$key]="*"
echo "${statusline[*]}" | xargs printf "%s"
printf "\r"
;;
*)
echo "$line"
prev="$line"
esac
done
}
filter="filter_manifest_short | filter_terraform_status"
args=()
for arg in "$@"; do
case "$arg" in
-compact) filter="filter_manifest_compact | filter_terraform_status";;
-short) ;;
-full) filter="cat";;
-*) args+=("$arg");;
*) args+=("-target=$arg")
esac
done
terraform plan -destroy -detailed-exitcode ${args[*]} | eval $filter
test ${PIPESTATUS[0]} = 2 || exit $?
echo "Do you want to perform these actions?"
echo " Terraform will perform the actions described above."
echo " Only 'yes' will be accepted to approve.:"
echo ""
read -p " Enter a value: " VALUE
test "$VALUE" = "yes" || exit 0
echo ""
terraform destroy -auto-approve ${args[*]} | eval $filter
status=${PIPESTATUS[0]}
exit $status
#!/bin/sh
set -e
exec terraform init -upgrade \
| grep -v -P 'Finding .* versions matching|Initializing (modules|the backend|provider plugins)...|Upgrading modules...|Using previously-installed|Reusing previous version of|from the shared cache directory|in modules/' \
| sed '/Terraform has been successfully initialized/,$d' \
| uniq \
| sed '1{/^$/d}'
#!/bin/bash
terraform state list "$@" | sed 's/\x1b\[[01]m//g'
exit ${PIPESTATUS[0]}
#!/bin/bash
set -e
terraform state mv "$@"
#!/bin/bash
function filter_manifest_short() {
grep --line-buffered -v -P '\(known after apply\)|\(\d+ unchanged \w+ hidden\)'
}
function filter_manifest_compact() {
grep --line-buffered -v -P '^\s\s[\s+~-]'
}
function filter_terraform_status() {
declare -A statusline
declare -A progress
progress=(["-"]="\\\\" ["\\\\"]="|" ["|"]="/" ["/"]="-" [r]="R" [R]="r" [a]="A" [A]="a" [c]="C" [C]="c" [d]="D" [D]="d")
IFS=''
while read line; do
test "$line" != "$prev" || continue
case "$line" in
*': Refreshing state...'*)
key="-"
currentstate="${statusline[$key]:-/}"
statusline[$key]="${progress[$currentstate]}"
echo "${statusline[*]}" | xargs printf "%s"
printf "\r"
;;
*': Reading...'*)
statusline["-"]="*"
key="${line%: Reading...*}"
statusline[$key]="r"
echo "${statusline[*]}" | xargs printf "%s"
printf "\r"
;;
*)
echo "$line"
prev="$line"
esac
done
}
filter="filter_manifest_short | filter_terraform_status"
args=()
for arg in "$@"; do
case "$arg" in
-compact) filter="filter_manifest_compact | filter_terraform_status";;
-short) ;;
-full) filter="cat";;
-*) args+=("$arg");;
*) args+=("-target=$arg")
esac
done
terraform plan -detailed-exitcode ${args[*]} | eval $filter
exit ${PIPESTATUS[0]}
#!/bin/bash
args=()
for arg in "$@"; do
case "$arg" in
-*) args+=("$arg");;
*) args+=("-target=$arg")
esac
done
terraform refresh ${args[*]}
exit ${PIPESTATUS[0]}
#!/bin/bash
set -e
terraform state rm "$@"
#!/bin/bash
if [ $# -gt 0 ]; then
terraform state show -no-color "$@" | sed 's/\x1b\[[01]m//g'
exit ${PIPESTATUS[0]}
else
terraform show -no-color
exit $?
fi
#!/bin/bash
set -e
for r in "$@"; do
terraform taint "$r"
done
#!/bin/bash
set -e
for r in "$@"; do
terraform untaint "$r"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment