-
Notifications
You must be signed in to change notification settings - Fork 456
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A shot at v2.0.0-beta2 without use-on-install
- Loading branch information
Showing
11 changed files
with
233 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
#!/usr/bin/env bash | ||
# Usage: tfenv resolve-version [<version>] | ||
# Summary: Resolve the version to action based on the environment and optional input token | ||
|
||
set -uo pipefail; | ||
|
||
#################################### | ||
# 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 | ||
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 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 | ||
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 # | ||
##################### | ||
|
||
declare version_requested version regex min_required version_file; | ||
|
||
declare arg="${1:-""}"; | ||
|
||
if [ -z "${arg}" ]; then | ||
version_file="$(tfenv-version-file)"; | ||
log 'debug' "Version File: ${version_file}"; | ||
|
||
if [ "${version_file}" != "${TFENV_ROOT}/version" ]; then | ||
log 'debug' "Version File (${version_file}) is not the default \${TFENV_ROOT}/version (${TFENV_ROOT}/version)"; | ||
version_requested="$(cat "${version_file}")" \ | ||
|| log 'error' "Failed to open ${version_file}"; | ||
|
||
elif [ -f "${version_file}" ]; then | ||
log 'debug' "Version File is the default \${TFENV_ROOT}/version (${TFENV_ROOT}/version)"; | ||
version_requested="$(cat "${version_file}")" \ | ||
|| log 'error' "Failed to open ${version_file}"; | ||
|
||
# Absolute fallback | ||
if [ -z "${version_requested}" ]; then | ||
log 'debug' 'Version file had no content. Falling back to "latest"'; | ||
version_requested='latest'; | ||
fi; | ||
|
||
else | ||
log 'debug' "Version File is the default \${TFENV_ROOT}/version (${TFENV_ROOT}/version) but it doesn't exist"; | ||
log 'info' 'No version requested on the command line or in the version file search path. Installing "latest"'; | ||
version_requested='latest'; | ||
fi; | ||
else | ||
version_requested="${arg}"; | ||
fi; | ||
|
||
log 'debug' "Version Requested: ${version_requested}"; | ||
|
||
if [[ "${version_requested}" =~ ^min-required$ ]]; then | ||
log 'info' 'Detecting minimum required version...'; | ||
min_required="$(tfenv-min-required)" \ | ||
|| log 'error' 'tfenv-min-required failed'; | ||
|
||
log 'info' "Minimum required version detected: ${min_required}"; | ||
version_requested="${min_required}"; | ||
fi; | ||
|
||
if [[ "${version_requested}" =~ ^latest\:.*$ ]]; then | ||
version="${version_requested%%\:*}"; | ||
regex="${version_requested##*\:}"; | ||
log 'debug' "Version uses latest keyword with regex: ${regex}"; | ||
elif [[ "${version_requested}" =~ ^latest$ ]]; then | ||
version="${version_requested}"; | ||
regex="^[0-9]\+\.[0-9]\+\.[0-9]\+$"; | ||
log 'debug' "Version uses latest keyword alone. Forcing regex to match stable versions only: ${regex}"; | ||
else | ||
version="${version_requested}"; | ||
regex="^${version_requested}$"; | ||
log 'debug' "Version is explicit: ${version}. Regex enforces the version: ${regex}"; | ||
fi; | ||
|
||
echo "${version}:${regex}"; |
Oops, something went wrong.