|
3 | 3 | set -e |
4 | 4 | set -o pipefail |
5 | 5 |
|
| 6 | +# https://github.com/terraform-docs/terraform-docs/releases/download/v0.6.0/terraform-docs-v0.6.0-linux-amd64 |
| 7 | +# https://github.com/terraform-docs/terraform-docs/releases/download/v0.6.0/terraform-docs-v0.6.0-darwin-amd64 |
| 8 | + |
| 9 | +readonly NO_BINARY_ALTNAME=@@UNDEFINED@@ |
| 10 | +readonly g_github_coordinates=terraform-docs/terraform-docs |
| 11 | +readonly g_binary_name=terraform-docs |
| 12 | +readonly g_binary_altname="$NO_BINARY_ALTNAME" |
| 13 | +# |
| 14 | +# __VERSION__/__VERSION_SHORT__/__BINARY_NAME__/__PLATFORM__ |
| 15 | +# |
| 16 | +readonly g_filename_template=__BINARY_NAME__-__VERSION__-__PLATFORM__ |
| 17 | +# |
| 18 | +# __GITHUB_COORDINATES__/__VERSION__/__VERSION_SHORT__/__FILENAME__ |
| 19 | +# |
| 20 | +readonly g_download_url_template=https://github.com/__GITHUB_COORDINATES__/releases/download/__VERSION__/__FILENAME__ |
| 21 | +# |
| 22 | +# __ARCHIVE_DIR__/__BINARY_NAME__/__VERSION__/__VERSION_SHORT__/__PLATFORM__ |
| 23 | +# |
| 24 | +readonly g_binary_path_in_archive_template=__ARCHIVE_DIR__/__BINARY_NAME__-__VERSION__-__PLATFORM__ |
| 25 | +readonly g_downloaded_file_is_not_an_archive=true |
| 26 | +readonly g_platform_pattern=uname_l_dash_amd64 # one of uname_c_x86_64|uname_l_dash_amd64|uname_l_underscore_amd64|custom |
| 27 | + |
| 28 | +# Borrowed to someone, but I don't remember who it was, sorry :( |
| 29 | +# Print message $2 with log-level $1 to STDERR, colorized if terminal |
| 30 | +# log DEBUG "DOCKER_HOST ${DOCKER_HOST}" |
| 31 | +function log() { |
| 32 | + local level=${1?} |
| 33 | + shift |
| 34 | + local code |
| 35 | + local line |
| 36 | + code='' |
| 37 | + line="[$(date '+%F %T')] $level: $*" |
| 38 | + if [ -t 2 ]; then |
| 39 | + case "$level" in |
| 40 | + INFO) code=36 ;; |
| 41 | + DEBUG) code=35 ;; |
| 42 | + WARN) code=33 ;; |
| 43 | + ERROR) code=31 ;; |
| 44 | + *) code=37 ;; |
| 45 | + esac |
| 46 | + echo -e "\e[${code}m${line} \e[94m(${FUNCNAME[1]})\e[0m" |
| 47 | + else |
| 48 | + echo "$line" |
| 49 | + fi >&2 |
| 50 | +} |
| 51 | + |
6 | 52 | install_tool() { |
7 | | - #local install_type=$1 |
| 53 | + local install_type=$1 |
8 | 54 | local version=$2 |
9 | 55 | local install_path=$3 |
10 | 56 | local tmp_download_dir=$4 |
11 | 57 | local binary_name=$5 |
| 58 | + local binary_alt_name=$6 |
| 59 | + local downloaded_file_is_not_an_archive=$7 |
12 | 60 |
|
13 | 61 | local platform |
14 | 62 | local bin_install_path="$install_path/bin" |
15 | | - local binary_path="$bin_install_path/${binary_name}" |
| 63 | + local full_path_to_binary="$bin_install_path/${binary_name}" |
| 64 | + local full_path_to_alt_binary |
| 65 | + if [[ -n "$binary_alt_name" || "$binary_alt_name" == "$NO_BINARY_ALTNAME" ]]; then |
| 66 | + full_path_to_alt_binary="$bin_install_path/${binary_alt_name}" |
| 67 | + fi |
16 | 68 | local download_url |
17 | | - local download_path |
| 69 | + local download_target_file |
| 70 | + local download_sub_path_dir |
| 71 | + |
| 72 | + if [[ "$install_type" != "version" ]]; then |
| 73 | + log ERROR "Install of type [$install_type] not supported" |
| 74 | + fi |
18 | 75 |
|
19 | 76 | platform=$(get_platform) |
20 | 77 | download_url=$(get_download_url "$version" "$platform" "$binary_name") |
21 | | - download_path="$tmp_download_dir/"$(get_filename "$version" "$platform" "$binary_name") |
| 78 | + download_sub_path_dir=$tmp_download_dir/sub |
| 79 | + mkdir -p "$download_sub_path_dir" |
| 80 | + download_target_file="$download_sub_path_dir/"$(get_filename "$version" "$platform" "$binary_name") |
22 | 81 |
|
23 | | - echo "Downloading [${binary_name}] from ${download_url} to ${download_path}" |
24 | | - curl -Lo "$download_path" "$download_url" |
| 82 | + log INFO "Downloading [${binary_name}] from ${download_url} to ${download_target_file}" |
| 83 | + curl --location --output "$download_target_file" "$download_url" |
25 | 84 |
|
26 | | - echo "Creating bin directory" |
| 85 | + log INFO "Creating bin directory [${bin_install_path}]" |
27 | 86 | mkdir -p "${bin_install_path}" |
28 | 87 |
|
29 | | - echo "Cleaning previous binaries" |
30 | | - rm -f "$binary_path" 2>/dev/null || true |
| 88 | + log INFO "Cleaning previous binaries if any" |
| 89 | + rm -f "$full_path_to_binary" 2>/dev/null || true |
| 90 | + if [[ -n "$binary_alt_name" ]]; then |
| 91 | + rm -f "$full_path_to_alt_binary" 2>/dev/null || true |
| 92 | + fi |
| 93 | + if [[ "$downloaded_file_is_not_an_archive" != "true" ]]; then |
| 94 | + log INFO "Extracting archive" |
| 95 | + tar xpf "$download_target_file" -C "$tmp_download_dir" |
| 96 | + else |
| 97 | + log INFO "Preparing downloaded file" |
| 98 | + cp "$download_target_file" "$tmp_download_dir" |
| 99 | + fi |
| 100 | + log INFO "Copying binaries" |
| 101 | + cp "$(get_binary_path_in_archive "${tmp_download_dir}" "${binary_name}" "${version}" "${platform}")" "${full_path_to_binary}" |
| 102 | + chmod +x "${full_path_to_binary}" |
| 103 | + if [[ -n "$binary_alt_name" ]]; then |
| 104 | + cp "${full_path_to_binary}" "${full_path_to_alt_binary}" |
| 105 | + chmod +x "${full_path_to_alt_binary}" |
| 106 | + fi |
| 107 | +} |
31 | 108 |
|
32 | | - echo "Copying binary" |
33 | | - cp "$(get_binary_path_in_archive "${tmp_download_dir}" "${binary_name}" "${version}" "${platform}")" "${binary_path}" |
34 | | - chmod +x "${binary_path}" |
| 109 | +get_version_short() { |
| 110 | + local version=$1 |
| 111 | + echo "$version" | tr -d "v" |
35 | 112 | } |
36 | 113 |
|
37 | 114 | get_binary_path_in_archive() { |
38 | 115 | local archive_dir=$1 |
39 | 116 | local binary_name=$2 |
40 | 117 | local version=$3 |
41 | 118 | local platform=$4 |
| 119 | + local version_short |
| 120 | + version_short=$(get_version_short "$version") |
42 | 121 |
|
43 | | - echo "${archive_dir}/${binary_name}-${version}-${platform}" |
| 122 | + echo $g_binary_path_in_archive_template | |
| 123 | + sed "s|__ARCHIVE_DIR__|${archive_dir}|g;s|__BINARY_NAME__|${binary_name}|g;s|__VERSION__|${version}|g;s|__VERSION_SHORT__|${version_short}|g;s|__PLATFORM__|${platform}|g;" |
44 | 124 | } |
45 | 125 |
|
46 | 126 | get_platform() { |
| 127 | + get_platform_${g_platform_pattern} |
| 128 | +} |
| 129 | + |
| 130 | +get_platform_custom() { |
| 131 | + echo "NOT USED" |
| 132 | +} |
| 133 | + |
| 134 | +get_platform_uname_c_x86_64() { |
| 135 | + echo "$(uname)_x86_64" |
| 136 | +} |
| 137 | + |
| 138 | +get_platform_uname_l_dash_amd64() { |
47 | 139 | echo "$(uname | tr '[:upper:]' '[:lower:]')-amd64" |
48 | 140 | } |
49 | 141 |
|
| 142 | +get_platform_uname_l_underscore_amd64() { |
| 143 | + echo "$(uname | tr '[:upper:]' '[:lower:]')_amd64" |
| 144 | +} |
| 145 | + |
50 | 146 | get_filename() { |
51 | | - #local version="$1" |
| 147 | + local version="$1" |
52 | 148 | local platform="$2" |
53 | 149 | local binary_name="$3" |
| 150 | + local version_short |
| 151 | + version_short=$(get_version_short "$version") |
| 152 | + # log DEBUG "version=${version}, version_short=${version_short}, binary_name=${binary_name}, platform=${platform}" |
54 | 153 |
|
55 | | - echo "${binary_name}-${version}-${platform}" |
| 154 | + echo "$g_filename_template" | |
| 155 | + sed "s/__VERSION__/${version}/g;s/__VERSION_SHORT__/${version_short}/g;s/__BINARY_NAME__/${binary_name}/g;s/__PLATFORM__/${platform}/g" |
56 | 156 | } |
57 | 157 |
|
58 | | -# https://github.com/terraform-docs/terraform-docs/releases/download/v0.6.0/terraform-docs-v0.6.0-linux-amd64 |
59 | | - |
60 | 158 | get_download_url() { |
61 | 159 | local version="$1" |
62 | 160 | local platform="$2" |
63 | 161 | local binary_name="$3" |
64 | 162 | local filename |
65 | 163 | filename="$(get_filename "$version" "$platform" "$binary_name")" |
| 164 | + local version_short |
| 165 | + version_short=$(get_version_short "$version") |
| 166 | + # log DEBUG "version=${version}, binary_name=${binary_name}, platform=${platform}, filename=${filename}" |
| 167 | + |
| 168 | + echo "$g_download_url_template" | |
| 169 | + sed "s|__GITHUB_COORDINATES__|${g_github_coordinates}|g;s|__VERSION__|${version}|g;s|__VERSION_SHORT__|${version_short}|g;s|__FILENAME__|${filename}|g;" |
66 | 170 |
|
67 | | - echo "https://github.com/terraform-docs/terraform-docs/releases/download/${version}/${filename}" |
68 | 171 | } |
69 | 172 |
|
70 | | -tmp_download_dir="$(mktemp -d -t 'asdf_XXXXXXXX')" |
71 | | -trap 'rm -rf "${tmp_download_dir}"' EXIT |
72 | | -install_tool "$ASDF_INSTALL_TYPE" "$ASDF_INSTALL_VERSION" "$ASDF_INSTALL_PATH" "$tmp_download_dir" terraform-docs |
| 173 | +readonly _tmp_download_dir="$(mktemp -d -t 'asdf_XXXXXXXX')" |
| 174 | +trap 'rm -rf "${_tmp_download_dir}"' EXIT |
| 175 | +install_tool "$ASDF_INSTALL_TYPE" "$ASDF_INSTALL_VERSION" "$ASDF_INSTALL_PATH" "$_tmp_download_dir" "$g_binary_name" "$g_binary_altname" "$g_downloaded_file_is_not_an_archive" |
0 commit comments