#!/bin/sh set -e # Guardy installer - https://guardy.run # Usage: curl https://guardy.run | sh GUARDY_VERSION="${GUARDY_VERSION:-latest}" INSTALL_DIR="${INSTALL_DIR:-$HOME/.local/bin}" GITLAB_PROJECT="deepbrain.space/guardy" VERSION_ENDPOINT="${VERSION_ENDPOINT:-https://guardy.dev/version}" # Colors BLUE='\033[0;34m' GREEN='\033[0;32m' YELLOW='\033[1;33m' RED='\033[0;31m' NC='\033[0m' # No Color info() { echo "${BLUE}ℹ${NC} $1" } success() { echo "${GREEN}✓${NC} $1" } warn() { echo "${YELLOW}⚠${NC} $1" } error() { echo "${RED}✗${NC} $1" exit 1 } # Detect target triple (similar to moon's approach) detect_target() { arch=$(uname -sm) case "$arch" in "Darwin x86_64") echo "x86_64-apple-darwin" ;; "Darwin arm64") echo "aarch64-apple-darwin" ;; "Linux aarch64" | "Linux arm64") # Detect musl vs gnu if ldd --version 2>&1 | grep -q "musl"; then echo "aarch64-unknown-linux-musl" else echo "aarch64-unknown-linux-gnu" fi ;; "Linux x86_64") # Detect musl vs gnu if ldd --version 2>&1 | grep -q "musl"; then echo "x86_64-unknown-linux-musl" else echo "x86_64-unknown-linux-gnu" fi ;; *) error "Unsupported system or architecture: $arch" ;; esac } # Get latest version from guardy.dev get_latest_version() { if [ "$GUARDY_VERSION" = "latest" ]; then # Use the /version endpoint from guardy.dev (Cloudflare Pages Function) # This is the single source of truth for the latest version LATEST=$(curl -s "$VERSION_ENDPOINT" | sed -n 's/.*"version":"\([^"]*\)".*/\1/p') if [ -z "$LATEST" ]; then # Fallback to GitLab API if guardy.dev is unreachable LATEST=$(curl -s "https://gitlab.com/api/v4/projects/${GITLAB_PROJECT}/releases" | \ sed -n 's/.*"tag_name":"\(guardy-[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)".*/\1/p' | \ head -1) fi if [ -z "$LATEST" ]; then error "Could not determine latest version" fi echo "$LATEST" else echo "$GUARDY_VERSION" fi } # Download and install install_guardy() { TARGET=$(detect_target) if [ "$GUARDY_VERSION" = "latest" ]; then info "Fetching latest version from guardy.dev..." fi VERSION=$(get_latest_version) # Strip 'guardy-' prefix from version for filename VERSION_NUM="${VERSION#guardy-}" info "Installing Guardy ${VERSION} for ${TARGET}..." # Construct download URL # Format: https://gitlab.com/deepbrain.space/guardy/-/releases/guardy-0.2.3/downloads/guardy-0.2.3-x86_64-unknown-linux-gnu.tar.gz TARBALL="guardy-${VERSION_NUM}-${TARGET}.tar.gz" DOWNLOAD_URL="https://gitlab.com/${GITLAB_PROJECT}/-/releases/${VERSION}/downloads/${TARBALL}" info "Downloading from: ${DOWNLOAD_URL}" # Create temp directory TMP_DIR=$(mktemp -d) cd "$TMP_DIR" # Download if ! curl -L -o "$TARBALL" "$DOWNLOAD_URL"; then error "Failed to download guardy. Please check if version ${VERSION} exists." fi # Extract info "Extracting..." tar xzf "$TARBALL" # Create install directory if it doesn't exist mkdir -p "$INSTALL_DIR" # Install binary info "Installing to ${INSTALL_DIR}..." mv guardy "$INSTALL_DIR/guardy" chmod +x "$INSTALL_DIR/guardy" # Cleanup cd - rm -rf "$TMP_DIR" success "Guardy installed successfully!" # Check if in PATH if ! echo "$PATH" | grep -q "$INSTALL_DIR"; then warn "$INSTALL_DIR is not in your PATH" echo "" echo "Add this to your shell config (~/.bashrc, ~/.zshrc, etc.):" echo " export PATH=\"\$PATH:$INSTALL_DIR\"" echo "" fi # Show next steps echo "" echo "${BLUE}Next steps:${NC}" echo " 1. Reload your shell or run: export PATH=\"\$PATH:$INSTALL_DIR\"" echo " 2. Initialize in your project: ${GREEN}guardy init${NC}" echo " 3. Install git hooks: ${GREEN}guardy hooks install${NC}" echo "" echo "Documentation: https://guardy.dev/docs" } # Main main() { echo "" echo "${BLUE}🛡️ Guardy Installer${NC}" echo "" install_guardy } main