Skip to content

Commit 1d6c853

Browse files
authored
Merge pull request #270 from nitrocode/tfenv-pin
Add `tfenv pin` command
2 parents 3769e99 + 8abfe0e commit 1d6c853

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

libexec/tfenv-help

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Commands:
1212
list-remote List all installable versions
1313
version-name Print current version
1414
init Update environment to use 'tfenv' correctly.
15+
pin Write the current active version to ./.terraform-version
1516
';
1617

1718
exit 0;

libexec/tfenv-pin

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env bash
2+
3+
set -uo pipefail;
4+
5+
####################################
6+
# Ensure we can execute standalone #
7+
####################################
8+
9+
function early_death() {
10+
echo "[FATAL] ${0}: ${1}" >&2;
11+
exit 1;
12+
};
13+
14+
if [ -z "${TFENV_ROOT:-""}" ]; then
15+
# http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
16+
readlink_f() {
17+
local target_file="${1}";
18+
local file_name;
19+
20+
while [ "${target_file}" != "" ]; do
21+
cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT";
22+
file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT";
23+
target_file="$(readlink "${file_name}")";
24+
done;
25+
26+
echo "$(pwd -P)/${file_name}";
27+
};
28+
29+
TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)";
30+
[ -n "${TFENV_ROOT}" ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT";
31+
else
32+
TFENV_ROOT="${TFENV_ROOT%/}";
33+
fi;
34+
export TFENV_ROOT;
35+
36+
if [ -n "${TFENV_HELPERS:-""}" ]; then
37+
log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again';
38+
else
39+
[ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh";
40+
if source "${TFENV_ROOT}/lib/helpers.sh"; then
41+
log 'debug' 'Helpers sourced successfully';
42+
else
43+
early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh";
44+
fi;
45+
fi;
46+
47+
# Ensure libexec and bin are in $PATH
48+
for dir in libexec bin; do
49+
case ":${PATH}:" in
50+
*:${TFENV_ROOT}/${dir}:*) log 'debug' "\$PATH already contains '${TFENV_ROOT}/${dir}', not adding it again";;
51+
*)
52+
log 'debug' "\$PATH does not contain '${TFENV_ROOT}/${dir}', prepending and exporting it now";
53+
export PATH="${TFENV_ROOT}/${dir}:${PATH}";
54+
;;
55+
esac;
56+
done;
57+
58+
#####################
59+
# Begin Script Body #
60+
#####################
61+
62+
[ "${#}" -ne 0 ] \
63+
&& log 'error' "usage: tfenv pin"
64+
65+
[ -d "${TFENV_ROOT}/versions/" ] \
66+
|| log 'error' 'No versions available. Please install one with: tfenv install'
67+
68+
[[ -x "${TFENV_ROOT}/versions" && -r "${TFENV_ROOT}/versions" ]] \
69+
|| log 'error' "tfenv versions directory is inaccessible: ${TFENV_ROOT}/versions";
70+
71+
version_name="$(tfenv-version-name 2>/dev/null || true)" \
72+
&& log 'debug' "tfenv-version-name reported: ${version_name}";
73+
74+
echo "${version_name}" > .terraform-version;
75+
log 'info' "Pinned version by writing \"${version_name}\" to $(pwd)/.terraform-version";
76+
77+
exit 0;

0 commit comments

Comments
 (0)