Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add utility functions for processing Github releases #330

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions misc/install.func
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,52 @@ EOF
echo "bash -c \"\$(wget -qLO - https://github.com/community-scripts/ProxmoxVE/raw/main/ct/${app}.sh)\"" >/usr/bin/update
chmod +x /usr/bin/update
}

# This function downloads the latest release of a GitHub repository
download_latest_github_release() {
local user="$1"
local repo="$2"

local release_info=$(curl -s "https://api.github.com/repos/$user/$repo/releases/latest")

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
extract_github_tarball() {
local tarball="$1"
local target_root="$2"

local temp_dir=$(mktemp -d)
tar -xf "$tarball" -C "$temp_dir"

if [ $? -ne 0 ]; then
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"
}