diff --git a/README.md b/README.md index 432d7b3..6fb60e5 100644 --- a/README.md +++ b/README.md @@ -32,17 +32,23 @@ Currently tfenv supports the following OSes ### tfenv install Install a specific version of Terraform `latest` is a syntax to install latest version +`latest:` is a syntax to install latest version matching regex (used by grep -e) ```sh $ tfenv install 0.7.0 $ tfenv install latest +$ tfenv install latest:^0.8 ``` If you use [.terraform-version](#terraform-version), `tfenv install` (no argument) will install the version written in it. ### tfenv use Switch a version to use +`latest` is a syntax to use the latest installed version +`latest:` is a syntax to use latest installed version matching regex (used by grep -e) ```sh $ tfenv use 0.7.0 +$ tfenv use latest +$ tfenv use latest:^0.8 ``` ### tfenv list diff --git a/libexec/tfenv---version b/libexec/tfenv---version index 9b87556..ab53e94 100755 --- a/libexec/tfenv---version +++ b/libexec/tfenv---version @@ -12,7 +12,7 @@ set -e [ -n "$TFENV_DEBUG" ] && set -x -version="0.3.4" +version="0.4.0" git_revision="" if cd "${BASH_SOURCE%/*}" 2>/dev/null && git remote -v 2>/dev/null | grep -q tfenv; then diff --git a/libexec/tfenv-help b/libexec/tfenv-help index d32316b..a516953 100755 --- a/libexec/tfenv-help +++ b/libexec/tfenv-help @@ -1,5 +1,4 @@ #!/usr/bin/env bash -set -e [ -n "$TFENV_DEBUG" ] && set -x echo "Usage: tfenv [] diff --git a/libexec/tfenv-install b/libexec/tfenv-install index d0c7c22..30ec174 100755 --- a/libexec/tfenv-install +++ b/libexec/tfenv-install @@ -1,15 +1,27 @@ #!/usr/bin/env bash -set -e -[ -n "$TFENV_DEBUG" ] && set -x -if [ $# -gt 1 ];then - echo "usage: tfenv install []" 1>&2 +function error_and_die() { + echo -e "${1}" >&2 exit 1 +} + +[ -n "$TFENV_DEBUG" ] && set -x + +[ $# -gt 1 ] && error_and_die "usage: tfenv install []" + +declare version regex +if [[ "${1}" =~ ^latest\:.*$ ]]; then + version="${1%%\:*}" + regex="${1##*\:}" +else + version="${1}" fi -version="${1}" -if [ "${version}" == "latest" ];then - version=$(tfenv-list-remote | head -n 1) +if [ ${version} == "latest" ]; then + version="$(tfenv-list-remote | grep -e "${regex}" | head -n 1)" + [ -n "${version}" ] || error_and_die "No matching version found in remote" +elif [ -z "$(tfenv-list-remote | grep "${version}")" ]; then + error_and_die "Terraform version '${version}' doesn't exist in remote, please confirm version name." fi if [ -z "${version}" ]; then @@ -19,20 +31,12 @@ if [ -z "${version}" ]; then fi fi -if [ -z "${version}" ];then - echo "version is not specified" 1>&2 - exit 1 -fi +[ -n "${version}" ] || error_and_die "Version is not specified" -dst_path=${TFENV_ROOT}/versions/${version} +dst_path="${TFENV_ROOT}/versions/${version}" if [ -f ${dst_path}/terraform ];then - echo "already installed ${version}" - exit -fi - -if [ -z "$(tfenv-list-remote | grep "${version}")" ];then - echo "'${version}' doesn't exist in remote, please confirm version name." - exit 1 + echo "Terraform v${version} is already installed" + exit 0 fi case "$(uname -s)" in @@ -46,11 +50,11 @@ MINGW64* ) os="linux_amd64" esac -archive_name="terraform_${version}_${os}.zip" -archive_url="https://releases.hashicorp.com/terraform/${version}/${archive_name}" -echo "install Terraform ${version}" -echo "get archive from ${archive_url}" -curl -f -o /tmp/${archive_name} "${archive_url}" -mkdir -p ${dst_path} -unzip /tmp/${archive_name} -d ${dst_path} -echo -e "\033[0;32mthe installation ${version} was successful!!!\033[0;39m" +tarball_name="terraform_${version}_${os}.zip" +tarball_url="https://releases.hashicorp.com/terraform/${version}/${tarball_name}" +echo "Installing Terraform v${version}" +echo "Downloading release tarball from ${tarball_url}" +curl --tlsv1.2 -f -o /tmp/${tarball_name} "${tarball_url}" || error_and_die "Tarball download failed" +mkdir -p ${dst_path} || error_and_die "Failed to make directory ${dst_path}" +unzip /tmp/${tarball_name} -d ${dst_path} || error_and_die "Tarball unzip failed" +echo -e "\033[0;32mInstallation of terraform v${version} successful\033[0;39m" diff --git a/libexec/tfenv-list b/libexec/tfenv-list index 75cdf82..bd88a54 100755 --- a/libexec/tfenv-list +++ b/libexec/tfenv-list @@ -1,10 +1,17 @@ #!/usr/bin/env bash -set -e -[ -n "$TFENV_DEBUG" ] && set -x -if [ $# -ne 0 ];then - echo "usage: tfenv list" 1>&2 +function error_and_die() { + echo -e "${1}" >&2 exit 1 -fi +} + +[ -n "$TFENV_DEBUG" ] && set -x +[ $# -ne 0 ] \ + && error_and_die "usage: tfenv list" + +[ -d "${TFENV_ROOT}/versions" ] \ + || error_and_die "No versions available. Please install one with: tfenv install" + +set -e ls -1 "${TFENV_ROOT}/versions" | sort -t'.' -k 1nr,1 -k 2nr,2 -k 3nr,3 diff --git a/libexec/tfenv-list-remote b/libexec/tfenv-list-remote index 17c7cfb..6fae6a0 100755 --- a/libexec/tfenv-list-remote +++ b/libexec/tfenv-list-remote @@ -7,4 +7,4 @@ if [ $# -ne 0 ];then exit 1 fi -curl -sf https://releases.hashicorp.com/terraform/ | grep -o -E "[0-9]+\.[0-9]+\.[0-9]+(-(rc|beta)[0-9]+)?" | uniq +curl --tlsv1.2 -sf https://releases.hashicorp.com/terraform/ | grep -o -E "[0-9]+\.[0-9]+\.[0-9]+(-(rc|beta)[0-9]+)?" | uniq diff --git a/libexec/tfenv-use b/libexec/tfenv-use index f20d8cb..e3d45ce 100755 --- a/libexec/tfenv-use +++ b/libexec/tfenv-use @@ -1,23 +1,37 @@ #!/usr/bin/env bash -set -e -[ -n "$TFENV_DEBUG" ] && set -x -if [ $# -ne 1 ];then - echo "usage: tfenv use " 1>&2 +function error_and_die() { + echo -e "${1}" >&2 exit 1 -fi +} -version="${1}" -if [ -z "${version}" ];then - echo "version is not specified" 1>&2 - exit 1 +[ -n "$TFENV_DEBUG" ] && set -x + +[ $# -ne 1 ] && error_and_die "usage: tfenv use " + +declare version +if [[ "${1}" =~ ^latest\:.*$ ]]; then + version="${1%%\:*}" + regex="${1##*\:}" +else + version="${1}" fi +[ -d "${TFENV_ROOT}/versions" ] \ + || error_and_die "No versions of terraform installed. Please install one with: tfenv install" + +[ "${version}" == "latest" ] \ + && version="$(\ls "${TFENV_ROOT}/versions" \ + | sort -t'.' -k 1nr,1 -k 2nr,2 -k 3nr,3 \ + | grep -e "${regex}" \ + | head -n 1 + )" + +[ -n "${version}" ] || error_and_die "Version not specified or not found" + target_path=${TFENV_ROOT}/versions/${version} -if [ ! -f ${target_path}/terraform ];then - echo "${version} is not installed" 1>&2 - exit 1 -fi +[ -f ${target_path}/terraform ] \ + || error_and_die "Terraform version ${version} is not installed" echo "${version}" > "${TFENV_ROOT}/version" -terraform --version +terraform --version || error_and_die "'terraform --version' failed. Something is seriously wrong" diff --git a/test/test_install_and_use.sh b/test/test_install_and_use.sh index 262b88e..258d765 100755 --- a/test/test_install_and_use.sh +++ b/test/test_install_and_use.sh @@ -14,6 +14,17 @@ if ! check_version ${v}; then exit 1 fi +echo "### Install latest version with Regex" +cleanup + +v=$(tfenv list-remote | grep 0.8 | head -n 1) +tfenv install latest:^0.8 +tfenv use latest:^0.8 +if ! check_version ${v}; then + echo "Installing latest version ${v} with Regex" 1>&2 + exit 1 +fi + echo "### Install specific version" cleanup @@ -28,7 +39,7 @@ fi echo "### Install .terraform-version" cleanup -v=0.6.15 +v=0.8.8 echo ${v} > ./.terraform-version tfenv install if ! check_version ${v}; then @@ -41,7 +52,7 @@ cleanup v=9.9.9 expected_error_message="'${v}' doesn't exist in remote, please confirm version name." -if [ -z "$(tfenv install ${v} | grep "${expected_error_message}")" ]; then +if [ -z "$(tfenv install ${v} 2>&1 | grep "${expected_error_message}")" ]; then echo "Installing invalid version ${v}" 1>&2 exit 1 fi