forked from Gitlink/gitlink-cli
197 lines
6.0 KiB
Bash
Executable File
197 lines
6.0 KiB
Bash
Executable File
#!/bin/bash
|
|
# GitLink CLI 一键安装脚本
|
|
# 自动检测平台,下载预编译二进制,安装 skills
|
|
# 用法: curl -sSL https://www.gitlink.org.cn/Gitlink/gitlink-cli/raw/master/install.sh | bash
|
|
set -e
|
|
|
|
REPO_OWNER="Gitlink"
|
|
REPO_NAME="gitlink-cli"
|
|
BINARY_NAME="gitlink-cli"
|
|
API_BASE="https://www.gitlink.org.cn"
|
|
INSTALL_DIR="${INSTALL_DIR:-/usr/local/bin}"
|
|
SKILLS_DIR="${HOME}/.gitlink/skills"
|
|
VERSION="${VERSION:-latest}"
|
|
|
|
# ---------- 颜色输出 ----------
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
CYAN='\033[0;36m'
|
|
NC='\033[0m'
|
|
|
|
info() { echo -e "${GREEN}[INFO]${NC} $*"; }
|
|
warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
|
|
error() { echo -e "${RED}[ERROR]${NC} $*"; }
|
|
step() { echo -e "${CYAN}[STEP]${NC} $*"; }
|
|
|
|
# ---------- 平台检测 ----------
|
|
detect_platform() {
|
|
local os arch
|
|
case "$(uname -s)" in
|
|
Linux) os="linux" ;;
|
|
Darwin) os="darwin" ;;
|
|
MINGW*|MSYS*|CYGWIN*) os="windows" ;;
|
|
*) error "不支持的操作系统: $(uname -s)"; exit 1 ;;
|
|
esac
|
|
case "$(uname -m)" in
|
|
x86_64|amd64) arch="amd64" ;;
|
|
aarch64|arm64) arch="arm64" ;;
|
|
*) error "不支持的架构: $(uname -m)"; exit 1 ;;
|
|
esac
|
|
echo "${os}/${arch}"
|
|
}
|
|
|
|
# ---------- 获取最新版本 ----------
|
|
fetch_latest_version() {
|
|
local releases_url="${API_BASE}/api/${REPO_OWNER}/${REPO_NAME}/releases.json"
|
|
|
|
info "获取最新版本..."
|
|
|
|
local releases
|
|
releases=$(curl -sSL --connect-timeout 10 --max-time 30 "${releases_url}" 2>/dev/null || true)
|
|
|
|
if [ -z "$releases" ]; then
|
|
error "无法获取发布列表: ${releases_url}"
|
|
exit 1
|
|
fi
|
|
|
|
# 尝试解析第一个 release 的 tag_name
|
|
local tag
|
|
tag=$(echo "$releases" | grep -o '"tag_name"[[:space:]]*:[[:space:]]*"[^"]*"' | head -1 | sed 's/.*"\([^"]*\)"$/\1/')
|
|
|
|
if [ -z "$tag" ]; then
|
|
# 备选:尝试直接匹配数组第一个元素的 tag_name
|
|
tag=$(echo "$releases" | grep -oP '"tag_name"\s*:\s*"\K[^"]+' | head -1)
|
|
fi
|
|
|
|
echo "${tag:-v0.1.0}"
|
|
}
|
|
|
|
# ---------- 下载二进制 ----------
|
|
download_binary() {
|
|
local platform="$1"
|
|
local version="$2"
|
|
local os="${platform%/*}"
|
|
local arch="${platform#*/}"
|
|
local archive_name="${BINARY_NAME}_${version}_${os}_${arch}.tar.gz"
|
|
local download_url="${API_BASE}/api/${REPO_OWNER}/${REPO_NAME}/releases/${version}/assets/${archive_name}"
|
|
|
|
step "下载: ${archive_name}"
|
|
info "URL: ${download_url}"
|
|
|
|
local tmpdir
|
|
tmpdir="$(mktemp -d)"
|
|
trap "rm -rf ${tmpdir}" EXIT
|
|
|
|
curl -sSL --connect-timeout 10 --max-time 120 -o "${tmpdir}/${archive_name}" "${download_url}"
|
|
|
|
if [ ! -s "${tmpdir}/${archive_name}" ]; then
|
|
error "下载失败,文件为空或不存在"
|
|
error "请确认以下版本已发布: ${version}"
|
|
error "发布页: ${API_BASE}/${REPO_OWNER}/${REPO_NAME}/releases"
|
|
exit 1
|
|
fi
|
|
|
|
step "解压..."
|
|
tar -xzf "${tmpdir}/${archive_name}" -C "${tmpdir}"
|
|
|
|
local binary_path="${tmpdir}/${BINARY_NAME}"
|
|
if [ ! -f "${binary_path}" ]; then
|
|
# 可能在子目录中
|
|
binary_path=$(find "${tmpdir}" -name "${BINARY_NAME}" -type f 2>/dev/null | head -1)
|
|
fi
|
|
|
|
if [ ! -f "${binary_path}" ]; then
|
|
error "解压后未找到二进制文件"
|
|
exit 1
|
|
fi
|
|
|
|
chmod +x "${binary_path}"
|
|
|
|
# ---------- 安装 ----------
|
|
step "安装到 ${INSTALL_DIR}/${BINARY_NAME}"
|
|
|
|
if [ ! -w "${INSTALL_DIR}" ]; then
|
|
warn "需要管理员权限写入 ${INSTALL_DIR}"
|
|
sudo mkdir -p "${INSTALL_DIR}"
|
|
sudo mv "${binary_path}" "${INSTALL_DIR}/${BINARY_NAME}"
|
|
sudo chmod +x "${INSTALL_DIR}/${BINARY_NAME}"
|
|
else
|
|
mkdir -p "${INSTALL_DIR}"
|
|
mv "${binary_path}" "${INSTALL_DIR}/${BINARY_NAME}"
|
|
fi
|
|
|
|
info "二进制: ${INSTALL_DIR}/${BINARY_NAME}"
|
|
}
|
|
|
|
# ---------- 安装 skills ----------
|
|
install_skills() {
|
|
local version="$1"
|
|
step "安装 skills..."
|
|
|
|
mkdir -p "${SKILLS_DIR}"
|
|
|
|
local skills_url="${API_BASE}/api/${REPO_OWNER}/${REPO_NAME}/releases/${version}/assets/${BINARY_NAME}_${version}_skills.tar.gz"
|
|
local tmpdir
|
|
tmpdir="$(mktemp -d)"
|
|
|
|
curl -sSL --connect-timeout 10 --max-time 60 -o "${tmpdir}/skills.tar.gz" "${skills_url}" 2>/dev/null || true
|
|
|
|
if [ -s "${tmpdir}/skills.tar.gz" ]; then
|
|
tar -xzf "${tmpdir}/skills.tar.gz" -C "${SKILLS_DIR}" 2>/dev/null || true
|
|
info "Skills 安装到: ${SKILLS_DIR}"
|
|
else
|
|
warn "Skills 包不可用(可稍后通过 gitlink-cli-install-skills 安装)"
|
|
fi
|
|
|
|
rm -rf "${tmpdir}"
|
|
}
|
|
|
|
# ---------- 验证安装 ----------
|
|
verify() {
|
|
step "验证安装..."
|
|
if command -v "${BINARY_NAME}" >/dev/null 2>&1; then
|
|
info "安装成功! $("${BINARY_NAME}" version 2>/dev/null || echo "${BINARY_NAME}")"
|
|
elif [ -x "${INSTALL_DIR}/${BINARY_NAME}" ]; then
|
|
info "安装成功! $("${INSTALL_DIR}/${BINARY_NAME}" version 2>/dev/null || echo "${INSTALL_DIR}/${BINARY_NAME}")"
|
|
warn "请将 ${INSTALL_DIR} 添加到 PATH: export PATH=${INSTALL_DIR}:\$PATH"
|
|
else
|
|
error "安装验证失败"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# ---------- main ----------
|
|
main() {
|
|
echo ""
|
|
echo " ╔══════════════════════════════════════╗"
|
|
echo " ║ GitLink CLI 一键安装 ║"
|
|
echo " ╚══════════════════════════════════════╝"
|
|
echo ""
|
|
|
|
local platform
|
|
platform=$(detect_platform)
|
|
info "检测到平台: ${platform}"
|
|
|
|
local version
|
|
if [ "${VERSION}" = "latest" ]; then
|
|
version=$(fetch_latest_version)
|
|
info "最新版本: ${version}"
|
|
else
|
|
version="${VERSION}"
|
|
info "指定版本: ${version}"
|
|
fi
|
|
|
|
download_binary "${platform}" "${version}"
|
|
install_skills "${version}"
|
|
verify
|
|
|
|
echo ""
|
|
info "快速开始:"
|
|
info " gitlink-cli auth login # 登录账号"
|
|
info " gitlink-cli --help # 查看所有命令"
|
|
echo ""
|
|
}
|
|
|
|
main "$@"
|