Skip to content

Commit 8a86d9e

Browse files
authored
Merge pull request #35 from cartest/master
Additional development of latest. Bash standards. Revert to upstream README.md. Add CHANGELOG.md. Develop Test Suite.
2 parents 3bc360c + f82abeb commit 8a86d9e

16 files changed

+280
-122
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## 0.4.1 (March 8, 2017)
2+
3+
* Update error_and_die functions to better report their source location
4+
* libexec/tfenv-version-name: Respect `latest` & `latest:<regex>` syntax in .terraform-version
5+
* Extension and development of test suite standards
6+
7+
## 0.4.0 (March 6, 2017)
8+
9+
* Add capability to specify `latest` or `latest:<regex>` in the `use` and `install` actions.
10+
* Add error_and_die functions to standardise error output
11+
* Specify --tlsv1.2 to curl requests to remote repo. TLSv1.2 required; supported by but not auto-selected by older NSS.

README.md

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ $ tfenv use latest:^0.8
5555
List installed versions
5656
```sh
5757
% tfenv list
58+
0.9.0-beta2
59+
0.8.8
60+
0.8.4
5861
0.7.0
5962
0.7.0-rc4
6063
0.6.16
@@ -66,20 +69,29 @@ List installed versions
6669
List installable versions
6770
```sh
6871
% tfenv list-remote
69-
0.7.0
70-
0.7.0-rc4
71-
0.7.0-rc3
72-
0.7.0-rc2
73-
0.7.0-rc1
74-
0.6.16
75-
0.6.15
76-
0.6.14
77-
0.6.13
72+
0.9.0-beta2
73+
0.9.0-beta1
74+
0.8.8
75+
0.8.7
76+
0.8.6
77+
0.8.5
78+
0.8.4
79+
0.8.3
80+
0.8.2
81+
0.8.1
82+
0.8.0
83+
0.8.0-rc3
84+
0.8.0-rc2
85+
0.8.0-rc1
86+
0.8.0-beta2
87+
0.8.0-beta1
88+
0.7.13
89+
0.7.12
7890
...
7991
```
8092

8193
## .terraform-version
82-
If you put `.terraform-version` file on your project root, tfenv detects it and use the version written in it.
94+
If you put `.terraform-version` file on your project root, tfenv detects it and use the version written in it. If the version is `latest` or `latest:<regex>`, the latest matching version currently installed will be selected.
8395

8496
```sh
8597
$ cat .terraform-version
@@ -96,6 +108,10 @@ $ echo 0.7.3 > .terraform-version
96108
$ terraform --version
97109
Terraform v0.7.3
98110

