diff --git a/README.md b/README.md index fdb6393..42ba98f 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,13 @@ which tofuenv Install a specific version of OpenTofu. -If no parameter is passed, the version to use is resolved automatically via [TOFUENV_TOFU_VERSION environment variable](#TOFUENV_TOFU_VERSION) or [.opentofu-version files](#opentofu-version-file), in that order of precedence, i.e. TOFUENV_TOFU_VERSION, then .opentofu-version. The default is `latest` if none are found. +If no parameter is passed, the version to use is resolved automatically via the following: + +- [TOFUENV_TOFU_VERSION environment variable](#TOFUENV_TOFU_VERSION) +- [.opentofu-version files](#opentofu-version-file) +- [TOFUENV_CONFIG_DIR](#TOFUENV_CONFIG_DIR)`/version` +- [TOFUENV_TOFU_DEFAULT_VERSION](#TOFUENV_TOFU_DEFAULT_VERSION) +- The default of `latest`. If a parameter is passed, available options: @@ -138,6 +144,12 @@ terraform { #### TOFUENV +##### `TOFUENV_TOFU_DEFAULT_VERSION` + +String (Default: "latest") + +Specify which version of tofu to install if no other version information can be found. + ##### `TOFUENV_GITHUB_TOKEN` String (Default: "") diff --git a/lib/tofuenv-version-name.sh b/lib/tofuenv-version-name.sh index c19e884..e974317 100644 --- a/lib/tofuenv-version-name.sh +++ b/lib/tofuenv-version-name.sh @@ -10,10 +10,14 @@ function tofuenv-version-name() { && log 'debug' "TOFUENV_VERSION_FILE retrieved from tofuenv-version-file: ${TOFUENV_VERSION_FILE}" \ || log 'error' 'Failed to retrieve TOFUENV_VERSION_FILE from tofuenv-version-file'; - TOFUENV_VERSION="$(cat "${TOFUENV_VERSION_FILE}" || true)" \ - && log 'debug' "TOFUENV_VERSION specified in TOFUENV_VERSION_FILE: ${TOFUENV_VERSION}"; - - TOFUENV_VERSION_SOURCE="${TOFUENV_VERSION_FILE}"; + if [[ -f "${TOFUENV_VERSION_FILE}" ]]; then + TOFUENV_VERSION="$(cat "${TOFUENV_VERSION_FILE}" || true)" \ + && log 'debug' "TOFUENV_VERSION specified in TOFUENV_VERSION_FILE: ${TOFUENV_VERSION}"; + TOFUENV_VERSION_SOURCE="${TOFUENV_VERSION_FILE}"; + else + TOFUENV_VERSION="${TOFUENV_TOFU_DEFAULT_VERSION:-"latest"}"; + TOFUENV_VERSION_SOURCE='TOFUENV_TOFU_VERSION'; + fi; else TOFUENV_VERSION="${TOFUENV_TOFU_VERSION}" \ diff --git a/libexec/tofuenv-resolve-version b/libexec/tofuenv-resolve-version index 732cfcb..aa6a8fe 100755 --- a/libexec/tofuenv-resolve-version +++ b/libexec/tofuenv-resolve-version @@ -71,30 +71,18 @@ declare version_requested version regex min_required version_file; declare arg="${1:-""}"; -if [ -z "${arg}" -a -z "${TOFUENV_TOFU_VERSION:-""}" ]; then - version_file="$(tofuenv-version-file)"; - log 'debug' "Version File: ${version_file}"; - - if [ "${version_file}" != "${TOFUENV_CONFIG_DIR}/version" ]; then - log 'debug' "Version File (${version_file}) is not the default \${TOFUENV_CONFIG_DIR}/version (${TOFUENV_CONFIG_DIR}/version)"; - version_requested="$(cat "${version_file}")" \ - || log 'error' "Failed to open ${version_file}"; - - elif [ -f "${version_file}" ]; then - log 'debug' "Version File is the default \${TOFUENV_CONFIG_DIR}/version (${TOFUENV_CONFIG_DIR}/version)"; - version_requested="$(cat "${version_file}")" \ - || log 'error' "Failed to open ${version_file}"; - - # Absolute fallback - if [ -z "${version_requested}" ]; then - log 'debug' 'Version file had no content. Falling back to "latest"'; - version_requested='latest'; - fi; - - else - log 'debug' "Version File is the default \${TOFUENV_CONFIG_DIR}/version (${TOFUENV_CONFIG_DIR}/version) but it doesn't exist"; - log 'debug' 'No version requested on the command line or in the version file search path. Installing "latest"'; - version_requested='latest'; +if [ -z "${arg}" ] && [ -z "${TOFUENV_TOFU_VERSION:-""}" ]; then + log 'debug' 'We are not hardcoded by a TOFUENV_TOFU_VERSION environment variable'; + + version_file="$(tofuenv-version-file)" \ + && log 'debug' "Version file retrieved from tofuenv-version-file: ${version_file}" \ + || log 'error' 'Failed to retrieve version file from tofuenv-version-file'; + + if [[ -f "${version_file}" ]]; then + version_requested="$(cat "${version_file}" || true)" \ + && log 'debug' "Version specified in ${version_file}: ${version_requested}"; + else + version_requested="${TOFUENV_TOFU_DEFAULT_VERSION:-"latest"}"; fi; elif [ -n "${TOFUENV_TOFU_VERSION:-""}" ]; then version_requested="${TOFUENV_TOFU_VERSION}"; diff --git a/test/test_uninstall.sh b/test/test_uninstall.sh index 0eb2b72..f9541cc 100755 --- a/test/test_uninstall.sh +++ b/test/test_uninstall.sh @@ -88,13 +88,13 @@ done; echo "### Uninstall removes versions directory" cleanup || error_and_die "Cleanup failed?!" ( - tofuenv install 1.6.0-beta5 || exit 1 - tofuenv install 1.6.0-beta4 || exit 1 - [ -d "./versions" ] || exit 1 - tofuenv uninstall 1.6.0-beta5 || exit 1 - [ -d "./versions" ] || exit 1 - tofuenv uninstall 1.6.0-beta4 || exit 1 - [ -d "./versions" ] && exit 1 || exit 0 + tofuenv install 1.6.0-beta5 || error_and_die "Failed to install 1.6.0-beta5" + tofuenv install 1.6.0-beta4 || error_and_die "Failed to install 1.6.0-beta" + [ -d "./versions" ] || error_and_die "Versions directory does not exist" + tofuenv uninstall 1.6.0-beta5 || error_and_die "Failed to uninstall 1.6.0-beta5" + [ -d "./versions" ] || error_and_die "Versions directory does not exist - 2" + tofuenv uninstall 1.6.0-beta4 || error_and_die "Failed to uninstall 1.6.0-beta4" + [ -d "./versions" ] && error_and_die "Versions directory exists but shouldn't" || exit 0 ) || error_and_proceed "Removing last version deletes versions directory" if [ "${#errors[@]}" -gt 0 ]; then