feat: enhance installation script with requirement checks and streamlined download process

- Added a function to check for required commands (curl).
- Simplified the download function to exclusively use curl for downloading assets.
- Improved error handling and user feedback when fetching the latest release information.
- Updated the main installation function to construct the binary name directly and provide clearer output during the installation process.
This commit is contained in:
dacrab
2024-12-29 16:51:01 +02:00
parent a9094d34dd
commit ad7ae6fa58
2 changed files with 76 additions and 47 deletions

View File

@@ -13,6 +13,14 @@ NC='\033[0m'
TMP_DIR=$(mktemp -d)
trap 'rm -rf "$TMP_DIR"' EXIT
# Check for required commands
check_requirements() {
if ! command -v curl >/dev/null 2>&1; then
echo -e "${RED}Error: curl is required${NC}"
exit 1
fi
}
# Detect system information
detect_system() {
local os arch suffix
@@ -42,19 +50,11 @@ detect_system() {
echo "$os $arch $suffix"
}
# Download with progress using curl or wget
# Download with progress
download() {
local url="$1"
local output="$2"
if command -v curl >/dev/null 2>&1; then
curl -#L "$url" -o "$output"
elif command -v wget >/dev/null 2>&1; then
wget --show-progress -q "$url" -O "$output"
else
echo -e "${RED}Error: curl or wget is required${NC}"
exit 1
fi
curl -#L "$url" -o "$output"
}
# Check and create installation directory
@@ -69,38 +69,10 @@ setup_install_dir() {
fi
}
# Find matching asset from release
find_asset() {
local json="$1"
local os="$2"
local arch="$3"
local suffix="$4"
# Try possible binary names
local binary_names=(
"cursor-id-modifier_${os}_${arch}${suffix}" # lowercase os
"cursor-id-modifier_$(tr '[:lower:]' '[:upper:]' <<< ${os:0:1})${os:1}_${arch}${suffix}" # capitalized os
)
local url=""
for name in "${binary_names[@]}"; do
echo -e "${BLUE}Looking for asset: $name${NC}"
url=$(echo "$json" | grep -o "\"browser_download_url\": \"[^\"]*${name}\"" | cut -d'"' -f4)
if [ -n "$url" ]; then
echo -e "${GREEN}Found matching asset: $name${NC}"
echo "$url"
return 0
fi
done
# If no match found, show available assets
echo -e "${YELLOW}Available assets:${NC}"
echo "$json" | grep "\"name\":" | cut -d'"' -f4
return 1
}
# Main installation function
main() {
check_requirements
echo -e "${BLUE}Starting installation...${NC}"
# Detect system
@@ -109,7 +81,6 @@ main() {
# Set installation directory
INSTALL_DIR="/usr/local/bin"
[ "$OS" = "darwin" ] && INSTALL_DIR="/usr/local/bin"
# Setup installation directory
setup_install_dir "$INSTALL_DIR"
@@ -117,17 +88,22 @@ main() {
# Get latest release info
echo -e "${BLUE}Fetching latest release information...${NC}"
LATEST_URL="https://api.github.com/repos/dacrab/go-cursor-help/releases/latest"
RELEASE_JSON=$(curl -s "$LATEST_URL")
# Find matching asset
DOWNLOAD_URL=$(find_asset "$RELEASE_JSON" "$OS" "$ARCH" "$SUFFIX")
# Construct binary name
BINARY_NAME="cursor-id-modifier_${OS}_${ARCH}${SUFFIX}"
echo -e "${BLUE}Looking for asset: $BINARY_NAME${NC}"
# Get download URL directly
DOWNLOAD_URL=$(curl -s "$LATEST_URL" | tr -d '\n' | grep -o "\"browser_download_url\": \"[^\"]*${BINARY_NAME}[^\"]*\"" | cut -d'"' -f4)
if [ -z "$DOWNLOAD_URL" ]; then
echo -e "${RED}Error: Could not find appropriate binary for $OS $ARCH${NC}"
exit 1
fi
echo -e "${BLUE}Downloading latest release...${NC}"
echo -e "${GREEN}Found matching asset: $BINARY_NAME${NC}"
echo -e "${BLUE}Downloading from: $DOWNLOAD_URL${NC}"
download "$DOWNLOAD_URL" "$TMP_DIR/cursor-id-modifier"
# Install binary
@@ -138,9 +114,9 @@ main() {
echo -e "${GREEN}Installation completed successfully!${NC}"
echo -e "${BLUE}Running cursor-id-modifier...${NC}"
# Run the program
# Run the program with sudo, preserving environment variables
export AUTOMATED_MODE=1
if ! cursor-id-modifier; then
if ! sudo -E cursor-id-modifier; then
echo -e "${RED}Failed to run cursor-id-modifier${NC}"
exit 1
fi