Skip to content

Commit

Permalink
Rewrite script initialisation to cleanly handle symlinks and prep all…
Browse files Browse the repository at this point in the history
… scripts for standalone
  • Loading branch information
Zordrak committed Nov 26, 2019
1 parent dd2eced commit e76ca9e
Show file tree
Hide file tree
Showing 21 changed files with 814 additions and 337 deletions.
63 changes: 46 additions & 17 deletions bin/terraform
Original file line number Diff line number Diff line change
@@ -1,37 +1,66 @@
#!/usr/bin/env bash
set -uo pipefail;

# Ensure we can execute standalone
if [ -n "${TFENV_ROOT:-""}" ]; then
if [ -n "${TFENV_DEBUG:-""}" ]; then
[ -n "${TFENV_HELPERS:-""}" ] \
&& log 'debug' "TFENV_ROOT already defined as ${TFENV_ROOT}" \
|| echo "[DEBUG] TFENV_ROOT already defined as ${TFENV_ROOT}" >&2;
fi;
####################################
# Ensure we can execute standalone #
####################################

function early_death() {
echo "[FATAL] ${0}: ${1}" >&2;
exit 1;
};

if [ -z "${TFENV_ROOT:-""}" ]; then
# http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
readlink_f() {
local target_file="${1}";
local file_name;

while [ "${target_file}" != "" ]; do
cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT";
file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT";
target_file="$(readlink "${file_name}")";
done;

echo "$(pwd -P)/${file_name}";
};

TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)";
[ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT";
else
export TFENV_ROOT="$(cd "$(dirname "${0}")/.." && pwd)";
if [ -n "${TFENV_DEBUG:-""}" ]; then
[ -n "${TFENV_HELPERS:-""}" ] \
&& log 'debug' "TFENV_ROOT declared as ${TFENV_ROOT}" \
|| echo "[DEBUG] TFENV_ROOT declared as ${TFENV_ROOT}" >&2;
fi;
TFENV_ROOT="${TFENV_ROOT%/}";
fi;
export TFENV_ROOT;

if [ -n "${TFENV_HELPERS:-""}" ]; then
log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again';
else
[ -n "${TFENV_DEBUG:-""}" ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2;
[ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh";
if source "${TFENV_ROOT}/lib/helpers.sh"; then
log 'debug' 'Helpers sourced successfully';
else
echo "[ERROR] Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2;
exit 1;
early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh";
fi;
fi;

# Ensure libexec and bin are in $PATH
for dir in libexec bin; do
case ":${PATH}:" in
*:${TFENV_ROOT}/${dir}:*) log 'debug' "\$PATH already contains '${TFENV_ROOT}/${dir}', not adding it again";;
*)
log 'debug' "\$PATH does not contain '${TFENV_ROOT}/${dir}', prepending and exporting it now";
export PATH="${TFENV_ROOT}/${dir}:${PATH}";
;;
esac;
done;

#####################
# Begin Script Body #
#####################

log 'debug' "program=\"${0##*/}\"";

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

log 'debug' "Exec: \"${tfenv_path}\" exec \"${@}\"";
exec "${tfenv_path}" exec "${@}" \
Expand Down
56 changes: 39 additions & 17 deletions bin/tfenv
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
#!/usr/bin/env bash
set -euo pipefail;
set -uo pipefail;

declare arg="${1:-""}";
####################################
# Ensure we can execute standalone #
####################################

function early_death() {
echo "[FATAL] ${0}: ${1}" >&2;
exit 1;
};

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

while [ "${target_file}" != "" ]; do
cd "$(dirname ${target_file})";
file_name="$(basename "${target_file}")";
cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT";
file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT";
target_file="$(readlink "${file_name}")";
done;

echo "$(pwd -P)/${file_name}";
}
};

TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)";
[ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT";
else
TFENV_ROOT="${TFENV_ROOT%/}"
fi
TFENV_ROOT="${TFENV_ROOT%/}";
fi;
export TFENV_ROOT;

if [ -n "${TFENV_HELPERS:-""}" ]; then
log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again';
else
[ -n "${TFENV_DEBUG:-""}" ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2;
[ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh";
if source "${TFENV_ROOT}/lib/helpers.sh"; then
log 'debug' 'Helpers sourced successfully';
else
echo "[ERROR] Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2;
exit 1;
early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh";
fi;
fi;

log 'debug' "Prepending ${TFENV_ROOT}/libexec to PATH";
PATH="${TFENV_ROOT}/libexec:${PATH}";
export PATH;
# Ensure libexec and bin are in $PATH
for dir in libexec bin; do
case ":${PATH}:" in
*:${TFENV_ROOT}/${dir}:*) log 'debug' "\$PATH already contains '${TFENV_ROOT}/${dir}', not adding it again";;
*)
log 'debug' "\$PATH does not contain '${TFENV_ROOT}/${dir}', prepending and exporting it now";
export PATH="${TFENV_ROOT}/${dir}:${PATH}";
;;
esac;
done;

#####################
# Begin Script Body #
#####################

declare arg="${1:-""}";

log 'debug' "Setting TFENV_DIR to ${PWD}";
export TFENV_DIR="${PWD}"
Expand All @@ -52,16 +72,18 @@ abort() {
echo "tfenv: ${*}";
fi;
} >&2;
exit 1;
}
};

