-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(code)!: change code to get closer to the template (#37)
* refactor: rework list-all * refactor: provide latest-stable * refactor: move some funcs to utils.bash * refactor: split code * fix: remove call to get_ext * chore: remove echo * fixup * refactor: remove all echo calls * chore: update bats tests * fix: remove remaining call to log * fix: remove remaining call to log * refactor: rework vercomp * refactor: add tests * refactor: add more tests
- Loading branch information
Showing
12 changed files
with
436 additions
and
345 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
external-sources=true |
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,23 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -euo pipefail | ||
|
||
current_script_path=${BASH_SOURCE[0]} | ||
plugin_dir=$(dirname "$(dirname "$current_script_path")") | ||
|
||
# shellcheck source=SCRIPTDIR/../lib/utils.bash | ||
source "${plugin_dir}/lib/utils.bash" | ||
|
||
mkdir -p "$ASDF_DOWNLOAD_PATH" | ||
|
||
# TODO: Adapt this to proper extension and adapt extracting strategy. | ||
release_file="$ASDF_DOWNLOAD_PATH/$TOOL_NAME-$ASDF_INSTALL_VERSION$.tar.gz" | ||
|
||
# Download tar.gz file to the download directory | ||
download_release "$ASDF_INSTALL_VERSION" "$release_file" | ||
|
||
# Extract contents of tar.gz file into the download directory | ||
tar -xzf "$release_file" -C "$ASDF_DOWNLOAD_PATH" || fail "Could not extract $release_file" | ||
|
||
# Remove the tar.gz file since we don't need to keep it | ||
rm "$release_file" |
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 |
---|---|---|
@@ -1,324 +1,11 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -e | ||
set -o pipefail | ||
set -euo pipefail | ||
|
||
readonly TOOL_NAME="k9s" | ||
current_script_path=${BASH_SOURCE[0]} | ||
plugin_dir=$(dirname "$(dirname "$current_script_path")") | ||
|
||
# https://github.com/derailed/k9s/releases/download/v0.16.1/k9s_Linux_x86_64.tar.gz | ||
# https://github.com/derailed/k9s/releases/download/v0.16.1/k9s_Darwin_x86_64.tar.gz | ||
# https://github.com/derailed/k9s/releases/download/v0.16.1/k9s_Darwin_arm64.tar.gz | ||
# shellcheck source=SCRIPTDIR/../lib/utils.bash | ||
source "${plugin_dir}/lib/utils.bash" | ||
|
||
readonly NO_BINARY_ALTNAME=@@UNDEFINED@@ | ||
# shellcheck disable=SC2034 | ||
readonly g_github_coordinates=derailed/k9s | ||
readonly g_binary_name=k9s | ||
readonly g_binary_altname="${NO_BINARY_ALTNAME}" | ||
|
||
# | ||
# available : __VERSION__/__VERSION_SHORT__/__BINARY_NAME__/__PLATFORM__ | ||
# | ||
readonly g_filename_template=__BINARY_NAME_____PLATFORM__.tar.gz | ||
# | ||
# available : __GITHUB_COORDINATES__/__VERSION__/__VERSION_SHORT__/__FILENAME__ | ||
# | ||
# shellcheck disable=SC2034 | ||
readonly g_download_url_template=https://github.com/__GITHUB_COORDINATES__/releases/download/v__VERSION__/__FILENAME__ | ||
# | ||
# available : __ARCHIVE_DIR__/__BINARY_NAME__/__VERSION__/__VERSION_SHORT__/__PLATFORM__ | ||
# | ||
readonly g_binary_path_in_archive_template=__ARCHIVE_DIR__/__BINARY_NAME__ | ||
readonly g_downloaded_file_is_not_an_archive=false | ||
|
||
# one of uname_c_x86_64|uname_l_dash_amd64|uname_l_underscore_amd64|uname_l_underscore_amd64|custom | ||
readonly g_platform_pattern=custom | ||
|
||
fail() { | ||
log ERROR "asdf-$TOOL_NAME: $*" | ||
exit 1 | ||
} | ||
|
||
# Borrowed to someone, but I don't remember who it was, sorry :( | ||
# Print message $2 with log-level $1 to STDERR, colorized if terminal | ||
# log DEBUG "DOCKER_HOST ${DOCKER_HOST}" | ||
function log() { | ||
local level=${1?} | ||
shift | ||
local code | ||
local line | ||
code='' | ||
line="[$(date '+%F %T')] $level: $*" | ||
if [ -t 2 ]; then | ||
case "$level" in | ||
INFO) code=36 ;; | ||
DEBUG) code=35 ;; | ||
WARN) code=33 ;; | ||
ERROR) code=31 ;; | ||
*) code=37 ;; | ||
esac | ||
echo -e "\e[${code}m${line} \e[94m(${FUNCNAME[1]})\e[0m" | ||
else | ||
echo "$line" | ||
fi >&2 | ||
} | ||
|
||
# from https://stackoverflow.com/questions/4023830/how-to-compare-two-strings-in-dot-separated-version-format-in-bash | ||
function vercomp() { | ||
if [[ "$1" == "$2" ]]; then | ||
return 0 | ||
fi | ||
local IFS=. | ||
# shellcheck disable=SC2206 | ||
local i ver1=($1) ver2=($2) | ||
# fill empty fields in ver1 with zeros | ||
for ((i = ${#ver1[@]}; i < ${#ver2[@]}; i++)); do | ||
ver1[i]=0 | ||
done | ||
for ((i = 0; i < ${#ver1[@]}; i++)); do | ||
if [[ -z ${ver2[i]} ]]; then | ||
# fill empty fields in ver2 with zeros | ||
ver2[i]=0 | ||
fi | ||
if ((10#${ver1[i]} > 10#${ver2[i]})); then | ||
return 1 | ||
fi | ||
if ((10#${ver1[i]} < 10#${ver2[i]})); then | ||
return 2 | ||
fi | ||
done | ||
return 0 | ||
} | ||
|
||
install_tool() { | ||
local install_type=$1 | ||
local version=$2 | ||
local install_path=$3 | ||
local tmp_download_dir=$4 | ||
local binary_name=$5 | ||
local binary_alt_name=$6 | ||
local downloaded_file_is_not_an_archive=$7 | ||
|
||
local platform | ||
local bin_install_path="$install_path/bin" | ||
local full_path_to_binary="$bin_install_path/${binary_name}" | ||
local full_path_to_alt_binary | ||
if [[ -n "$binary_alt_name" || "$binary_alt_name" == "$NO_BINARY_ALTNAME" ]]; then | ||
full_path_to_alt_binary="$bin_install_path/${binary_alt_name}" | ||
fi | ||
local download_url | ||
local download_target_file | ||
local download_sub_path_dir | ||
|
||
if [[ "$install_type" != "version" ]]; then | ||
log ERROR "Install of type [$install_type] not supported" | ||
fi | ||
|
||
platform=$(get_platform) | ||
download_url=$(get_download_url "$version" "$platform" "$binary_name") | ||
download_sub_path_dir=$tmp_download_dir/sub | ||
mkdir -p "$download_sub_path_dir" | ||
download_target_file="$download_sub_path_dir/"$(get_filename "$version" "$platform" "$binary_name") | ||
|
||
log INFO "Downloading [${binary_name}] from ${download_url} to ${download_target_file}" | ||
curl --location --output "$download_target_file" "$download_url" | ||
|
||
log INFO "Creating bin directory [${bin_install_path}]" | ||
mkdir -p "${bin_install_path}" | ||
|
||
log INFO "Cleaning previous binaries if any" | ||
rm -f "$full_path_to_binary" 2>/dev/null || true | ||
if [[ -n "$binary_alt_name" ]]; then | ||
rm -f "$full_path_to_alt_binary" 2>/dev/null || true | ||
fi | ||
if [[ "$downloaded_file_is_not_an_archive" != "true" ]]; then | ||
log INFO "Extracting archive" | ||
tar xpf "$download_target_file" -C "$tmp_download_dir" | ||
else | ||
log INFO "Preparing downloaded file" | ||
cp "$download_target_file" "$tmp_download_dir" | ||
fi | ||
log INFO "Copying binaries" | ||
# editorconfig-checker-disable | ||
cp "$(get_binary_path_in_archive "${tmp_download_dir}" "${binary_name}" "${version}" "${platform}")" "${full_path_to_binary}" | ||
# editorconfig-checker-enable | ||
chmod +x "${full_path_to_binary}" | ||
if [[ -n "$binary_alt_name" ]]; then | ||
cp "${full_path_to_binary}" "${full_path_to_alt_binary}" | ||
chmod +x "${full_path_to_alt_binary}" | ||
fi | ||
} | ||
|
||
get_version_short() { | ||
local version=$1 | ||
echo "$version" | tr -d "v" | ||
} | ||
|
||
get_binary_path_in_archive() { | ||
local archive_dir=$1 | ||
local binary_name=$2 | ||
local version=$3 | ||
local platform=$4 | ||
local version_short | ||
version_short=$(get_version_short "$version") | ||
|
||
# editorconfig-checker-disable | ||
echo $g_binary_path_in_archive_template | | ||
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;" | ||
# editorconfig-checker-enable | ||
} | ||
|
||
get_platform() { | ||
get_platform_${g_platform_pattern} | ||
} | ||
|
||
get_cpu() { | ||
local machine_hardware_name | ||
machine_hardware_name="$(uname -m)" | ||
|
||
case "$machine_hardware_name" in | ||
'x86_64') | ||
vercomp "$version" "0.26.7" | ||
if [[ $? == 1 ]]; then local cpu_type="amd64"; else local cpu_type="x86_64"; fi | ||
;; | ||
'powerpc64le' | 'ppc64le') local cpu_type="ppc64le" ;; | ||
'aarch64') local cpu_type="arm64" ;; | ||
'armv5l' | 'armv6l' | 'armv7l') local cpu_type="arm" ;; | ||
*) local cpu_type="$machine_hardware_name" ;; | ||
esac | ||
|
||
echo "$cpu_type" | ||
} | ||
|
||
get_platform_custom() { | ||
echo "$(uname)_$(get_cpu)" | ||
} | ||
|
||
get_platform_uname_c_x86_64() { | ||
echo "$(uname)_x86_64" | ||
} | ||
|
||
get_platform_uname_l_dash_amd64() { | ||
echo "$(uname | tr '[:upper:]' '[:lower:]')-amd64" | ||
} | ||
|
||
get_platform_uname_l_underscore_amd64() { | ||
echo "$(uname | tr '[:upper:]' '[:lower:]')_amd64" | ||
} | ||
|
||
get_filename() { | ||
local version="$1" | ||
local platform="$2" | ||
local binary_name="$3" | ||
local version_short | ||
version_short=$(get_version_short "$version") | ||
# log DEBUG "version=${version}, version_short=${version_short}, binary_name=${binary_name}, platform=${platform}" | ||
|
||
# editorconfig-checker-disable | ||
echo "$g_filename_template" | | ||
sed "s/__VERSION__/${version}/g;s/__VERSION_SHORT__/${version_short}/g;s/__BINARY_NAME__/${binary_name}/g;s/__PLATFORM__/${platform}/g" | ||
# editorconfig-checker-enable | ||
|
||
} | ||
|
||
get_download_url() { | ||
local version="$1" | ||
local platform="$2" | ||
local binary_name="$3" | ||
|
||
k9s_get_download_url "$version" "$platform" "$binary_name" | ||
} | ||
|
||
get_filename_post_0_14_0() { | ||
local version="$1" | ||
local platform="$2" | ||
local binary_name="$3" | ||
|
||
echo "${binary_name}_${platform}.tar.gz" | ||
} | ||
|
||
get_filename_pre_0_14_0() { | ||
local version="$1" | ||
local platform="$2" | ||
local binary_name="$3" | ||
|
||
echo "${binary_name}_${version}_${platform}.tar.gz" | ||
} | ||
|
||
get_filename_0_24_10() { | ||
local version="$1" | ||
local platform="$2" | ||
local binary_name="$3" | ||
|
||
echo "${binary_name}_v${version}_${platform}.tar.gz" | ||
} | ||
|
||
k9s_get_download_url() { | ||
local version="$1" | ||
local platform="$2" | ||
local binary_name="$3" | ||
local filename | ||
local path_version | ||
|
||
# https://github.com/derailed/k9s/releases/download/v0.16.1/k9s_Linux_x86_64.tar.gz | ||
# https://github.com/derailed/k9s/releases/download/0.11.1/k9s_0.11.1_Linux_x86_64.tar.gz | ||
vercomp "$version" "0.14.0" | ||
case $? in | ||
0) op='=' ;; | ||
1) op='>' ;; | ||
2) op='<' ;; | ||
esac | ||
if [[ "$op" == '<' ]]; then | ||
filename="$(get_filename_pre_0_14_0 "$version" "$platform" "$binary_name")" | ||
else | ||
filename="$(get_filename_post_0_14_0 "$version" "$platform" "$binary_name")" | ||
fi | ||
|
||
vercomp "$version" "0.13.0" | ||
case $? in | ||
0) op='=' ;; | ||
1) op='>' ;; | ||
2) op='<' ;; | ||
esac | ||
if [[ "$op" == '<' ]]; then | ||
path_version="$version" | ||
else | ||
path_version="v$version" | ||
fi | ||
|
||
vercomp "$version" "0.24.10" | ||
case $? in | ||
0) op='=' ;; | ||
1) op='>' ;; | ||
2) op='<' ;; | ||
esac | ||
if [[ "$op" == '=' ]]; then | ||
filename="$(get_filename_0_24_10 "$version" "$platform" "$binary_name")" | ||
path_version="v$version" | ||
else | ||
: # do not alter behavior | ||
fi | ||
|
||
vercomp "$version" "0.25.0" | ||
case $? in | ||
0) op='=' ;; | ||
1) op='>' ;; | ||
2) op='<' ;; | ||
esac | ||
if [[ "$op" == '<' ]] && [[ "$platform" == "Darwin_arm64" ]]; then | ||
fail "Version $version is not supported on platform $platform" | ||
else | ||
: # do not alter behavior | ||
fi | ||
|
||
echo "https://github.com/derailed/k9s/releases/download/${path_version}/${filename}" | ||
} | ||
# | ||
_tmp_download_dir="$(mktemp -d -t 'asdf_XXXXXXXX')" | ||
trap 'rm -rf "${_tmp_download_dir}"' EXIT | ||
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" | ||
install_version "$ASDF_INSTALL_TYPE" "$ASDF_INSTALL_VERSION" "$ASDF_INSTALL_PATH" |
Oops, something went wrong.