Skip to content

Commit

Permalink
fixup! feat: create segment to display Terraform version
Browse files Browse the repository at this point in the history
  • Loading branch information
fllp committed Jan 18, 2024
1 parent c779b63 commit 87b77ea
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/segments/terraform.bash
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,32 @@
tf_icon="${SEGMENTS_TF_ICON:-}"
tf_extenstion="${TF_FILE_EXTENCION:-tf}"
scanned_dirs="${SEGMENTS_TF_SCANNED_DIRS:-$PWD}"
declare tf_version=
declare tf_version=""

_tf_available() {
command -v terraform
}

_get_terraform_version() {
# when version managers are available, parse their
# config files first before calling 'terraform version'

# check for version files created by 'tfenv'
if [[ -n $(command -v tfenv) ]] && [[ -f $PWD/.terraform-version ]]; then
if [[ $(command -v tfenv) ]] && [[ -f $PWD/.terraform-version ]]; then
tf_version="$(tr -d '\n' <.terraform-version)"

# check for version files created by 'asdf'
elif [[ $(command -v asdf) ]] && [[ -f $PWD/.tool-versions ]] && [[ "$(<.tool-versions)" =~ terraform.([0-9.]+) ]]; then
tf_version="${BASH_REMATCH[1]}"

# get version from terraform directly (slowest)
elif [[ -n $(command -v terraform) ]] && [[ "$(terraform -v)" =~ v([0-9.]+) ]]; then
elif [[ $(_tf_available) ]] && [[ "$(terraform -v)" =~ v([0-9.]+) ]]; then
tf_version="${BASH_REMATCH[1]}"
fi
}

segments::terraform() {
if [[ -n $(find "$scanned_dirs" -maxdepth 1 -name "*.${tf_extenstion}" -print -quit 2>/dev/null) ]]; then
if [[ $(_tf_available) ]] && [[ -n $(find ${scanned_dirs} -maxdepth 1 -name "*.${tf_extenstion}" -print -quit 2>/dev/null) ]]; then
_get_terraform_version
segment="$tf_icon $tf_version"
print_themed_segment 'normal' "$segment"
Expand Down

0 comments on commit 87b77ea

Please sign in to comment.