From 1e2728fe3385bd32f044f124e8d9992de8e4a6d1 Mon Sep 17 00:00:00 2001 From: Yar Kravtsov Date: Sat, 24 May 2025 09:56:51 +0300 Subject: [PATCH] feat(install): enhance installer script robustness and flexibility --- README.md | 6 ++++ RELEASE.md | 8 +++++- install.sh | 84 ++++++++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 82 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 39fff91..51d2d89 100644 --- a/README.md +++ b/README.md @@ -15,15 +15,21 @@ lnk push "setup" ```bash # Quick install (recommended) curl -sSL https://raw.githubusercontent.com/yarlson/lnk/main/install.sh | bash +``` +```bash # Homebrew (macOS/Linux) brew tap yarlson/lnk brew install lnk +``` +```bash # Manual download wget https://github.com/yarlson/lnk/releases/latest/download/lnk-$(uname -s | tr '[:upper:]' '[:lower:]')-amd64 chmod +x lnk-* && sudo mv lnk-* /usr/local/bin/lnk +``` +```bash # From source git clone https://github.com/yarlson/lnk.git && cd lnk && go build . && sudo mv lnk /usr/local/bin/ ``` diff --git a/RELEASE.md b/RELEASE.md index 55a9f03..afc3885 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -64,11 +64,13 @@ git push origin v1.0.0 ## What GoReleaser Does 1. **Builds binaries** for multiple platforms: + - Linux (amd64, arm64) - macOS (amd64, arm64) - Windows (amd64) 2. **Creates archives** with consistent naming: + - `lnk_Linux_x86_64.tar.gz` - `lnk_Darwin_arm64.tar.gz` - etc. @@ -76,6 +78,7 @@ git push origin v1.0.0 3. **Generates checksums** for verification 4. **Creates GitHub release** with: + - Automatic changelog from conventional commits - Installation instructions - Download links for all platforms @@ -112,17 +115,20 @@ ls -la dist/ After a release is published, users can install lnk using multiple methods: ### 1. Shell Script (Recommended) + ```bash curl -sSL https://raw.githubusercontent.com/yarlson/lnk/main/install.sh | bash ``` ### 2. Homebrew (macOS/Linux) + ```bash brew tap yarlson/lnk brew install lnk ``` ### 3. Manual Download + ```bash # Download from GitHub releases wget https://github.com/yarlson/lnk/releases/latest/download/lnk_Linux_x86_64.tar.gz @@ -181,4 +187,4 @@ The Homebrew formula is automatically maintained in the [homebrew-lnk](https://g 1. Check that the GITHUB_TOKEN has access to the homebrew-lnk repository 2. Verify the repository name and owner in `.goreleaser.yml` 3. Check the release workflow logs for Homebrew-related errors -4. Ensure the homebrew-lnk repository exists and is accessible \ No newline at end of file +4. Ensure the homebrew-lnk repository exists and is accessible diff --git a/install.sh b/install.sh index 9b65ac1..684d706 100644 --- a/install.sh +++ b/install.sh @@ -17,6 +17,9 @@ REPO="yarlson/lnk" INSTALL_DIR="/usr/local/bin" BINARY_NAME="lnk" +# Fallback version if redirect fails +FALLBACK_VERSION="v0.0.2" + # Detect OS and architecture detect_platform() { local os arch @@ -45,11 +48,44 @@ detect_platform() { echo "${os}_${arch}" } -# Get the latest release version +# Get latest version by following redirect get_latest_version() { - curl -s "https://api.github.com/repos/${REPO}/releases/latest" | \ - grep '"tag_name":' | \ - sed -E 's/.*"([^"]+)".*/\1/' + echo -e "${BLUE}Getting latest release version...${NC}" >&2 + + # Get redirect location from releases/latest + local redirect_url + redirect_url=$(curl -s -I "https://github.com/${REPO}/releases/latest" | grep -i "^location:" | sed 's/\r$//' | cut -d' ' -f2-) + + if [ -z "$redirect_url" ]; then + echo -e "${YELLOW}⚠ Could not get redirect URL, using fallback version ${FALLBACK_VERSION}${NC}" >&2 + echo "$FALLBACK_VERSION" + return 0 + fi + + # Extract version from redirect URL (format: https://github.com/user/repo/releases/tag/v1.2.3) + local version + version=$(echo "$redirect_url" | sed -E 's|.*/releases/tag/([^/]*)\s*$|\1|') + + if [ -z "$version" ] || [ "$version" = "$redirect_url" ]; then + echo -e "${YELLOW}⚠ Could not parse version from redirect URL: $redirect_url${NC}" >&2 + echo -e "${YELLOW}Using fallback version ${FALLBACK_VERSION}${NC}" >&2 + echo "$FALLBACK_VERSION" + return 0 + fi + + echo "$version" +} + +# Get version to install +get_version() { + # Allow override via environment variable + if [ -n "$LNK_VERSION" ]; then + echo "$LNK_VERSION" + elif [ -n "$1" ]; then + echo "$1" + else + get_latest_version + fi } # Download and install @@ -59,14 +95,9 @@ install_lnk() { echo -e "${BLUE}🔗 Installing lnk...${NC}" platform=$(detect_platform) - version=$(get_latest_version) + version=$(get_version "$1") - if [ -z "$version" ]; then - echo -e "${RED}Error: Failed to get latest version${NC}" - exit 1 - fi - - echo -e "${BLUE}Latest version: ${version}${NC}" + echo -e "${BLUE}Version: ${version}${NC}" echo -e "${BLUE}Platform: ${platform}${NC}" # Download URL @@ -82,6 +113,16 @@ install_lnk() { # Download the binary if ! curl -sL "$url" -o "$filename"; then echo -e "${RED}Error: Failed to download ${url}${NC}" + echo -e "${YELLOW}Please check if the release exists at: https://github.com/${REPO}/releases/tag/${version}${NC}" + echo -e "${YELLOW}Available releases: https://github.com/${REPO}/releases${NC}" + exit 1 + fi + + # Check if we got an HTML error page instead of the binary + if file "$filename" 2>/dev/null | grep -q "HTML"; then + echo -e "${RED}Error: Downloaded file appears to be an HTML page (404 error)${NC}" + echo -e "${YELLOW}The release ${version} might not exist.${NC}" + echo -e "${YELLOW}Available releases: https://github.com/${REPO}/releases${NC}" exit 1 fi @@ -107,20 +148,33 @@ install_lnk() { echo -e "${GREEN}✅ lnk installed successfully!${NC}" echo -e "${GREEN}Run 'lnk --help' to get started.${NC}" + + # Test the installation + if command -v lnk >/dev/null 2>&1; then + echo -e "${GREEN}Installed version: $(lnk --version)${NC}" + fi } # Check if running with --help if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then echo "Lnk installer script" echo "" - echo "Usage: curl -sSL https://raw.githubusercontent.com/yarlson/lnk/main/install.sh | bash" + echo "Usage:" + echo " curl -sSL https://raw.githubusercontent.com/yarlson/lnk/main/install.sh | bash" + echo " curl -sSL https://raw.githubusercontent.com/yarlson/lnk/main/install.sh | bash -s v0.0.1" + echo " LNK_VERSION=v0.0.1 curl -sSL https://raw.githubusercontent.com/yarlson/lnk/main/install.sh | bash" echo "" echo "This script will:" echo " 1. Detect your OS and architecture" - echo " 2. Download the latest lnk release" - echo " 3. Install it to /usr/local/bin (requires sudo)" + echo " 2. Auto-detect the latest release by following GitHub redirects" + echo " 3. Download and install to /usr/local/bin (requires sudo)" + echo "" + echo "Environment variables:" + echo " LNK_VERSION - Specify version to install (e.g., v0.0.1)" + echo "" + echo "Manual installation: https://github.com/yarlson/lnk/releases" exit 0 fi # Run the installer -install_lnk \ No newline at end of file +install_lnk "$1" \ No newline at end of file