Skip to content

Commit 3bc360c

Browse files
authored
Merge pull request #34 from kamatama41/fix-pr-33
Fix PR 33
2 parents 0d0f977 + f48c556 commit 3bc360c

File tree

8 files changed

+92
-51
lines changed

8 files changed

+92
-51
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,23 @@ Currently tfenv supports the following OSes
3232
### tfenv install
3333
Install a specific version of Terraform
3434
`latest` is a syntax to install latest version
35+
`latest:<regex>` is a syntax to install latest version matching regex (used by grep -e)
3536
```sh
3637
$ tfenv install 0.7.0
3738
$ tfenv install latest
39+
$ tfenv install latest:^0.8
3840
```
3941

4042
If you use [.terraform-version](#terraform-version), `tfenv install` (no argument) will install the version written in it.
4143

4244
### tfenv use
4345
Switch a version to use
46+
`latest` is a syntax to use the latest installed version
47+
`latest:<regex>` is a syntax to use latest installed version matching regex (used by grep -e)
4448
```sh
4549
$ tfenv use 0.7.0
50+
$ tfenv use latest
51+
$ tfenv use latest:^0.8
4652
```
4753

4854
### tfenv list

libexec/tfenv---version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
set -e
1313
[ -n "$TFENV_DEBUG" ] && set -x
1414

15-
version="0.3.4"
15+
version="0.4.0"
1616
git_revision=""
1717

1818
if cd "${BASH_SOURCE%/*}" 2>/dev/null && git remote -v 2>/dev/null | grep -q tfenv; then

libexec/tfenv-help

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env bash
2-
set -e
32
[ -n "$TFENV_DEBUG" ] && set -x
43
echo "Usage: tfenv <command> [<options>]
54

libexec/tfenv-install

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
#!/usr/bin/env bash
2-
set -e
3-
[ -n "$TFENV_DEBUG" ] && set -x
42

5-
if [ $# -gt 1 ];then
6-
echo "usage: tfenv install [<version>]" 1>&2
3+
function error_and_die() {
4+
echo -e "${1}" >&2
75
exit 1
6+
}
7+
8+
[ -n "$TFENV_DEBUG" ] && set -x
9+
10+
[ $# -gt 1 ] && error_and_die "usage: tfenv install [<version>]"
11+
12+
declare version regex
13+
if [[ "${1}" =~ ^latest\:.*$ ]]; then
14+
version="${1%%\:*}"
15+
regex="${1##*\:}"
16+
else
17+
version="${1}"
818
fi
919

10-
version="${1}"
11-
if [ "${version}" == "latest" ];then
12-
version=$(tfenv-list-remote | head -n 1)
20+
if [ ${version} == "latest" ]; then
21+
version="$(tfenv-list-remote | grep -e "${regex}" | head -n 1)"
22+
[ -n "${version}" ] || error_and_die "No matching version found in remote"
23+
elif [ -z "$(tfenv-list-remote | grep "${version}")" ]; then
24+
error_and_die "Terraform version '${version}' doesn't exist in remote, please confirm version name."
1325
fi
1426

1527
if [ -z "${version}" ]; then
@@ -19,20 +31,12 @@ if [ -z "${version}" ]; then
1931
fi
2032
fi
2133

22-
if [ -z "${version}" ];then
23-
echo "version is not specified" 1>&2
24-
exit 1
25-
fi
34+
[ -n "${version}" ] || error_and_die "Version is not specified"
2635

27-
dst_path=${TFENV_ROOT}/versions/${version}
36+
dst_path="${TFENV_ROOT}/versions/${version}"
2837
if [ -f ${dst_path}/terraform ];then
29-
echo "already installed ${version}"
30-
exit
31-
fi
32-
33-
if [ -z "$(tfenv-list-remote | grep "${version}")" ];then
34-
echo "'${version}' doesn't exist in remote, please confirm version name."
35-
exit 1
38+
echo "Terraform v${version} is already installed"
39+
exit 0
3640
fi
3741

3842
case "$(uname -s)" in
@@ -46,11 +50,11 @@ MINGW64* )
4650
os="linux_amd64"
4751
esac
4852

49-
archive_name="terraform_${version}_${os}.zip"
50-
archive_url="https://releases.hashicorp.com/terraform/${version}/${archive_name}"
51-
echo "install Terraform ${version}"
52-
echo "get archive from ${archive_url}"
53-
curl -f -o /tmp/${archive_name} "${archive_url}"
54-
mkdir -p ${dst_path}
55-
unzip /tmp/${archive_name} -d ${dst_path}
56-
echo -e "\033[0;32mthe installation ${version} was successful!!!\033[0;39m"
53+
tarball_name="terraform_${version}_${os}.zip"
54+
tarball_url="https://releases.hashicorp.com/terraform/${version}/${tarball_name}"
55+
echo "Installing Terraform v${version}"
56+
echo "Downloading release tarball from ${tarball_url}"
57+
curl --tlsv1.2 -f -o /tmp/${tarball_name} "${tarball_url}" || error_and_die "Tarball download failed"
58+
mkdir -p ${dst_path} || error_and_die "Failed to make directory ${dst_path}"
59+
unzip /tmp/${tarball_name} -d ${dst_path} || error_and_die "Tarball unzip failed"
60+
echo -e "\033[0;32mInstallation of terraform v${version} successful\033[0;39m"

libexec/tfenv-list

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
#!/usr/bin/env bash
2-
set -e
3-
[ -n "$TFENV_DEBUG" ] && set -x
42

5-
if [ $# -ne 0 ];then
6-
echo "usage: tfenv list" 1>&2
3+
function error_and_die() {
4+
echo -e "${1}" >&2
75
exit 1
8-
fi
6+
}
7+
8+
[ -n "$TFENV_DEBUG" ] && set -x
99

10+
[ $# -ne 0 ] \
11+
&& error_and_die "usage: tfenv list"
12+
13+
[ -d "${TFENV_ROOT}/versions" ] \
14+
|| error_and_die "No versions available. Please install one with: tfenv install"
15+
16+
set -e
1017
ls -1 "${TFENV_ROOT}/versions" | sort -t'.' -k 1nr,1 -k 2nr,2 -k 3nr,3

libexec/tfenv-list-remote

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ if [ $# -ne 0 ];then
77
exit 1
88
fi
99

10-
curl -sf https://releases.hashicorp.com/terraform/ | grep -o -E "[0-9]+\.[0-9]+\.[0-9]+(-(rc|beta)[0-9]+)?" | uniq
10+
curl --tlsv1.2 -sf https://releases.hashicorp.com/terraform/ | grep -o -E "[0-9]+\.[0-9]+\.[0-9]+(-(rc|beta)[0-9]+)?" | uniq

libexec/tfenv-use

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
11
#!/usr/bin/env bash
2-
set -e
3-
[ -n "$TFENV_DEBUG" ] && set -x
42

5-
if [ $# -ne 1 ];then
6-
echo "usage: tfenv use <version>" 1>&2
3+
function error_and_die() {
4+
echo -e "${1}" >&2
75
exit 1
8-
fi
6+
}
97

10-
version="${1}"
11-
if [ -z "${version}" ];then
12-
echo "version is not specified" 1>&2
13-
exit 1
8+
[ -n "$TFENV_DEBUG" ] && set -x
9+
10+
[ $# -ne 1 ] && error_and_die "usage: tfenv use <version>"
11+
12+
declare version
13+
if [[ "${1}" =~ ^latest\:.*$ ]]; then
14+
version="${1%%\:*}"
15+
regex="${1##*\:}"
16+
else
17+
version="${1}"
1418
fi
1519

20+
[ -d "${TFENV_ROOT}/versions" ] \
21+
|| error_and_die "No versions of terraform installed. Please install one with: tfenv install"
22+
23+
[ "${version}" == "latest" ] \
24+
&& version="$(\ls "${TFENV_ROOT}/versions" \
25+
| sort -t'.' -k 1nr,1 -k 2nr,2 -k 3nr,3 \
26+
| grep -e "${regex}" \
27+
| head -n 1
28+
)"
29+
30+
[ -n "${version}" ] || error_and_die "Version not specified or not found"
31+
1632
target_path=${TFENV_ROOT}/versions/${version}
17-
if [ ! -f ${target_path}/terraform ];then
18-
echo "${version} is not installed" 1>&2
19-
exit 1
20-
fi
33+
[ -f ${target_path}/terraform ] \
34+
|| error_and_die "Terraform version ${version} is not installed"
2135

2236
echo "${version}" > "${TFENV_ROOT}/version"
23-
terraform --version
37+
terraform --version || error_and_die "'terraform --version' failed. Something is seriously wrong"

test/test_install_and_use.sh

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ if ! check_version ${v}; then
1414
exit 1
1515
fi
1616

17+
echo "### Install latest version with Regex"
18+
cleanup
19+
20+
v=$(tfenv list-remote | grep 0.8 | head -n 1)
21+
tfenv install latest:^0.8
22+
tfenv use latest:^0.8
23+
if ! check_version ${v}; then
24+
echo "Installing latest version ${v} with Regex" 1>&2
25+
exit 1
26+
fi
27+
1728
echo "### Install specific version"
1829
cleanup
1930

@@ -28,7 +39,7 @@ fi
2839
echo "### Install .terraform-version"
2940
cleanup
3041

31-
v=0.6.15
42+
v=0.8.8
3243
echo ${v} > ./.terraform-version
3344
tfenv install
3445
if ! check_version ${v}; then
@@ -41,7 +52,7 @@ cleanup
4152

4253
v=9.9.9
4354
expected_error_message="'${v}' doesn't exist in remote, please confirm version name."
44-
if [ -z "$(tfenv install ${v} | grep "${expected_error_message}")" ]; then
55+
if [ -z "$(tfenv install ${v} 2>&1 | grep "${expected_error_message}")" ]; then
4556
echo "Installing invalid version ${v}" 1>&2
4657
exit 1
4758
fi

0 commit comments

Comments
 (0)