From e1e3c929f7defb6495c201e9270d6f81fd32c141 Mon Sep 17 00:00:00 2001 From: Luke Sanwick Date: Wed, 31 May 2023 18:25:00 -0700 Subject: [PATCH] [Feature] Attempt to guess download URL (#4) Migrated code from https://github.com/AZMCode/asdf-jq/pull/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. --- bin/download | 55 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 7 deletions(-) diff --git a/bin/download b/bin/download index 7ba7323..2c385ba 100755 --- a/bin/download +++ b/bin/download @@ -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() { @@ -27,6 +28,7 @@ get_platform() { fi return } + get_arch(){ declare arch="$(uname -m)" if [ "$arch" == 'x86_64' ]; then @@ -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" @@ -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=($@) @@ -107,6 +111,7 @@ filter_assets() { done echo "${filteredArr[@]}" } + find_file_url() { declare -r install_version="$@" @@ -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" @@ -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