Skip to content

Commit e76ca9e

Browse files
committed
Rewrite script initialisation to cleanly handle symlinks and prep all scripts for standalone
1 parent dd2eced commit e76ca9e

21 files changed

+814
-337
lines changed

bin/terraform

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,66 @@
11
#!/usr/bin/env bash
22
set -uo pipefail;
33

4-
# Ensure we can execute standalone
5-
if [ -n "${TFENV_ROOT:-""}" ]; then
6-
if [ -n "${TFENV_DEBUG:-""}" ]; then
7-
[ -n "${TFENV_HELPERS:-""}" ] \
8-
&& log 'debug' "TFENV_ROOT already defined as ${TFENV_ROOT}" \
9-
|| echo "[DEBUG] TFENV_ROOT already defined as ${TFENV_ROOT}" >&2;
10-
fi;
4+
####################################
5+
# Ensure we can execute standalone #
6+
####################################
7+
8+
function early_death() {
9+
echo "[FATAL] ${0}: ${1}" >&2;
10+
exit 1;
11+
};
12+
13+
if [ -z "${TFENV_ROOT:-""}" ]; then
14+
# http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
15+
readlink_f() {
16+
local target_file="${1}";
17+
local file_name;
18+
19+
while [ "${target_file}" != "" ]; do
20+
cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT";
21+
file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT";
22+
target_file="$(readlink "${file_name}")";
23+
done;
24+
25+
echo "$(pwd -P)/${file_name}";
26+
};
27+
28+
TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)";
29+
[ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT";
1130
else
12-
export TFENV_ROOT="$(cd "$(dirname "${0}")/.." && pwd)";
13-
if [ -n "${TFENV_DEBUG:-""}" ]; then
14-
[ -n "${TFENV_HELPERS:-""}" ] \
15-
&& log 'debug' "TFENV_ROOT declared as ${TFENV_ROOT}" \
16-
|| echo "[DEBUG] TFENV_ROOT declared as ${TFENV_ROOT}" >&2;
17-
fi;
31+
TFENV_ROOT="${TFENV_ROOT%/}";
1832
fi;
33+
export TFENV_ROOT;
1934

2035
if [ -n "${TFENV_HELPERS:-""}" ]; then
2136
log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again';
2237
else
23-
[ -n "${TFENV_DEBUG:-""}" ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2;
38+
[ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh";
2439
if source "${TFENV_ROOT}/lib/helpers.sh"; then
2540
log 'debug' 'Helpers sourced successfully';
2641
else
27-
echo "[ERROR] Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2;
28-
exit 1;
42+
early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh";
2943
fi;
3044
fi;
3145

46+
# Ensure libexec and bin are in $PATH
47+
for dir in libexec bin; do
48+
case ":${PATH}:" in
49+
*:${TFENV_ROOT}/${dir}:*) log 'debug' "\$PATH already contains '${TFENV_ROOT}/${dir}', not adding it again";;
50+
*)
51+
log 'debug' "\$PATH does not contain '${TFENV_ROOT}/${dir}', prepending and exporting it now";
52+
export PATH="${TFENV_ROOT}/${dir}:${PATH}";
53+
;;
54+
esac;
55+
done;
56+
57+
#####################
58+
# Begin Script Body #
59+
#####################
60+
3261
log 'debug' "program=\"${0##*/}\"";
3362

34-
declare tfenv_path="$(dirname "$(command -v "${0}")")/tfenv";
63+
declare tfenv_path="${TFENV_ROOT}/bin/tfenv";
3564

3665
log 'debug' "Exec: \"${tfenv_path}\" exec \"${@}\"";
3766
exec "${tfenv_path}" exec "${@}" \

bin/tfenv

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
#!/usr/bin/env bash
2-
set -euo pipefail;
2+
set -uo pipefail;
33

4-
declare arg="${1:-""}";
4+
####################################
5+
# Ensure we can execute standalone #
6+
####################################
7+
8+
function early_death() {
9+
echo "[FATAL] ${0}: ${1}" >&2;
10+
exit 1;
11+
};
512

613
if [ -z "${TFENV_ROOT:-""}" ]; then
714
# http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
@@ -10,35 +17,48 @@ if [ -z "${TFENV_ROOT:-""}" ]; then
1017
local file_name;
1118

1219
while [ "${target_file}" != "" ]; do
13-
cd "$(dirname ${target_file})";
14-
file_name="$(basename "${target_file}")";
20+
cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT";
21+
file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT";
1522
target_file="$(readlink "${file_name}")";
1623
done;
1724

1825
echo "$(pwd -P)/${file_name}";
19-
}
26+
};
2027

2128
TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)";
29+
[ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT";
2230
else
23-
TFENV_ROOT="${TFENV_ROOT%/}"
24-
fi
31+
TFENV_ROOT="${TFENV_ROOT%/}";
32+
fi;
2533
export TFENV_ROOT;
2634

2735
if [ -n "${TFENV_HELPERS:-""}" ]; then
2836
log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again';
2937
else
30-
[ -n "${TFENV_DEBUG:-""}" ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2;
38+
[ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh";
3139
if source "${TFENV_ROOT}/lib/helpers.sh"; then
3240
log 'debug' 'Helpers sourced successfully';
3341
else
34-
echo "[ERROR] Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2;
35-
exit 1;
42+
early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh";
3643
fi;
3744
fi;
3845

39-
log 'debug' "Prepending ${TFENV_ROOT}/libexec to PATH";
40-
PATH="${TFENV_ROOT}/libexec:${PATH}";
41-
export PATH;
46+
# Ensure libexec and bin are in $PATH
47+
for dir in libexec bin; do
48+
case ":${PATH}:" in
49+
*:${TFENV_ROOT}/${dir}:*) log 'debug' "\$PATH already contains '${TFENV_ROOT}/${dir}', not adding it again";;
50+
*)
51+
log 'debug' "\$PATH does not contain '${TFENV_ROOT}/${dir}', prepending and exporting it now";
52+
export PATH="${TFENV_ROOT}/${dir}:${PATH}";
53+
;;
54+
esac;
55+
done;
56+
57+
#####################
58+
# Begin Script Body #
59+
#####################
60+
61+
declare arg="${1:-""}";
4262

4363
log 'debug' "Setting TFENV_DIR to ${PWD}";
4464
export TFENV_DIR="${PWD}"
@@ -52,16 +72,18 @@ abort() {
5272
echo "tfenv: ${*}";
5373
fi;
5474
} >&2;
55-
exit 1;
56-
}
75+
};
76+
77+
log 'debug' "tfenv argument is: ${arg}";
5778

5879
case "${arg}" in
5980
"")
6081
log 'debug' 'No argument provided, dumping version and help and aborting';
6182
{
6283
tfenv---version;
6384
tfenv-help;
64-
} | abort;
85+
} | abort && exit 1;
86+
exit 1;
6587
;;
6688
-v | --version )
6789
log 'debug' 'tfenv version requested...';
@@ -79,7 +101,7 @@ case "${arg}" in
79101
{
80102
echo "No such command '${arg}'";
81103
tfenv-help;
82-
} | abort;
104+
} | abort && exit 1;
83105
fi;
84106
shift 1;
85107
log 'debug' "Exec: \"${command_path}\" \"${@}\"";

lib/bashlog.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ function log() {
147147
'error')
148148
echo -e "${std_line}" >&2;
149149
if [ "${debug_level}" -gt 1 ]; then
150-
echo -e "Here's a shell to debug with. 'exit 0' to continue. Other exit codes will abort - parent shell will terminate.";
150+
echo -e "Here's a shell for debugging the current environment. 'exit 0' to resume script from here. Non-zero exit code will abort - parent shell will terminate." >&2;
151151
bash || exit "${?}";
152152
else
153153
exit 1;

lib/helpers.sh

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,28 @@
22

33
set -uo pipefail;
44

