Files
opencode/command/gitea-switch-org.md
Voson 5a05d5ab53 chore: 重构 OpenCode 命令和技能文档体系
- 新增:统一的 git 命令文档(add/commit/push/pull 等)
- 新增:整合的 Gitea 技能文档(API、运行器、工作流等)
- 新增:工作流模板(Android、Go、Node.js 等)
- 移除:已弃用的旧命令脚本和发布脚本
- 改进:.gitignore 添加敏感文件保护规则
- 改进:AGENTS.md 完善了开发规范和示例

此次重组统一了命令和技能的文档结构,便于后续维护和扩展。
2026-01-13 00:27:21 +08:00

3.5 KiB
Raw Blame History

description
description
Switch default Gitea organization

gitea-switch-org

切换默认 Gitea 组织。

Features

  • 更新配置文件中的默认组织
  • 验证组织是否存在
  • 立即生效,无需重启

User Input Format

<organization_name>

Examples:

  • ai - Switch to 'ai' organization
  • my-team - Switch to 'my-team' organization

Steps

1. Check Configuration

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

new_org="$1"

if [ -z "$new_org" ]; then
  echo "用法: /gitea-switch-org <organization_name>"
  echo ""
  echo "示例:"
  echo "  /gitea-switch-org ai"
  echo "  /gitea-switch-org my-team"
  exit 1
fi

3. Validate Organization Exists

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

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

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 命令略有不同,需兼容处理