gitlink-cli/skills/gitlink-shared/scripts/detect-cli.sh

58 lines
2.0 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# ================================================================
# gitlink-cli 路径检测
# 优先使用项目本地编译版本,不存在则使用系统 PATH 版本
# 在所有脚本开头 source 本文件source "$(dirname "$0")/../../gitlink-shared/scripts/detect-cli.sh"
# ================================================================
# 查找项目根目录(从当前脚本位置向上找)
find_project_root() {
local dir="$1"
while [ "$dir" != "/" ]; do
if [ -f "$dir/go.mod" ] && grep -q "gitlink-cli" "$dir/go.mod" 2>/dev/null; then
echo "$dir"
return 0
fi
dir="$(dirname "$dir")"
done
echo ""
}
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)"
# 本地编译版本路径
LOCAL_CLI=""
if [ -f "$PROJECT_ROOT/gitlink-cli.exe" ]; then
LOCAL_CLI="$PROJECT_ROOT/gitlink-cli.exe"
elif [ -f "$PROJECT_ROOT/gitlink-cli" ]; then
LOCAL_CLI="$PROJECT_ROOT/gitlink-cli"
fi
# 判断:如果本地版本存在且包含完整命令,优先使用
if [ -n "$LOCAL_CLI" ]; then
# 检查本地版本是否包含 commit 命令
if "$LOCAL_CLI" commit --help > /dev/null 2>&1; then
echo "[INFO] 使用本地编译版本: $LOCAL_CLI"
# 创建 wrapper 目录并加到 PATH 最前面
WRAPPER_DIR="$PROJECT_ROOT/.cli-bin"
mkdir -p "$WRAPPER_DIR"
# 生成 wrapper 脚本
cat > "$WRAPPER_DIR/gitlink-cli" << WRAPPER_EOF
#!/bin/bash
exec "$LOCAL_CLI" "\$@"
WRAPPER_EOF
chmod +x "$WRAPPER_DIR/gitlink-cli" 2>/dev/null || true
export PATH="$WRAPPER_DIR:$PATH"
else
echo "[INFO] 系统 PATH 版本: $(which gitlink-cli 2>/dev/null || echo '未找到')"
fi
else
if command -v gitlink-cli > /dev/null 2>&1; then
echo "[INFO] 使用系统 PATH 版本: $(which gitlink-cli)"
else
echo "[ERROR] 未找到 gitlink-cli请先安装或编译"
exit 1
fi
fi