name: "execute-python-script"
command: "/bin/bash"
args:
  - "-c"
  - |
    set -euo pipefail

    SCRIPT_CONTENT="$1"
    ENV_NAME="${2:-default}"
    ADDITIONAL_ARGS="${3:-}"

    BASE_DIR="${HOME}/.cyberstrike/venvs"

    detect_project_root() {
      if [ -n "${CYBERSTRIKE_ROOT:-}" ] && [ -d "${CYBERSTRIKE_ROOT}" ]; then
        printf '%s\n' "${CYBERSTRIKE_ROOT}"
        return
      fi
      if command -v git >/dev/null 2>&1; then
        local root_path
        if root_path=$(git rev-parse --show-toplevel 2>/dev/null); then
          printf '%s\n' "$root_path"
          return
        fi
      fi
      printf '%s\n' "$(pwd)"
    }

    resolve_env_dir() {
      local requested="$1"
      if [ -n "${VIRTUAL_ENV:-}" ] && { [ -z "$requested" ] || [ "$requested" = "default" ]; }; then
        printf '%s\n' "$VIRTUAL_ENV"
        return
      fi
      if [ -z "$requested" ] || [ "$requested" = "default" ]; then
        local root
        root="$(detect_project_root)"
        printf '%s/venv\n' "$root"
        return
      fi
      printf '%s/%s\n' "$BASE_DIR" "$requested"
    }

    ENV_DIR="$(resolve_env_dir "$ENV_NAME")"
    mkdir -p "$(dirname "$ENV_DIR")"
    if [ ! -d "$ENV_DIR" ]; then
      python3 -m venv "$ENV_DIR"
    fi

    # shellcheck disable=SC1090
    source "$ENV_DIR/bin/activate"

    if [ -n "$ADDITIONAL_ARGS" ]; then
      python3 $ADDITIONAL_ARGS -c "$SCRIPT_CONTENT"
    else
      python3 -c "$SCRIPT_CONTENT"
    fi
  - "_"
enabled: true
short_description: "在指定虚拟环境中编写并执行Python脚本"
description: |
  在虚拟环境中执行Python脚本。

  **主要功能：**
  - 执行Python脚本
  - 虚拟环境支持
  - 脚本内容执行

  **使用场景：**
  - 脚本执行
  - 自动化任务
  - 数据处理
parameters:
  - name: "script"
    type: "string"
    description: |
      要执行的Python脚本内容，支持多行。

      **示例：**
      ```
      for i in range(1, 10):
          print(i)
      ```
    required: true
    position: 0
    format: "positional"
  - name: "env_name"
    type: "string"
    description: "虚拟环境名称（默认default）"
    required: false
    default: "default"
    position: 1
    format: "positional"
  - name: "additional_args"
    type: "string"
    description: |
      额外的execute-python-script参数。用于传递未在参数列表中定义的execute-python-script选项。

      **示例值：**
      - 根据工具特性添加常用参数示例

      **注意事项：**
      - 多个参数用空格分隔
      - 确保参数格式正确，避免命令注入
      - 此参数会直接追加到命令末尾
    required: false
    format: "positional"
    default: ""
