Skip to content

Commit

Permalink
Merge pull request #5 from cunymatthieu/feature/update-script-from-tfenv
Browse files Browse the repository at this point in the history
update script from tfenv code
  • Loading branch information
cunymatthieu authored Sep 17, 2019
2 parents d4906f2 + b71a324 commit c853eb9
Show file tree
Hide file tree
Showing 14 changed files with 104 additions and 89 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.0 (Jully 15, 2019)

* update script

## 0.0.2 (May 3, 2017)

* Add last version on terragrunt 0.12.16
Expand Down
4 changes: 2 additions & 2 deletions bin/terragrunt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash
set -e
[ -n "$TGENV_DEBUG" ] && set -x
[ -n "${TGENV_DEBUG}" ] && set -x

program="${0##*/}"
exec tgenv exec "$@"
exec "$(dirname "$(command -v "${0}")")/tgenv" exec "${@}"
30 changes: 15 additions & 15 deletions bin/tgenv
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
#!/usr/bin/env bash
set -e
if [ -n "$TGENV_DEBUG" ]; then
if [ -n "${TGENV_DEBUG}" ]; then
export PS4='+ [${BASH_SOURCE##*/}:${LINENO}] '
set -x
fi

# 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 target_file="${1}"
local file_name

while [ "$target_file" != "" ]; do
cd $(dirname ${target_file})
file_name=$(basename ${target_file})
target_file=$(readlink ${file_name})
while [ "${target_file}" != "" ]; do
cd "$(dirname ${target_file})"
file_name="$(basename "${target_file}")"
target_file="$(readlink "${file_name}")"
done

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

if [ -z "${TGENV_ROOT}" ]; then
TGENV_ROOT=$(cd $(dirname $(readlink_f $0))/.. && pwd)
TGENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"
else
TGENV_ROOT="${TGENV_ROOT%/}"
fi
Expand All @@ -30,15 +30,15 @@ export PATH
export TGENV_DIR="${PWD}"

abort() {
{ if [ "$#" -eq 0 ]; then cat -
else echo "tgenv: $*"
{ if [ "${#}" -eq 0 ]; then cat -
else echo "tgenv: ${*}"
fi
} >&2
exit 1
}

command="$1"
case "$command" in
command="${1}"
case "${command}" in
"" )
{ tgenv---version
tgenv-help
Expand All @@ -52,12 +52,12 @@ case "$command" in
;;
* )
command_path="$(command -v "tgenv-${command}" || true)"
if [ -z "$command_path" ];then
{ echo "no such command '$command'"
if [ -z "${command_path}" ];then
{ echo "no such command '${command}'"
tgenv-help
} | abort
fi
shift 1
exec "$command_path" "$@"
exec "${command_path}" "${@}"
;;
esac
26 changes: 26 additions & 0 deletions libexec/helpers
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash

function error_and_die() {
echo -e "tgenv: $(basename ${0}): \033[0;31m[ERROR] ${1}\033[0;39m" >&2
exit 1
}

function warn_and_continue() {
echo -e "tgenv: $(basename ${0}): \033[0;33m[WARN] ${1}\033[0;39m" >&2
}

function info() {
echo -e "\033[0;32m[INFO] ${1}\033[0;39m"
}

# Curl wrapper to switch TLS option for each OS
function curlw () {
local TLS_OPT="--tlsv1.2"

# Check if curl is 10.12.6 or above
if [[ -n "$(command -v sw_vers 2>/dev/null)" && ("$(sw_vers)" =~ 10\.12\.([6-9]|[0-9]{2}) || "$(sw_vers)" =~ 10\.1[3-9]) ]]; then
TLS_OPT=""
fi

curl ${TLS_OPT} "$@"
}
3 changes: 2 additions & 1 deletion libexec/tgenv---version
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
set -e
[ -n "${TGENV_DEBUG}" ] && set -x

version="0.0.2"
version=$(awk '/^##/{ print $2; exit}' "${TGENV_ROOT}"/CHANGELOG.md)
git_revision=""

if cd "${BASH_SOURCE%/*}" 2>/dev/null && git remote -v 2>/dev/null | grep -q tgenv; then
git_revision="$(git describe --tags HEAD 2>/dev/null || true)"
git_revision="${git_revision#v}"
fi

echo "tgenv ${git_revision:-$version}"
5 changes: 2 additions & 3 deletions libexec/tgenv-exec
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
set -e
[ -n "${TGENV_DEBUG}" ] && set -x

tg_v_n=`tgenv-version-name`
export TGENV_VERSION="$tg_v_n"
export TGENV_VERSION="$(tgenv-version-name)"
TG_BIN_PATH="${TGENV_ROOT}/versions/${TGENV_VERSION}/terragrunt"
export PATH="${TG_BIN_PATH}:${PATH}"
"${TG_BIN_PATH}" "${@}"
"${TG_BIN_PATH}" "${@}"
38 changes: 17 additions & 21 deletions libexec/tgenv-install
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
#!/usr/bin/env bash

function error_and_die() {
echo -e "tgenv: $(basename ${0}): \033[0;31m[ERROR] ${1}\033[0;39m" >&2
exit 1
}

function warn_and_continue() {
echo -e "tgenv: $(basename ${0}): \033[0;33m[WARN] ${1}\033[0;39m" >&2
}

function info() {
echo -e "\033[0;32m[INFO] ${1}\033[0;39m"
}

[ -n "${TGENV_DEBUG}" ] && set -x
source "${TGENV_ROOT}/libexec/helpers"

[ "${#}" -gt 1 ] && error_and_die "usage: tgenv install [<version>]"

Expand All @@ -22,7 +10,7 @@ declare version_requested version regex
if [ -z "${1}" ]; then
version_file="$(tgenv-version-file)"
if [ "${version_file}" != "${TGENV_ROOT}/version" ]; then
version_requested="$(cat ${version_file} || true)"
version_requested="$(cat "${version_file}" || true)"
fi
else
version_requested="${1}"
Expand All @@ -33,10 +21,10 @@ if [[ "${version_requested}" =~ ^latest\:.*$ ]]; then
regex="${version_requested##*\:}"
elif [[ "${version_requested}" =~ ^latest$ ]]; then
version="${version_requested}"
regex=""
regex="^[0-9]\+\.[0-9]\+\.[0-9]\+$"
else
version="${version_requested}"
regex="${version_requested}"
regex="^${version_requested}$"
fi

[ -n "${version}" ] || error_and_die "Version is not specified"
Expand All @@ -49,29 +37,37 @@ if [ -f "${dst_path}/terragrunt" ]; then
exit 0
fi

TGENV_ARCH="${TGENV_ARCH:-amd64}"
case "$(uname -s)" in
Darwin*)
os="darwin_amd64"
os="darwin_${TGENV_ARCH}"
;;
MINGW64*)
os="windows_amd64"
os="windows_${TGENV_ARCH}"
;;
MSYS_NT*)
os="windows_${TGENV_ARCH}"
;;
CYGWIN_NT*)
os="windows_${TGENV_ARCH}"
;;
*)
os="linux_amd64"
os="linux_${TGENV_ARCH}"
;;
esac

