--- description: Switch default Gitea organization --- # gitea-switch-org 切换默认 Gitea 组织。 ## Features - 更新配置文件中的默认组织 - 验证组织是否存在 - 立即生效,无需重启 ## User Input Format ``` ``` **Examples**: - `ai` - Switch to 'ai' organization - `my-team` - Switch to 'my-team' organization ## Steps ### 1. Check Configuration ```bash config_file="$HOME/.config/gitea/config.env" if [ ! -f "$config_file" ]; then echo "❌ Gitea 未配置" echo "请先运行 /gitea-reset 进行配置" exit 1 fi source "$config_file" ``` ### 2. Parse User Input ```bash new_org="$1" if [ -z "$new_org" ]; then echo "用法: /gitea-switch-org " echo "" echo "示例:" echo " /gitea-switch-org ai" echo " /gitea-switch-org my-team" exit 1 fi ``` ### 3. Validate Organization Exists ```bash echo "正在验证组织 '$new_org'..." response=$(curl -s -w "\n%{http_code}" \ -H "Authorization: token $GITEA_TOKEN" \ "${GITEA_URL}/api/v1/orgs/${new_org}") http_code=$(echo "$response" | tail -n1) body=$(echo "$response" | sed '$d') if [ "$http_code" != "200" ]; then echo "❌ 组织 '$new_org' 不存在或无权限访问" # Suggest available organizations echo "" echo "你可以访问的组织:" curl -s -H "Authorization: token $GITEA_TOKEN" \ "${GITEA_URL}/api/v1/user/orgs" | jq -r '.[].username' | sed 's/^/ - /' exit 1 fi org_name=$(echo "$body" | jq -r '.full_name // .username') echo "✓ 组织验证成功: $org_name" ``` ### 4. Update Configuration File ```bash echo "正在更新配置..." # Check if GITEA_DEFAULT_ORG already exists in config if grep -q "^GITEA_DEFAULT_ORG=" "$config_file"; then # Update existing line (macOS compatible) if [[ "$OSTYPE" == "darwin"* ]]; then sed -i '' "s/^GITEA_DEFAULT_ORG=.*/GITEA_DEFAULT_ORG=$new_org/" "$config_file" else sed -i "s/^GITEA_DEFAULT_ORG=.*/GITEA_DEFAULT_ORG=$new_org/" "$config_file" fi else # Add new line after GITEA_TOKEN if [[ "$OSTYPE" == "darwin"* ]]; then sed -i '' "/^GITEA_TOKEN=/a\\ GITEA_DEFAULT_ORG=$new_org " "$config_file" else sed -i "/^GITEA_TOKEN=/a GITEA_DEFAULT_ORG=$new_org" "$config_file" fi fi echo "✓ 默认组织已切换到: $new_org" ``` ### 5. Display Summary ```bash echo "" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo "切换完成" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo "" echo " 新的默认组织: $new_org" echo "" echo "现在可以:" echo " - 创建仓库: /create-gitea-repo my-project" echo " (将创建到 $new_org/my-project)" echo " - 查看配置: /gitea-config" echo "" ``` ## Output Example ``` 正在验证组织 'ai'... ✓ 组织验证成功: AI Team 正在更新配置... ✓ 默认组织已切换到: ai ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 切换完成 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 新的默认组织: ai 现在可以: - 创建仓库: /create-gitea-repo my-project (将创建到 ai/my-project) - 查看配置: /gitea-config ``` ## Notes - 切换组织不会影响已创建的仓库和 Runner - 仅影响后续创建仓库时的默认 owner - 可以随时切换到其他组织 - 使用 `jq` 解析 JSON 响应 - macOS 和 Linux 的 `sed` 命令略有不同,需兼容处理