Simplified functions, as per the feedback, also use only wget

This commit is contained in:
Paul William 2024-11-19 21:28:44 +13:00
parent 24a7149794
commit 84e5fafd0d

View File

@ -203,50 +203,21 @@ EOF
} }
# This function downloads the latest release of a GitHub repository # This function downloads the latest release of a GitHub repository
download_latest_github_release() { github_download_latest_release() {
local user="$1" local user="$1"
local repo="$2" local repo="$2"
local output_file="$3"
local release_info=$(curl -s "https://api.github.com/repos/$user/$repo/releases/latest") local tarball_url=$($STD wget -qLO - "https://api.github.com/repos/$user/$repo/releases/latest" | grep '"tarball_url":' | cut -d '"' -f 4)
$STD wget -qLO "$output_file" "$tarball_url"
local tarball_url=$(echo "$release_info" | grep '"tarball_url":' | cut -d '"' -f 4)
if [ -z "$tarball_url" ]; then
msg_error "Could not fetch the latest release tarball URL."
exit 1
fi
local output_file="${repo}-latest.tar.gz"
curl -s -L "$tarball_url" -o "$output_file"
if [ $? -ne 0 ]; then
msg_error "Failed to download the release tarball."
exit 1
fi
echo "$output_file"
} }
# This function extracts a GitHub release tarball into a target directory # This function extracts a GitHub release tarball into a target directory
extract_github_tarball() { github_extract_latest_release() {
local tarball="$1" local user="$1"
local target_root="$2" local repo="$2"
local output_directory="$3"
local temp_dir=$(mktemp -d) mkdir -p "$output_directory"
tar -xf "$tarball" -C "$temp_dir" github_download_latest_release "$user" "$repo" "/tmp/$repo.tar.gz"
tar -xzf "/tmp/$repo.tar.gz" -C "$output_directory" --strip-components 1
if [ $? -ne 0 ]; then rm "/tmp/$repo.tar.gz"
msg_error "Failed to extract the release tarball."
rm -rf "$temp_dir"
exit 1
fi
# Find the root directory inside the temp directory
local extracted_root=$(find "$temp_dir" -mindepth 1 -maxdepth 1 -type d)
# Create the target directory and move the files
mkdir -p "$target_root"
mv "$extracted_root"/* "$target_root"
rm -rf "$temp_dir"
} }