111+
$ echo latest:^0.8 > .terraform-version
112+
113+
$ terraform --version
114+
Terraform v0.8.8
99115
```
100116

101117
## Upgrading
@@ -112,3 +128,4 @@ $ rm -rf /some/path/to/tfenv
112128
- [tfenv itself](https://github.com/kamatama41/tfenv/blob/master/LICENSE)
113129
- [rbenv](https://github.com/rbenv/rbenv/blob/master/LICENSE)
114130
- tfenv partially uses rbenv's source code
131+

libexec/tfenv---version

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
# tagged.
1111

1212
set -e
13-
[ -n "$TFENV_DEBUG" ] && set -x
13+
[ -n "${TFENV_DEBUG}" ] && set -x
1414

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

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

libexec/tfenv-exec

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414
# PATH="$TFENV_ROOT/versions/0.7.0/bin:$PATH" terraform plan
1515

1616
set -e
17-
[ -n "$TFENV_DEBUG" ] && set -x
17+
[ -n "${TFENV_DEBUG}" ] && set -x
1818

19-
TFENV_VERSION="$(tfenv-version-name)"
20-
export TFENV_VERSION
19+
export TFENV_VERSION="$(tfenv-version-name)"
2120
TF_BIN_PATH="${TFENV_ROOT}/versions/${TFENV_VERSION}/terraform"
2221
export PATH="${TF_BIN_PATH}:${PATH}"
23-
"$TF_BIN_PATH" "$@"
22+
"${TF_BIN_PATH}" "${@}"

libexec/tfenv-help

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env bash
2-
[ -n "$TFENV_DEBUG" ] && set -x
2+
3+
[ -n "${TFENV_DEBUG}" ] && set -x
4+
35
echo "Usage: tfenv <command> [<options>]
46
57
Commands:

libexec/tfenv-install

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
11
#!/usr/bin/env bash
22

33
function error_and_die() {
4-
echo -e "${1}" >&2
4+
echo -e "tfenv: ${0}: ${1}" >&2
55
exit 1
66
}
77

8-
[ -n "$TFENV_DEBUG" ] && set -x
8+
[ -n "${TFENV_DEBUG}" ] && set -x
99

10-
[ $# -gt 1 ] && error_and_die "usage: tfenv install [<version>]"
10+
[ ${#} -gt 1 ] && error_and_die "usage: tfenv install [<version>]"
1111

12-
declare version regex
13-
if [[ "${1}" =~ ^latest\:.*$ ]]; then
14-
version="${1%%\:*}"
15-
regex="${1##*\:}"
16-
else
17-
version="${1}"
18-
fi
19-
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."
25-
fi
12+
declare version_requested version regex
2613

27-
if [ -z "${version}" ]; then
14+
if [ -z "${1}" ]; then
2815
version_file="$(tfenv-version-file)"
2916
if [ "${version_file}" != "${TFENV_ROOT}/version" ];then
30-
version="$(cat ${version_file} || true)"
17+
version_requested="$(cat ${version_file} || true)"
3118
fi
19+
else
20+
version_requested="${1}"
21+
fi
22+
23+
if [[ "${version_requested}" =~ ^latest\:.*$ ]]; then
24+
version="${version_requested%%\:*}"
25+
regex="${version_requested##*\:}"
26+
elif [[ "${version_requested}" =~ ^latest$ ]]; then
27+
version="${version_requested}"
28+
regex=""
29+
else
30+
version="${version_requested}"
31+
regex="${version_requested}"
3232
fi
3333

3434
[ -n "${version}" ] || error_and_die "Version is not specified"
35+
version="$(tfenv-list-remote | grep -e "${regex}" | head -n 1)"
36+
[ -n "${version}" ] || error_and_die "No versions matching '${1}' found in remote"
3537

3638
dst_path="${TFENV_ROOT}/versions/${version}"
3739
if [ -f ${dst_path}/terraform ];then

libexec/tfenv-list

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
#!/usr/bin/env bash
22

33
function error_and_die() {
4-
echo -e "${1}" >&2
4+
echo -e "tfenv: ${0}: ${1}" >&2
55
exit 1
66
}
77

8-
[ -n "$TFENV_DEBUG" ] && set -x
8+
[ -n "${TFENV_DEBUG}" ] && set -x
99

10-
[ $# -ne 0 ] \
10+
[ ${#} -ne 0 ] \
1111
&& error_and_die "usage: tfenv list"
1212

1313
[ -d "${TFENV_ROOT}/versions" ] \
1414
|| error_and_die "No versions available. Please install one with: tfenv install"
1515

16-
set -e
16+
[[ -x "${TFENV_ROOT}/versions" && -r "${TFENV_ROOT}/versions" ]] \
17+
|| error_and_die "tfenv versions directory is inaccessible!"
18+
1719
ls -1 "${TFENV_ROOT}/versions" | sort -t'.' -k 1nr,1 -k 2nr,2 -k 3nr,3

libexec/tfenv-list-remote

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env bash
22
set -e
3-
[ -n "$TFENV_DEBUG" ] && set -x
3+
[ -n "${TFENV_DEBUG}" ] && set -x
44

5-
if [ $# -ne 0 ];then
5+
if [ ${#} -ne 0 ];then
66
echo "usage: tfenv list-remote" 1>&2
77
exit 1
88
fi

libexec/tfenv-use

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,45 @@
11
#!/usr/bin/env bash
22

33
function error_and_die() {
4-
echo -e "${1}" >&2
4+
echo -e "tfenv: ${0}: ${1}" >&2
55
exit 1
66
}
77

8-
[ -n "$TFENV_DEBUG" ] && set -x
8+
[ -n "${TFENV_DEBUG}" ] && set -x
99

10-
[ $# -ne 1 ] && error_and_die "usage: tfenv use <version>"
10+
[ ${#} -ne 1 ] && error_and_die "usage: tfenv use <version>"
1111

12-
declare version
13-
if [[ "${1}" =~ ^latest\:.*$ ]]; then
14-
version="${1%%\:*}"
15-
regex="${1##*\:}"
12+
declare version_requested version regex
13+
14+
version_requested="${1}"
15+
16+
if [[ "${version_requested}" =~ ^latest\:.*$ ]]; then
17+
version="${version_requested%%\:*}"
18+
regex="${version_requested##*\:}"
19+
elif [[ "${version_requested}" =~ ^latest$ ]]; then
20+
version="${version_requested}"
21+
regex=""
1622
else
17-
version="${1}"
23+
version="${version_requested}"
24+
regex="${version_requested}"
1825
fi
1926

2027
[ -d "${TFENV_ROOT}/versions" ] \
2128
|| error_and_die "No versions of terraform installed. Please install one with: tfenv install"
2229

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-
)"
30+
version="$(\ls "${TFENV_ROOT}/versions" \
31+
| sort -t'.' -k 1nr,1 -k 2nr,2 -k 3nr,3 \
32+
| grep -e "${regex}" \
33+
| head -n 1
34+
)"
2935

30-
[ -n "${version}" ] || error_and_die "Version not specified or not found"
36+
[ -n "${version}" ] || error_and_die "No installed versions of terraform matched '${1}'"
3137

3238
target_path=${TFENV_ROOT}/versions/${version}
3339
[ -f ${target_path}/terraform ] \
34-
|| error_and_die "Terraform version ${version} is not installed"
40+
|| error_and_die "Version directory for ${version} is present, but the terraform binary is not! Manual intervention required."
41+
[ -x ${target_path}/terraform ] \
42+
|| error_and_die "Version directory for ${version} is present, but the terraform binary is not executable! Manual intervention required. "
3543

3644
echo "${version}" > "${TFENV_ROOT}/version"
3745
terraform --version || error_and_die "'terraform --version' failed. Something is seriously wrong"

libexec/tfenv-version-file

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
# Usage: tfenv version-file
33
# Summary: Detect the file that sets the current tfenv version
44
set -e
5-
[ -n "$TFENV_DEBUG" ] && set -x
5+
[ -n "${TFENV_DEBUG}" ] && set -x
66

77
find_local_version_file() {
8-
local root="$1"
9-
while ! [[ "$root" =~ ^//[^/]*$ ]]; do
8+
local root="${1}"
9+
while ! [[ "${root}" =~ ^//[^/]*$ ]]; do
1010
if [ -e "${root}/.terraform-version" ]; then
1111
echo "${root}/.terraform-version"
1212
return 0
1313
fi
14-
[ -n "$root" ] || break
14+
[ -n "${root}" ] || break
1515
root="${root%/*}"
1616
done
1717
return 1
1818
}
1919

20-
find_local_version_file "$TFENV_DIR" || echo "${TFENV_ROOT}/version"
20+
find_local_version_file "${TFENV_DIR}" || echo "${TFENV_ROOT}/version"

0 commit comments

Comments
 (0)