From 87b77eabbe036f88306e6ed868995bbf5a5b2b2c Mon Sep 17 00:00:00 2001 From: Flying Lollipop Date: Thu, 18 Jan 2024 11:37:12 +0100 Subject: [PATCH] fixup! feat: create segment to display Terraform version --- src/segments/terraform.bash | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/segments/terraform.bash b/src/segments/terraform.bash index 64ba377..002f768 100644 --- a/src/segments/terraform.bash +++ b/src/segments/terraform.bash @@ -3,14 +3,18 @@ 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' @@ -18,13 +22,13 @@ _get_terraform_version() { 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"