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..2367083 --- /dev/null +++ b/lib/tfenv-parse-version-file.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +set -uo pipefail; + +# Ensure GNU grep +check_dependencies; + +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 3975769..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="$(cat "${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 d0b66bc..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="$(cat "${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="$(cat "${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 83289c8..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="$(cat "${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="$(cat "${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 0c411cc..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="$(cat "${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'; diff --git a/test/test_install_and_use.sh b/test/test_install_and_use.sh index fd2b6f0..e454814 100755 --- a/test/test_install_and_use.sh +++ b/test/test_install_and_use.sh @@ -110,6 +110,14 @@ tests__kv=( '0.14.6,v0.14.6' ); +tests_file_only__desc=( + 'version with comment' +); + +tests_file_only__kv=( + '1.2.3,# this is a comment\n1.2.3' +); + tests_count=${#tests__desc[@]}; declare desc kv k v test_num; @@ -127,19 +135,23 @@ for ((test_iter=0; test_iter<${tests_count}; ++test_iter )) ; do || error_and_proceed "## Param Test ${test_num}/${tests_count}: ${desc} ( ${k} / ${v} ) failed"; done; -for ((test_iter=0; test_iter<${tests_count}; ++test_iter )) ; do +tests_file__desc=("${tests__desc[@]}" "${tests_file_only__desc[@]}"); +tests_file__kv=("${tests__kv[@]}" "${tests_file_only__kv[@]}"); +tests_file_count=${#tests_file__desc[@]}; + +for ((test_iter=0; test_iter<${tests_file_count}; ++test_iter )) ; do cleanup || log 'error' 'Cleanup failed?!'; - test_num=$((test_iter + 1)); - desc=${tests__desc[${test_iter}]}; - kv="${tests__kv[${test_iter}]}"; + test_num=$((test_iter + 1)); + desc="${tests_file__desc[${test_iter}]}"; + kv="${tests_file__kv[${test_iter}]}"; v="${kv%,*}"; k="${kv##*,}"; - log 'info' "## ./.terraform-version Test ${test_num}/${tests_count}: ${desc} ( ${k} / ${v} )"; + log 'info' "## ./.terraform-version Test ${test_num}/${tests_file_count}: ${desc} ( ${k} / ${v} )"; log 'info' "Writing ${k} to ./.terraform-version"; - echo "${k}" > ./.terraform-version; + echo -e "${k}" > ./.terraform-version; test_install_and_use "${v}" \ - && log info "## ./.terraform-version Test ${test_num}/${tests_count}: ${desc} ( ${k} / ${v} ) succeeded" \ - || error_and_proceed "## ./.terraform-version Test ${test_num}/${tests_count}: ${desc} ( ${k} / ${v} ) failed"; + && log info "## ./.terraform-version Test ${test_num}/${tests_file_count}: ${desc} ( ${k} / ${v} ) succeeded" \ + || error_and_proceed "## ./.terraform-version Test ${test_num}/${tests_file_count}: ${desc} ( ${k} / ${v} ) failed"; done; for ((test_iter=0; test_iter<${tests_count}; ++test_iter )) ; do