Skip to content

Commit

Permalink
[Feature] Attempt to guess download URL (#4)
Browse files Browse the repository at this point in the history
Migrated code from AZMCode#7

Thanks @timdp!

Test Plan:
- [x] Uninstall jq, and then ran `asdf install jq 1.6`, validated that I saw a correct guess and download.
  • Loading branch information
lsanwick authored Jun 1, 2023
1 parent e3d033b commit e1e3c92
Showing 1 changed file with 48 additions and 7 deletions.
55 changes: 48 additions & 7 deletions bin/download
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ source "${plugin_dir}/../lib/utils.bash"

declare -r JQ_REPO="https://github.com/jqlang/jq.git"
declare -r RELEASES_URL="https://api.github.com/repos/jqlang/jq/releases"
declare -r DOWNLOAD_BASE_URL="https://github.com/jqlang/jq/releases/download"


error_exit() {
Expand All @@ -27,6 +28,7 @@ get_platform() {
fi
return
}

get_arch(){
declare arch="$(uname -m)"
if [ "$arch" == 'x86_64' ]; then
Expand Down Expand Up @@ -67,6 +69,7 @@ get_assets_url() {

error_exit "Given version '$install_version' did not match any releases. Try list-all to see available options"
}

find_all_asset_names() {
declare install_version="$1"

Expand All @@ -83,6 +86,7 @@ find_all_asset_names() {
declare -a output=($(echo "$assets_json" | sed -n -E 's/[[:blank:]]*"browser_download_url":[[:blank:]]{0,2}"([^"]{8,})"/\1/p'))
echo "${output[@]}"
}

filter_assets() {
declare -a inArr=($@)

Expand All @@ -107,6 +111,7 @@ filter_assets() {
done
echo "${filteredArr[@]}"
}

find_file_url() {
declare -r install_version="$@"

Expand All @@ -123,19 +128,56 @@ find_file_url() {

echo "${usableAssets[0]}"
}

guess_download_url() {
declare -r install_version="$@"

declare -r arch="$(get_arch)"
declare -r platform="$(get_platform)"

local guessed_file=''
if [ $platform = 'osx' ]; then
guessed_file="jq-osx-amd64"
elif [ $platform = 'linux' && -n "$arch" ]; then
guessed_file="jq-linux$arch"
fi
local guess_successful=0
}

download() {
declare -r download_type="$1"
declare -r download_version="$2"
declare -r download_path="$3"
declare -r bin_path="${download_path}/bin/jq"

if [ "$download_type" == "version" ]; then
declare -r download_url="$(find_file_url "$download_version")"
printf "Downloading jq $download_version ($download_url)...\n"
if [ -z "$download_url" ]; then
error_exit "Malformed URL"
mkdir "${download_path}/bin"

declare -r platform=$(get_platform)
declare -r arch=$(get_arch)
local guessed_file=''
if [ $platform = osx ]; then
guessed_file="jq-osx-amd64"
elif [ $platform = linux && -n "$arch" ]; then
guessed_file="jq-linux$arch"
fi
local guess_successful=0
if [ -n "$guessed_file" ]; then
declare -r guessed_url="${DOWNLOAD_BASE_URL}/jq-${download_version}/${guessed_file}"
if curl_wrapper -fsS -L -o "$bin_path" "$guessed_url" 2>/dev/null; then
printf "Accurately guessed jq $download_version download URL: $guessed_url\n"
guess_successful=1
fi
fi

if [ $guess_successful = 0 ]; then
declare -r download_url=$(find_file_url "$download_version")
printf "Downloading jq $download_version ($download_url)...\n"
if [ -z "$download_url" ]; then
error_exit "Malformed URL"
fi
curl_wrapper -fsS -L -o "$bin_path" "$download_url"
fi
mkdir "$download_path/bin"
curl_wrapper -fsS -L -o "${download_path}/bin/jq" "$download_url"
else
rm -rf "$download_path"
git init "$download_path"
Expand All @@ -148,6 +190,5 @@ download() {
printf "jq $download_version installed!\n"
}


download "$ASDF_INSTALL_TYPE" "$ASDF_INSTALL_VERSION" "$ASDF_DOWNLOAD_PATH"
#download ref 2e01ff1fb69609540b2bdc4e62a60499f2b2fb8e ~/Desktop/jqsource

0 comments on commit e1e3c92

Please sign in to comment.