Skip to content

Commit

Permalink
Refactor version file parsing to own function
Browse files Browse the repository at this point in the history
  • Loading branch information
OJFord committed Aug 6, 2023
1 parent 759a13a commit 9d38f5f
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 6 deletions.
10 changes: 10 additions & 0 deletions lib/tfenv-parse-version-file.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/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}";
}
2 changes: 1 addition & 1 deletion lib/tfenv-version-name.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}";
Expand Down
4 changes: 2 additions & 2 deletions libexec/tfenv-resolve-version
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions libexec/tfenv-uninstall
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion libexec/tfenv-use
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 9d38f5f

Please sign in to comment.