From f5a266ff70b88d1c86a0d3bfe8cc7e1769d76306 Mon Sep 17 00:00:00 2001 From: Aditya Gupta Date: Fri, 26 Jun 2026 19:57:47 +0530 Subject: [PATCH] feat: Implement the github release helper with `github.sh` --- bootstrap.sh | 4 ++- installers/install_lazygit.sh | 10 ++----- installers/install_yazi.sh | 10 +++---- lib/github.sh | 53 +++++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 14 deletions(-) create mode 100644 lib/github.sh diff --git a/bootstrap.sh b/bootstrap.sh index d0bbd4a..0b5c89b 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -41,7 +41,7 @@ else BOOTSTRAP_SOURCE_DIR="$BOOTSTRAP_TMP_DIR" _BASE_URL="https://git.adityagupta.dev/sortedcord/bootstrap/raw/branch/master" - _LIBS=("lib/common.sh" "lib/rollback.sh" "lib/platform.sh" "lib/shell_config.sh" "lib/plugins.sh" "lib/registry_helpers.sh") + _LIBS=("lib/common.sh" "lib/rollback.sh" "lib/platform.sh" "lib/shell_config.sh" "lib/plugins.sh" "lib/registry_helpers.sh" "lib/github.sh") _curl_args=() for _lib in "${_LIBS[@]}"; do @@ -57,6 +57,7 @@ if [ -f "$BOOTSTRAP_SOURCE_DIR/lib/common.sh" ]; then . "$BOOTSTRAP_SOURCE_DIR/lib/platform.sh" . "$BOOTSTRAP_SOURCE_DIR/lib/shell_config.sh" . "$BOOTSTRAP_SOURCE_DIR/lib/registry_helpers.sh" + . "$BOOTSTRAP_SOURCE_DIR/lib/github.sh" init_rollback_system else echo "Error: Failed to locate or download bootstrap libraries." >&2 @@ -84,6 +85,7 @@ install_bootstrap() { "lib/platform.sh" "lib/shell_config.sh" "lib/registry_helpers.sh" + "lib/github.sh" "lib/plugins.sh" "commands/help.sh" "commands/con.sh" diff --git a/installers/install_lazygit.sh b/installers/install_lazygit.sh index 4adbb88..0963c15 100755 --- a/installers/install_lazygit.sh +++ b/installers/install_lazygit.sh @@ -30,11 +30,7 @@ install_lazygit() { fi local latest_tag="" - if has_command curl; then - latest_tag=$(curl -sL https://api.github.com/repos/jesseduffield/lazygit/releases/latest \ - | grep '"tag_name":' | head -n1 \ - | sed -E 's/.*"tag_name": "([^"]+)".*/\1/' || true) - fi + latest_tag=$(github_get_latest_release "jesseduffield/lazygit") if [ -z "$latest_tag" ]; then latest_tag="v0.62.2" # fallback @@ -49,8 +45,6 @@ install_lazygit() { arch_str="arm64" fi - local url="https://github.com/jesseduffield/lazygit/releases/download/${latest_tag}/lazygit_${version}_linux_${arch_str}.tar.gz" - TMP_DIR="$(make_temp_dir)" cleanup() { rm -rf "$TMP_DIR"; } trap cleanup EXIT @@ -58,7 +52,7 @@ install_lazygit() { local dest="$TMP_DIR/lazygit.tar.gz" log_info "Downloading lazygit ${latest_tag}..." - download_file "$url" "$dest" + github_download_asset "jesseduffield/lazygit" "$latest_tag" "lazygit_${version}_linux_${arch_str}\.tar\.gz" "$dest" log_info "Extracting..." tar -xzf "$dest" -C "$TMP_DIR" diff --git a/installers/install_yazi.sh b/installers/install_yazi.sh index 5d6fc9d..af3a7c6 100755 --- a/installers/install_yazi.sh +++ b/installers/install_yazi.sh @@ -68,15 +68,15 @@ install_yazi() { pkg_install curl git log_info "Fetching latest Yazi version from GitHub..." - local latest_tag - latest_tag=$(curl -sL https://api.github.com/repos/sxyazi/yazi/releases/latest | grep '"tag_name":' | head -n1 | sed -E 's/.*"tag_name": "([^"]+)".*/\1/' || true) + local latest_tag="" + latest_tag=$(github_get_latest_release "sxyazi/yazi") + if [ -z "$latest_tag" ]; then latest_tag="v26.5.6" fi - local deb_url="https://github.com/sxyazi/yazi/releases/download/${latest_tag}/yazi-x86_64-unknown-linux-gnu.deb" - log_info "Downloading Yazi ${latest_tag} from ${deb_url}..." - download_file "$deb_url" "$TMP_DIR/yazi.deb" + log_info "Downloading Yazi ${latest_tag}..." + github_download_asset "sxyazi/yazi" "$latest_tag" "yazi-x86_64-unknown-linux-gnu\.deb" "$TMP_DIR/yazi.deb" log_info "Installing Yazi package..." sudo apt install -y "$TMP_DIR/yazi.deb" diff --git a/lib/github.sh b/lib/github.sh new file mode 100644 index 0000000..c171669 --- /dev/null +++ b/lib/github.sh @@ -0,0 +1,53 @@ +#!/usr/bin/env bash +# GitHub API helper functions for Bootstrap installers + +# Usage: github_get_latest_release +# Prints the tag_name of the latest release. +github_get_latest_release() { + local repo="$1" + local tag + tag=$(curl -fsSL "https://api.github.com/repos/$repo/releases/latest" | jq -r '.tag_name // empty') + echo "$tag" +} + +# Usage: github_get_download_url +# Finds the asset matching the regex pattern in the specified release tag and prints its download URL. +github_get_download_url() { + local repo="$1" + local tag="$2" + local pattern="$3" + + # If the tag is exactly 'latest', fetch the latest release asset list + local endpoint + if [ "$tag" = "latest" ]; then + endpoint="https://api.github.com/repos/$repo/releases/latest" + else + endpoint="https://api.github.com/repos/$repo/releases/tags/$tag" + fi + + local url + url=$(curl -fsSL "$endpoint" | jq -r --arg regex "$pattern" '.assets[] | select(.name | test($regex; "i")) | .browser_download_url' | head -n1) + echo "$url" +} + +# Usage: github_download_asset +# Resolves the URL for the matching asset and downloads it to dest_file. +github_download_asset() { + local repo="$1" + local tag="$2" + local pattern="$3" + local dest="$4" + + local url + url=$(github_get_download_url "$repo" "$tag" "$pattern") + + if [ -z "$url" ]; then + log_error "Could not find asset matching regex '$pattern' for $repo@$tag" + return 1 + fi + + log_info "Downloading $url ..." + download_file "$url" "$dest" +} + +export -f github_get_latest_release github_get_download_url github_download_asset