log 'debug' "tfenv argument is: ${arg}";

case "${arg}" in
"")
log 'debug' 'No argument provided, dumping version and help and aborting';
{
tfenv---version;
tfenv-help;
} | abort;
} | abort && exit 1;
exit 1;
;;
-v | --version )
log 'debug' 'tfenv version requested...';
Expand All @@ -79,7 +101,7 @@ case "${arg}" in
{
echo "No such command '${arg}'";
tfenv-help;
} | abort;
} | abort && exit 1;
fi;
shift 1;
log 'debug' "Exec: \"${command_path}\" \"${@}\"";
Expand Down
2 changes: 1 addition & 1 deletion lib/bashlog.sh
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ function log() {
'error')
echo -e "${std_line}" >&2;
if [ "${debug_level}" -gt 1 ]; then
echo -e "Here's a shell to debug with. 'exit 0' to continue. Other exit codes will abort - parent shell will terminate.";
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;
bash || exit "${?}";
else
exit 1;
Expand Down
25 changes: 23 additions & 2 deletions lib/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@

set -uo pipefail;

if [ -z "${TFENV_ROOT:-""}" ]; then
# http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
readlink_f() {
local target_file="${1}";
local file_name;

while [ "${target_file}" != "" ]; do
cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT";
file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT";
target_file="$(readlink "${file_name}")";
done;

echo "$(pwd -P)/${file_name}";
};

TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)";
[ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT";
else
TFENV_ROOT="${TFENV_ROOT%/}";
fi;
export TFENV_ROOT;

if [ "${TFENV_DEBUG:-0}" -gt 0 ]; then
[ "${DEBUG:-0}" -gt "${TFENV_DEBUG:-0}" ] || export DEBUG="${TFENV_DEBUG:-0}";
if [[ "${TFENV_DEBUG}" -gt 2 ]]; then
Expand All @@ -10,7 +32,6 @@ if [ "${TFENV_DEBUG:-0}" -gt 0 ]; then
fi;
fi;

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

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

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

Expand Down
61 changes: 45 additions & 16 deletions libexec/tfenv---version
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,63 @@

set -uo pipefail;

# Ensure we can execute standalone
if [ -n "${TFENV_ROOT:-""}" ]; then
if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then
[ -n "${TFENV_HELPERS:-""}" ] \
&& log 'debug' "TFENV_ROOT already defined as ${TFENV_ROOT}" \
|| echo "[DEBUG] TFENV_ROOT already defined as ${TFENV_ROOT}" >&2;
fi;
####################################
# Ensure we can execute standalone #
####################################

function early_death() {
echo "[FATAL] ${0}: ${1}" >&2;
exit 1;
};

if [ -z "${TFENV_ROOT:-""}" ]; then
# http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
readlink_f() {
local target_file="${1}";
local file_name;

while [ "${target_file}" != "" ]; do
cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT";
file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT";
target_file="$(readlink "${file_name}")";
done;

echo "$(pwd -P)/${file_name}";
};

TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)";
[ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT";
else
export TFENV_ROOT="$(cd "$(dirname "${0}")/.." && pwd)";
if [ "${TFENV_DEBUG:-0}" -gt 1 ]; then
[ -n "${TFENV_HELPERS:-""}" ] \
&& log 'debug' "TFENV_ROOT declared as ${TFENV_ROOT}" \
|| echo "[DEBUG] TFENV_ROOT declared as ${TFENV_ROOT}" >&2;
fi;
TFENV_ROOT="${TFENV_ROOT%/}";
fi;
export TFENV_ROOT;

if [ -n "${TFENV_HELPERS:-""}" ]; then
log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again';
else
[ "${TFENV_DEBUG:-0}" -gt 1 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2;
[ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh";
if source "${TFENV_ROOT}/lib/helpers.sh"; then
log 'debug' 'Helpers sourced successfully';
else
echo "[ERROR] Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh" >&2;
exit 1;
early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh";
fi;
fi;

# Ensure libexec and bin are in $PATH
for dir in libexec bin; do
case ":${PATH}:" in
*:${TFENV_ROOT}/${dir}:*) log 'debug' "\$PATH already contains '${TFENV_ROOT}/${dir}', not adding it again";;
*)
log 'debug' "\$PATH does not contain '${TFENV_ROOT}/${dir}', prepending and exporting it now";
export PATH="${TFENV_ROOT}/${dir}:${PATH}";
;;
esac;
done;

#####################
# Begin Script Body #
#####################

log 'debug' 'Scraping tfenv version from CHANGELOG.md';
version="$(awk '/^##/{ print $2; exit}' "${TFENV_ROOT}/CHANGELOG.md")" \
&& log 'debug' "Found version '${version}' in CHANGELOG.md" \
Expand Down
Loading

0 comments on commit e76ca9e

Please sign in to comment.