version_url="https://github.com/gruntwork-io/terragrunt/releases/download/v${version}"

tarball_name="terragrunt_${os}"
tarball_name="terragrunt_${os}";

info "Installing Terragrunt v${version}"

mkdir -p "${dst_path}" || error_and_die "Failed to make directory ${dst_path}"

info "Downloading release tarball from ${version_url}/${tarball_name}"
curl -# --tlsv1.2 --location-trusted -f -o "${dst_path}/terragrunt" "${version_url}/${tarball_name}" || error_and_die "Tarball download failed"
curlw -# --location-trusted -f -o "${dst_path}/terragrunt" "${version_url}/${tarball_name}" || error_and_die "Tarball download failed"

chmod +x "${dst_path}/terragrunt" || error_and_die "Change rights failed"

info "Installation of terragrunt v${version} successful"
tgenv-use "${version}"
20 changes: 13 additions & 7 deletions libexec/tgenv-list
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
#!/usr/bin/env bash

function error_and_die() {
echo -e "tgenv: ${0}: ${1}" >&2
exit 1
}

[ -n "${TGENV_DEBUG}" ] && set -x
source "${TGENV_ROOT}/libexec/helpers"

[ ${#} -ne 0 ] \
[ "${#}" -ne 0 ] \
&& error_and_die "usage: tgenv list"

[ -d "${TGENV_ROOT}/versions" ] \
Expand All @@ -16,4 +12,14 @@ function error_and_die() {
[[ -x "${TGENV_ROOT}/versions" && -r "${TGENV_ROOT}/versions" ]] \
|| error_and_die "tgenv versions directory is inaccessible!"

ls -1 "${TGENV_ROOT}/versions" | sort -t'.' -k 1nr,1 -k 2nr,2 -k 3nr,3
print_version () {
if [ "${1}" == "$(tgenv-version-name)" ]; then
echo "* ${1} (set by $(tgenv-version-file))"
else
echo " ${1}"
fi
}

for local_version in $(ls -1 "${TGENV_ROOT}/versions" | sort -t'.' -k 1nr,1 -k 2nr,2 -k 3nr,3); do
print_version "${local_version}"
done
9 changes: 3 additions & 6 deletions libexec/tgenv-list-remote
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
#!/usr/bin/env bash
#set -e

function warn_and_continue() {
echo -e "tgenv: $(basename ${0}): \033[0;33m[WARN] ${1}\033[0;39m" >&2
}
set -e

[ -n "${TGENV_DEBUG}" ] && set -x
source "${TGENV_ROOT}/libexec/helpers"

if [ ${#} -ne 0 ];then
echo "usage: tgenv list-remote" 1>&2
exit 1
fi

link_release="https://api.github.com/repos/gruntwork-io/terragrunt/tags?per_page=1000"
print=`curl --tlsv1.2 -sf $link_release`
print=$(curl --tlsv1.2 -sf $link_release)

return_code=$?
if [ $return_code -eq 22 ];then
Expand Down
19 changes: 5 additions & 14 deletions libexec/tgenv-uninstall
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
#!/usr/bin/env bash

function error_and_die() {
echo -e "tgenv: $(basename ${0}): \033[0;31m[ERROR] ${1}\033[0;39m" >&2
exit 1
}

function info() {
echo -e "\033[0;32m[INFO] ${1}\033[0;39m"
}

[ -n "${TGENV_DEBUG}" ] && set -x
source "${TGENV_ROOT}/libexec/helpers"

[ ${#} -gt 1 ] && error_and_die "usage: tgenv uninstall [<version>]"
[ "${#}" -gt 1 ] && error_and_die "usage: tgenv uninstall [<version>]"

declare version_requested version regex

if [ -z "${1}" ]; then
version_file="$(tgenv-version-file)"
if [ "${version_file}" != "${TGENV_ROOT}/version" ];then
version_requested="$(cat ${version_file} || true)"
version_requested="$(cat "${version_file}" || true)"
fi
else
version_requested="${1}"
Expand All @@ -32,11 +24,11 @@ elif [[ "${version_requested}" =~ ^latest$ ]]; then
regex=""
else
version="${version_requested}"
regex="${version_requested}"
regex="^${version_requested}$"
fi

[ -n "${version}" ] || error_and_die "Version is not specified"
version="$(tgenv-list | grep -e "${regex}" | head -n 1)"
version="$(tgenv-list | sed -E 's/^(\*| )? //g; s/ \(set by .+\)$//' | grep -e "${regex}" | head -n 1)"
[ -n "${version}" ] || error_and_die "No versions matching '${1}' found in local"

dst_path="${TGENV_ROOT}/versions/${version}"
Expand All @@ -45,4 +37,3 @@ if [ -f "${dst_path}/terragrunt" ]; then
rm -r "${dst_path}"
info "\033[0;32mTerragrunt v${version} is successfully uninstalled\033[0;39m"
fi

24 changes: 11 additions & 13 deletions libexec/tgenv-use
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
#!/usr/bin/env bash

function error_and_die() {
echo -e "tgenv: ${0}: ${1}" >&2
exit 1
}

[ -n "${TGENV_DEBUG}" ] && set -x
source "${TGENV_ROOT}/libexec/helpers"

[ ${#} -ne 1 ] && error_and_die "usage: tgenv use <version>"
[ "${#}" -ne 1 ] && error_and_die "usage: tgenv use <version>"

declare version_requested version regex

Expand All @@ -18,10 +14,10 @@ if [[ "${version_requested}" =~ ^latest\:.*$ ]]; then
regex="${version_requested##*\:}"
elif [[ "${version_requested}" =~ ^latest$ ]]; then
version="${version_requested}"
regex=""
regex="^[0-9]\+\.[0-9]\+\.[0-9]\+$"
else
version="${version_requested}"
regex="${version_requested}"
regex="^${version_requested}$"
fi

[ -d "${TGENV_ROOT}/versions" ] \
Expand All @@ -35,11 +31,13 @@ version="$(\ls "${TGENV_ROOT}/versions" \

[ -n "${version}" ] || error_and_die "No installed versions of terragrunt matched '${1}'"

target_path=${TGENV_ROOT}/versions/${version}
[ -f ${target_path}/terragrunt ] \
target_path="${TGENV_ROOT}/versions/${version}"
[ -f "${target_path}/terragrunt" ] \
|| error_and_die "Version directory for ${version} is present, but the terragrunt binary is not! Manual intervention required."
[ -x ${target_path}/terragrunt ] \
[ -x "${target_path}/terragrunt" ] \
|| error_and_die "Version directory for ${version} is present, but the terragrunt binary is not executable! Manual intervention required. "

echo "${version}" > "${TGENV_ROOT}/version"
terragrunt --version || error_and_die "'terragrunt --version' failed. Something is seriously wrong"
info "Switching to v${version}"
echo "${version}" > "$(tgenv-version-file)" || error_and_die "'switch to v${version} failed'"
terragrunt --version 1>/dev/null || error_and_die "'terragrunt --version' failed. Something is seriously wrong"
info "Switching completed"
2 changes: 1 addition & 1 deletion libexec/tgenv-version-file
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ find_local_version_file() {
return 1
}

find_local_version_file "${TGENV_DIR}" || echo "${TGENV_ROOT}/version"
find_local_version_file "${TGENV_DIR}" || find_local_version_file "${HOME}" || echo "${TGENV_ROOT}/version"
Loading

0 comments on commit c853eb9

Please sign in to comment.