5+
if [ -z "${TFENV_ROOT:-""}" ]; then
6+
# http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
7+
readlink_f() {
8+
local target_file="${1}";
9+
local file_name;
10+
11+
while [ "${target_file}" != "" ]; do
12+
cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT";
13+
file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT";
14+
target_file="$(readlink "${file_name}")";
15+
done;
16+
17+
echo "$(pwd -P)/${file_name}";
18+
};
19+
20+
TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)";
21+
[ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT";
22+
else
23+
TFENV_ROOT="${TFENV_ROOT%/}";
24+
fi;
25+
export TFENV_ROOT;
26+
527
if [ "${TFENV_DEBUG:-0}" -gt 0 ]; then
628
[ "${DEBUG:-0}" -gt "${TFENV_DEBUG:-0}" ] || export DEBUG="${TFENV_DEBUG:-0}";
729
if [[ "${TFENV_DEBUG}" -gt 2 ]]; then
@@ -10,7 +32,6 @@ if [ "${TFENV_DEBUG:-0}" -gt 0 ]; then
1032
fi;
1133
fi;
1234

13-
[ -n "${TFENV_ROOT:-""}" ] || export TFENV_ROOT="$(cd "$(dirname "${0}")/.." && pwd)"
1435
source "${TFENV_ROOT}/lib/bashlog.sh";
1536

1637
# Curl wrapper to switch TLS option for each OS
@@ -28,7 +49,7 @@ export -f curlw;
2849

2950
check_version() {
3051
v="${1}";
31-
[ -n "$(terraform --version | grep -E "^Terraform v${v}((-dev)|( \([a-f0-9]+\)))?$")" ];
52+
[ -n "$(${TFENV_ROOT}/bin/terraform --version | grep -E "^Terraform v${v}((-dev)|( \([a-f0-9]+\)))?$")" ];
3253
}
3354
export -f check_version;
3455

libexec/tfenv---version

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,63 @@
1111

1212
set -uo pipefail;
1313

14-
# Ensure we can execute standalone
15-
if [ -n "${TFENV_ROOT:-""}" ]; then
16-
if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then
17-
[ -n "${TFENV_HELPERS:-""}" ] \
18-
&& log 'debug' "TFENV_ROOT already defined as ${TFENV_ROOT}" \
19-
|| echo "[DEBUG] TFENV_ROOT already defined as ${TFENV_ROOT}" >&2;
20-
fi;
14+
####################################
15+
# Ensure we can execute standalone #
16+
####################################
17+
18+
function early_death() {
19+
echo "[FATAL] ${0}: ${1}" >&2;
20+
exit 1;
21+
};
22+
23+
if [ -z "${TFENV_ROOT:-""}" ]; then
24+
# http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
25+
readlink_f() {
26+
local target_file="${1}";
27+
local file_name;
28+
29+
while [ "${target_file}" != "" ]; do
30+
cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT";
31+
file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT";
32+
target_file="$(readlink "${file_name}")";
33+
done;
34+
35+
echo "$(pwd -P)/${file_name}";
36+
};
37+
38+
TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)";
39+
[ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT";
2140
else
22-
export TFENV_ROOT="$(cd "$(dirname "${0}")/.." && pwd)";
23-
if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then
24-
[ -n "${TFENV_HELPERS:-""}" ] \
25-
&& log 'debug' "TFENV_ROOT declared as ${TFENV_ROOT}" \
26-
|| echo "[DEBUG] TFENV_ROOT declared as ${TFENV_ROOT}" >&2;
27-
fi;
41+
TFENV_ROOT="${TFENV_ROOT%/}";
2842
fi;
43+
export TFENV_ROOT;
2944

3045
if [ -n "${TFENV_HELPERS:-""}" ]; then
3146
log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again';
3247
else
33-
[ "${TFENV_DEBUG:-0}" -gt 1 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2;
48+
[ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh";
3449
if source "${TFENV_ROOT}/lib/helpers.sh"; then
3550
log 'debug' 'Helpers sourced successfully';
3651
else
37-
echo "[ERROR] Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2;
38-
exit 1;
52+
early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh";
3953
fi;
4054
fi;
4155

56+
# Ensure libexec and bin are in $PATH
57+
for dir in libexec bin; do
58+
case ":${PATH}:" in
59+
*:${TFENV_ROOT}/${dir}:*) log 'debug' "\$PATH already contains '${TFENV_ROOT}/${dir}', not adding it again";;
60+
*)
61+
log 'debug' "\$PATH does not contain '${TFENV_ROOT}/${dir}', prepending and exporting it now";
62+
export PATH="${TFENV_ROOT}/${dir}:${PATH}";
63+
;;
64+
esac;
65+
done;
66+
67+
#####################
68+
# Begin Script Body #
69+
#####################
70+
4271
log 'debug' 'Scraping tfenv version from CHANGELOG.md';
4372
version="$(awk '/^##/{ print $2; exit}' "${TFENV_ROOT}/CHANGELOG.md")" \
4473
&& log 'debug' "Found version '${version}' in CHANGELOG.md" \

0 commit comments

Comments
 (0)