From 5277b7eaf723fd421a3ee8cde37c2a9ae1548970 Mon Sep 17 00:00:00 2001 From: Oliver Ford Date: Sun, 6 Aug 2023 20:46:12 +0100 Subject: [PATCH] Refactor version file parsing to own function --- lib/helpers.sh | 1 + lib/tfenv-parse-version-file.sh | 11 +++++++++++ lib/tfenv-version-name.sh | 2 +- libexec/tfenv-resolve-version | 4 ++-- libexec/tfenv-uninstall | 4 ++-- libexec/tfenv-use | 2 +- 6 files changed, 18 insertions(+), 6 deletions(-) create mode 100755 lib/tfenv-parse-version-file.sh diff --git a/lib/helpers.sh b/lib/helpers.sh index 535232d..7bc3278 100755 --- a/lib/helpers.sh +++ b/lib/helpers.sh @@ -153,6 +153,7 @@ export -f check_dependencies; source "$TFENV_ROOT/lib/tfenv-exec.sh"; source "$TFENV_ROOT/lib/tfenv-min-required.sh"; +source "$TFENV_ROOT/lib/tfenv-parse-version-file.sh"; source "$TFENV_ROOT/lib/tfenv-version-file.sh"; source "$TFENV_ROOT/lib/tfenv-version-name.sh"; diff --git a/lib/tfenv-parse-version-file.sh b/lib/tfenv-parse-version-file.sh new file mode 100755 index 0000000..1a4befc --- /dev/null +++ b/lib/tfenv-parse-version-file.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +set -uo pipefail; + +function tfenv-parse-version-file() { + local version_file="$1" + log 'debug' "Parsing version from file ${version_file}" + + grep --invert-match '^#' "${version_file}"; +} +export -f tfenv-parse-version-file; diff --git a/lib/tfenv-version-name.sh b/lib/tfenv-version-name.sh index 8c53f56..47d7536 100644 --- a/lib/tfenv-version-name.sh +++ b/lib/tfenv-version-name.sh @@ -10,7 +10,7 @@ function tfenv-version-name() { && log 'debug' "TFENV_VERSION_FILE retrieved from tfenv-version-file: ${TFENV_VERSION_FILE}" \ || log 'error' 'Failed to retrieve TFENV_VERSION_FILE from tfenv-version-file'; - TFENV_VERSION="$(grep --invert-match '^#' "${TFENV_VERSION_FILE}" || true)" \ + TFENV_VERSION="$(tfenv-parse-version-file "${TFENV_VERSION_FILE}")" \ && log 'debug' "TFENV_VERSION specified in TFENV_VERSION_FILE: ${TFENV_VERSION}"; TFENV_VERSION_SOURCE="${TFENV_VERSION_FILE}"; diff --git a/libexec/tfenv-resolve-version b/libexec/tfenv-resolve-version index 19dcecb..9cfbf42 100755 --- a/libexec/tfenv-resolve-version +++ b/libexec/tfenv-resolve-version @@ -77,12 +77,12 @@ if [ -z "${arg}" -a -z "${TFENV_TERRAFORM_VERSION:-""}" ]; then if [ "${version_file}" != "${TFENV_CONFIG_DIR}/version" ]; then log 'debug' "Version File (${version_file}) is not the default \${TFENV_CONFIG_DIR}/version (${TFENV_CONFIG_DIR}/version)"; - version_requested="$(grep --invert-match '^#' "${version_file}")" \ + version_requested="$(tfenv-parse-version-file "${version_file}")" \ || log 'error' "Failed to open ${version_file}"; elif [ -f "${version_file}" ]; then log 'debug' "Version File is the default \${TFENV_CONFIG_DIR}/version (${TFENV_CONFIG_DIR}/version)"; - version_requested="$(grep --invert-match '^#' "${version_file}")" \ + version_requested="$(tfenv-parse-version-file "${version_file}")" \ || log 'error' "Failed to open ${version_file}"; # Absolute fallback diff --git a/libexec/tfenv-uninstall b/libexec/tfenv-uninstall index 818fa5a..6fcad63 100755 --- a/libexec/tfenv-uninstall +++ b/libexec/tfenv-uninstall @@ -69,11 +69,11 @@ if [ -z "${arg:-""}" -a -z "${TFENV_TERRAFORM_VERSION:-""}" ]; then log 'debug' "Version File: ${version_file}"; if [ "${version_file}" != "${TFENV_CONFIG_DIR}/version" ]; then log 'debug' "Version File (${version_file}) is not the default \${TFENV_CONFIG_DIR}/version (${TFENV_CONFIG_DIR}/version)"; - version_requested="$(grep --invert-match '^#' "${version_file}")" \ + version_requested="$(tfenv-parse-version-file "${version_file}")" \ || log 'error' "Failed to open ${version_file}"; elif [ -f "${version_file}" ]; then log 'debug' "Version File is the default \${TFENV_CONFIG_DIR}/version (${TFENV_CONFIG_DIR}/version)"; - version_requested="$(grep --invert-match '^#' "${version_file}")" \ + version_requested="$(tfenv-parse-version-file "${version_file}")" \ || log 'error' "Failed to open ${version_file}"; else log 'debug' "Version File is the default \${TFENV_CONFIG_DIR}/version (${TFENV_CONFIG_DIR}/version) but it doesn't exist"; diff --git a/libexec/tfenv-use b/libexec/tfenv-use index 13653b8..13d95df 100755 --- a/libexec/tfenv-use +++ b/libexec/tfenv-use @@ -76,7 +76,7 @@ if [ -z "${requested_arg}" -a -z "${TFENV_TERRAFORM_VERSION:-""}" ]; then version_source_suffix=" (set by ${loaded_version_file})"; if [ -f "${loaded_version_file}" ]; then - requested="$(grep --invert-match '^#' "${loaded_version_file}" || true)"; + requested="$(tfenv-parse-version-file "${loaded_version_file}")"; else # No-one asked for anything, no default version is set either requested='latest';