Files
agent-skills/skills/gitea-actor/scripts/setup.sh
yanghuajun336 ab5e59e999 feat: 优化 gitea-actor 技能,新增脚本工具和完整文档
- 新增 setup.sh 配置脚本
- 新增 api-examples.sh API 函数库
- 新增 gitea-cli.py Python CLI 工具
- 完善文档结构 (1640 行)
- 添加实际用例和工作流示例
- 增强故障排除和最佳实践指南
2026-03-31 22:35:30 +08:00

185 lines
5.0 KiB
Bash
Executable File
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
# Gitea-Actor 配置脚本
# 版本: 1.0.0
set -e
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 日志函数
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# 检查依赖
check_dependencies() {
local deps=("curl" "jq")
for dep in "${deps[@]}"; do
if ! command -v "$dep" &> /dev/null; then
log_error "缺少依赖: $dep"
log_info "请安装:"
if [[ "$dep" == "jq" ]]; then
log_info " Ubuntu/Debian: sudo apt install jq"
log_info " CentOS/RHEL: sudo yum install jq"
log_info " macOS: brew install jq"
fi
exit 1
fi
done
}
# 显示标题
show_header() {
echo -e "${BLUE}========================================${NC}"
echo -e "${GREEN} Gitea-Actor 配置向导${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""
}
# 验证 Gitea 连接
test_gitea_connection() {
local url=$1
local token=$2
log_info "正在验证 Gitea 连接..."
# 检查 URL 格式
if [[ ! "$url" =~ ^https?:// ]]; then
log_error "URL 格式不正确,请使用 http:// 或 https:// 开头"
return 1
fi
# 发送测试请求
local response
local http_code
response=$(curl -s -w "\n%{http_code}" \
-H "Authorization: token $token" \
-H "Accept: application/json" \
"$url/api/v1/user" 2>/dev/null || true)
http_code=$(echo "$response" | tail -n1)
local response_body=$(echo "$response" | sed '$d')
if [[ "$http_code" == "200" ]]; then
local username=$(echo "$response_body" | jq -r '.login // .username' 2>/dev/null || echo "unknown")
log_success "Gitea 连接成功!"
log_info "用户: $username"
return 0
else
log_error "Gitea 连接失败 (HTTP $http_code)"
if [[ -n "$response_body" ]]; then
log_info "错误信息: $(echo "$response_body" | jq -r '.message // .' 2>/dev/null || echo "$response_body")"
fi
return 1
fi
}
# 主函数
main() {
show_header
check_dependencies
# 检查现有配置
local config_file="$HOME/.gitea-actor.env"
if [[ -f "$config_file" ]]; then
log_warn "发现现有配置文件: $config_file"
echo ""
log_info "现有配置:"
grep -E "^(export )?(GITEA_URL|GITEA_TOKEN)=" "$config_file" || true
echo ""
read -p "是否覆盖现有配置? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
log_info "已取消配置"
exit 0
fi
fi
# 获取 Gitea URL
local default_url="https://gitea.com"
echo -e "${YELLOW}提示:${NC} 请输入您的 Gitea 实例 URL"
echo -e " 例如: https://gitea.example.com"
echo -e " 或: https://codehub.zlinkcloudtech.com"
echo ""
read -p "Gitea URL [默认: $default_url]: " gitea_url
gitea_url=${gitea_url:-$default_url}
# 移除末尾的斜杠
gitea_url=$(echo "$gitea_url" | sed 's|/$||')
# 获取访问令牌
echo ""
echo -e "${YELLOW}提示:${NC} 请提供个人访问令牌 (Personal Access Token)"
echo -e " 可以在 Gitea 的 设置 -> 应用 中创建"
echo ""
read -p "访问令牌: " gitea_token
if [[ -z "$gitea_token" ]]; then
log_error "访问令牌不能为空"
exit 1
fi
# 测试连接
echo ""
if test_gitea_connection "$gitea_url" "$gitea_token"; then
# 保存配置
cat > "$config_file" << EOF
# Gitea-Actor 配置文件
# 生成时间: $(date)
export GITEA_URL="$gitea_url"
export GITEA_TOKEN="$gitea_token"
# 使用方式:
# source ~/.gitea-actor.env
# 或添加到 ~/.bashrc / ~/.zshrc
EOF
log_success "配置已保存到: $config_file"
echo ""
log_info "使用方法:"
echo " 1. 立即加载配置: source $config_file"
echo " 2. 永久生效: 将以下行添加到 ~/.bashrc 或 ~/.zshrc:"
echo " source $config_file"
echo ""
log_info "验证命令:"
echo " curl -s -H \"Authorization: token \$GITEA_TOKEN\" \"\$GITEA_URL/api/v1/user\" | jq ."
# 询问是否立即加载配置
echo ""
read -p "是否立即加载配置到当前shell (Y/n): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Nn]$ ]]; then
if source "$config_file" 2>/dev/null; then
log_success "配置已加载到当前shell"
else
log_warn "无法自动加载配置,请手动运行: source $config_file"
fi
fi
else
log_error "配置失败请检查URL和令牌是否正确"
exit 1
fi
}
# 运行主函数
main "$@"