Created
July 31, 2024 05:59
-
-
Save ParagDoke/c609f42a003126318030cfc915747ec3 to your computer and use it in GitHub Desktop.
An attempt to download a binary from github and place it under ${HOME}/bin. Uses gojq.
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
function get_binary_from_latest_github_release() { | |
project=$1 | |
repo=$2 | |
extension=$3 | |
kernel_name=$(uname -s) | |
if [[ ${kernel_name} == Linux* ]]; then | |
os_kernel="linux" | |
fi | |
platform=$(uname -m) | |
if [[ ${platform} == x86_64 || ${platform} == amd64 ]]; then | |
os_platform="amd64" | |
fi | |
download_url=$(curl --retry 3 -L "https://api.github.com/repos/${project}/${repo}/releases/latest" | gojq -rec '.assets[] | .browser_download_url' | grep "${os_kernel}" | grep "${os_platform}" | egrep "${extension}\$" | grep -v grep) | |
pushd /tmp | |
downloaded_file=$(curl --retry 3 -L -O -w '%{filename_effective}' "${download_url}") | |
popd | |
if [[ ${downloaded_file} == *.tar.gz || ${downloaded_file} == *.tgz ]]; then | |
binary=${repo} | |
# some archives may have nested folders | |
# list archive contents, then hunt for the binary as last word | |
extract_from_archive=$(tar tzf /tmp/${downloaded_file} | egrep "/?${binary}\$") | |
# on this line, using tr delete everything that is complement of / | |
# in other words, delete all characters except / | |
# then get character count -> tells us how many components to strip | |
strip_components_count=$(echo "${extract_from_archive}" | tr -cd '/' | wc -c) | |
mkdir -p ${HOME}/bin | |
pushd ${HOME}/bin | |
tar xzf "/tmp/${downloaded_file}" --strip-components=${strip_components_count} "${extract_from_archive}" | |
popd | |
chmod u+x ${HOME}/bin/${binary} | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment