From b71a32439f23fa7745330e48c3495c4c8a23ddf3 Mon Sep 17 00:00:00 2001 From: "matthieu.cuny" Date: Tue, 16 Jul 2019 01:05:43 +0200 Subject: [PATCH] update script from tfenv code --- CHANGELOG.md | 4 ++++ bin/terragrunt | 4 ++-- bin/tgenv | 30 +++++++++++++++--------------- libexec/helpers | 26 ++++++++++++++++++++++++++ libexec/tgenv---version | 3 ++- libexec/tgenv-exec | 5 ++--- libexec/tgenv-install | 38 +++++++++++++++++--------------------- libexec/tgenv-list | 20 +++++++++++++------- libexec/tgenv-list-remote | 9 +++------ libexec/tgenv-uninstall | 19 +++++-------------- libexec/tgenv-use | 24 +++++++++++------------- libexec/tgenv-version-file | 2 +- libexec/tgenv-version-name | 8 ++------ list_all_versions_offline | 1 + 14 files changed, 104 insertions(+), 89 deletions(-) create mode 100755 libexec/helpers diff --git a/CHANGELOG.md b/CHANGELOG.md index 43df5d0..513d973 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/bin/terragrunt b/bin/terragrunt index 262048b..b035c96 100755 --- a/bin/terragrunt +++ b/bin/terragrunt @@ -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 "${@}" diff --git a/bin/tgenv b/bin/tgenv index cc9b336..bc48be4 100755 --- a/bin/tgenv +++ b/bin/tgenv @@ -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 @@ -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 @@ -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 diff --git a/libexec/helpers b/libexec/helpers new file mode 100755 index 0000000..e663ca6 --- /dev/null +++ b/libexec/helpers @@ -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} "$@" +} diff --git a/libexec/tgenv---version b/libexec/tgenv---version index 9e9b350..c8568ed 100755 --- a/libexec/tgenv---version +++ b/libexec/tgenv---version @@ -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}" diff --git a/libexec/tgenv-exec b/libexec/tgenv-exec index f46e7ac..866ccc6 100755 --- a/libexec/tgenv-exec +++ b/libexec/tgenv-exec @@ -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}" "${@}" \ No newline at end of file diff --git a/libexec/tgenv-install b/libexec/tgenv-install index 0a678e2..43e1cb9 100755 --- a/libexec/tgenv-install +++ b/libexec/tgenv-install @@ -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 []" @@ -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}" @@ -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" @@ -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}" diff --git a/libexec/tgenv-list b/libexec/tgenv-list index 7fae2b1..d03d155 100755 --- a/libexec/tgenv-list +++ b/libexec/tgenv-list @@ -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" ] \ @@ -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 diff --git a/libexec/tgenv-list-remote b/libexec/tgenv-list-remote index fef8e3c..741d5c8 100755 --- a/libexec/tgenv-list-remote +++ b/libexec/tgenv-list-remote @@ -1,11 +1,8 @@ #!/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 @@ -13,7 +10,7 @@ if [ ${#} -ne 0 ];then 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 diff --git a/libexec/tgenv-uninstall b/libexec/tgenv-uninstall index 5ed9075..7f0e895 100755 --- a/libexec/tgenv-uninstall +++ b/libexec/tgenv-uninstall @@ -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 []" +[ "${#}" -gt 1 ] && error_and_die "usage: tgenv uninstall []" 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}" @@ -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}" @@ -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 - diff --git a/libexec/tgenv-use b/libexec/tgenv-use index 68cd0c7..c4ef108 100755 --- a/libexec/tgenv-use +++ b/libexec/tgenv-use @@ -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 " +[ "${#}" -ne 1 ] && error_and_die "usage: tgenv use " declare version_requested version regex @@ -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" ] \ @@ -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" diff --git a/libexec/tgenv-version-file b/libexec/tgenv-version-file index e0063a6..2f178d1 100755 --- a/libexec/tgenv-version-file +++ b/libexec/tgenv-version-file @@ -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" diff --git a/libexec/tgenv-version-name b/libexec/tgenv-version-name index 9baade3..62c65a5 100755 --- a/libexec/tgenv-version-name +++ b/libexec/tgenv-version-name @@ -2,12 +2,8 @@ # Summary: Show the current Terragrunt version set -e -function error_and_die() { - echo -e "tgenv: ${0}: ${1}" >&2 - exit 1 -} - [ -n "${TGENV_DEBUG}" ] && set -x +source "${TGENV_ROOT}/libexec/helpers" [ -d "${TGENV_ROOT}/versions" ] \ || error_and_die "No versions of terragrunt installed. Please install one with: tgenv install" @@ -37,5 +33,5 @@ version_exists() { if version_exists "${TGENV_VERSION}"; then echo "${TGENV_VERSION}" else - error_and_die "version \`${TGENV_VERSION}' is not installed (set by ${TGENV_VERSION_FILE})" + error_and_die "version '${TGENV_VERSION}' is not installed (set by ${TGENV_VERSION_FILE})" fi diff --git a/list_all_versions_offline b/list_all_versions_offline index fd6d502..9a624b8 100644 --- a/list_all_versions_offline +++ b/list_all_versions_offline @@ -1,3 +1,4 @@ +0.18.2 0.13.19 0.13.18 0.13.17