Files
hose-use/scripts/install.sh
T

183 lines
4.8 KiB
Bash
Executable File

#!/usr/bin/env bash
set -e
REPO="yunyingk/hose-use"
echo "==> Detecting platform and architecture..."
OS="$(uname -s | tr '[:upper:]' '[:lower:]')"
ARCH="$(uname -m)"
case "$OS" in
darwin)
PLATFORM="darwin"
;;
linux)
PLATFORM="linux"
;;
*)
echo "Unsupported operating system: $OS" >&2
exit 1
;;
esac
case "$ARCH" in
x86_64|amd64)
ARCH_NAME="x64"
;;
arm64|aarch64)
ARCH_NAME="arm64"
;;
*)
echo "Unsupported architecture: $ARCH" >&2
exit 1
;;
esac
# Resolve best writable bin directory without requiring sudo
TARGET_DIR=""
for candidate in "/opt/homebrew/bin" "/usr/local/bin" "$HOME/.local/bin" "$HOME/bin"; do
if [ -d "$candidate" ] && [ -w "$candidate" ]; then
TARGET_DIR="$candidate"
break
fi
done
if [ -z "$TARGET_DIR" ]; then
mkdir -p "$HOME/.local/bin"
TARGET_DIR="$HOME/.local/bin"
fi
ASSET_NAME="hose-use-${PLATFORM}-${ARCH_NAME}"
TARGET_PATH="${TARGET_DIR}/hose-use"
get_file_size() {
local file="$1"
if [ ! -f "$file" ]; then
echo "0"
return
fi
local sz
sz=$(stat -f%z "$file" 2>/dev/null || stat -c%s "$file" 2>/dev/null || wc -c < "$file" | tr -d ' ')
echo "$sz"
}
format_bytes() {
local bytes="$1"
if [ -z "$bytes" ] || [ "$bytes" -le 0 ]; then
echo "0 B"
return
fi
if [ "$bytes" -ge 1048576 ]; then
awk "BEGIN {printf \"%.2f MB (%'d bytes)\", $bytes / 1048576, $bytes}" 2>/dev/null || awk "BEGIN {printf \"%.2f MB (%d bytes)\", $bytes / 1048576, $bytes}"
elif [ "$bytes" -ge 1024 ]; then
awk "BEGIN {printf \"%.2f KB (%d bytes)\", $bytes / 1024, $bytes}"
else
echo "${bytes} bytes"
fi
}
extract_host() {
local url="$1"
echo "$url" | sed -e 's|^[^/]*//||' -e 's|/.*$||'
}
download_candidate() {
local url="$1"
local dest="$2"
local label="$3"
local host
host="$(extract_host "$url")"
echo "==> Downloading from ${label}"
echo " Source Host: ${host}"
echo " Source URL: ${url}"
# Test connectivity & headers first
if ! curl -fIs --connect-timeout 4 "$url" >/dev/null 2>&1; then
echo " ↳ Source unreachable or asset not ready (404), falling back to next mirror..."
return 1
fi
# Download with progress bar
if curl -fL --progress-bar "$url" -o "$dest"; then
local size_bytes
size_bytes="$(get_file_size "$dest")"
if [ "$size_bytes" -gt 1000000 ]; then
echo "✓ Download complete! Total size: $(format_bytes "$size_bytes")"
echo "✓ Successfully fetched from host: ${host}"
return 0
fi
fi
rm -f "$dest"
return 1
}
echo "==> Target install location: ${TARGET_PATH}"
TMP_FILE="$(mktemp)"
DOWNLOAD_SUCCESS=false
# Candidate mirrors
GITEA_URL="https://git.ying-qing.cn/gitea/hose-use/releases/download/latest/${ASSET_NAME}"
GITHUB_URL="https://github.com/${REPO}/releases/latest/download/${ASSET_NAME}"
GHPROXY_URL="https://ghfast.top/https://github.com/${REPO}/releases/latest/download/${ASSET_NAME}"
# 1. Try Gitea mirror
if [ "${HOSE_USE_MIRROR:-auto}" = "gitea" ] || [ "${HOSE_USE_MIRROR:-auto}" = "auto" ]; then
if download_candidate "$GITEA_URL" "$TMP_FILE" "Gitea High-Speed Mirror"; then
DOWNLOAD_SUCCESS=true
fi
fi
# 2. Try direct GitHub Releases
if [ "$DOWNLOAD_SUCCESS" = "false" ]; then
if download_candidate "$GITHUB_URL" "$TMP_FILE" "GitHub Releases (Direct)"; then
DOWNLOAD_SUCCESS=true
fi
fi
# 3. Try Fast CDN Proxy Fallback
if [ "$DOWNLOAD_SUCCESS" = "false" ]; then
if download_candidate "$GHPROXY_URL" "$TMP_FILE" "Fast CDN Mirror (Fallback)"; then
DOWNLOAD_SUCCESS=true
fi
fi
if [ "$DOWNLOAD_SUCCESS" = "false" ]; then
echo "Error: Failed to download ${ASSET_NAME} from all candidate mirrors." >&2
rm -f "$TMP_FILE"
exit 1
fi
chmod +x "$TMP_FILE"
mv "$TMP_FILE" "$TARGET_PATH"
# Create command aliases pointing at the same single binary.
if [ -w "$TARGET_DIR" ]; then
ln -sf "$TARGET_PATH" "${TARGET_DIR}/hose" 2>/dev/null || true
ln -sf "$TARGET_PATH" "${TARGET_DIR}/hose-aipro" 2>/dev/null || true
fi
# Ad-hoc sign on macOS
if [ "$PLATFORM" = "darwin" ] && command -v codesign >/dev/null 2>&1; then
echo "==> Applying macOS code signature..."
codesign --force --sign - "$TARGET_PATH" 2>/dev/null || true
fi
echo "==> Initializing agent skill to ~/.agents/skills/hose-use..."
"$TARGET_PATH" skill install >/dev/null 2>&1 || true
echo "✓ Agent skill installed to ~/.agents/skills/hose-use"
echo ""
echo "✨ hose-use successfully installed to ${TARGET_PATH}!"
"$TARGET_PATH" version
echo ""
echo "Quick start:"
echo " 1. Login: hose-use auth login --phone=177... --password=..."
echo " 2. List corps: hose-use corps list"
echo " 3. List workflows: hose-use workflow list"
echo " 4. Switch mode: hose-use config mode stateless"
echo " 5. Self-update: hose-use update"
echo " AI PRO alias: hose-aipro"
echo ""