Skip to content

Commit

Permalink
Merge pull request #34 from kamatama41/fix-pr-33
Browse files Browse the repository at this point in the history
Fix PR 33
  • Loading branch information
kamatama41 authored Mar 8, 2017
2 parents 0d0f977 + f48c556 commit 3bc360c
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 51 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:<regex>` 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:<regex>` 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
Expand Down
2 changes: 1 addition & 1 deletion libexec/tfenv---version
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion libexec/tfenv-help
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env bash
set -e
[ -n "$TFENV_DEBUG" ] && set -x
echo "Usage: tfenv <command> [<options>]
Expand Down
58 changes: 31 additions & 27 deletions libexec/tfenv-install
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
#!/usr/bin/env bash
set -e
[ -n "$TFENV_DEBUG" ] && set -x

if [ $# -gt 1 ];then
echo "usage: tfenv install [<version>]" 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 [<version>]"

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
Expand All @@ -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
Expand All @@ -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"
17 changes: 12 additions & 5 deletions libexec/tfenv-list
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion libexec/tfenv-list-remote
Original file line number Diff line number Diff line change
Expand Up @@ -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
42 changes: 28 additions & 14 deletions libexec/tfenv-use
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
#!/usr/bin/env bash
set -e
[ -n "$TFENV_DEBUG" ] && set -x

if [ $# -ne 1 ];then
echo "usage: tfenv use <version>" 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 <version>"

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"
15 changes: 13 additions & 2 deletions test/test_install_and_use.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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

0 comments on commit 3bc360c

Please sign in to comment.