AGENTS.md

AGENTS.md

本文档记录了 AI 助手在本项目中执行的关键任务、系统集成工作以及完整的功能说明。
仓库根目录的 AGENTS.md 是权威来源,本页为站点镜像。

项目概述

Spark 是一个 CLI 工具(module spark,二进制 spark),用于管理多个 Git 仓库、脚本、任务工作流,并附带实用系统工具。基于 Cobra(CLI)、Viper(配置)、PTerm(终端 UI),BDD 测试使用 Ginkgo/Gomega。

核心功能:

  1. 多仓库更新 — 批量更新多个 Git 仓库到最新版本(支持 SSH)
  2. 仓库克隆 — 通过 gh repo clone 克隆 GitHub 仓库(默认 SSH);batch-clone 支持 GitHub 组织/用户与 GitLab 群组/用户
  3. Git 用户配置 — spark git config 配置仓库的 Git 用户信息
  4. Gitcode 远程管理 — spark git gitcode 添加 Gitcode 远程地址
  5. 账号状态 — spark git update-org-status 将账号(组织/用户)仓库列表写入 README
  6. 仓库扫描 — spark git scan 扫描目录中的仓库并保存到 SQLite
  7. 仓库推送 — spark git push-all 批量提交推送所有更改
  8. Issue 创建 — spark git issues 从 Markdown/任务文件创建 GitHub Issue
  9. 脚本管理 — 从 ~/.spark.yaml 或 scripts/ 目录发现并执行脚本
  10. 系统工具 — spark magic 提供 DNS 缓存刷新、node_modules/.venv 清理、pip/npm/go 镜像源切换、Neovim/Ghostty 模板部署
  11. 文档管理 — spark docs init/spark docs site(docmd 站点初始化)
  12. 进程诊断 — spark witr(Why Is This Running),检查进程或端口为何在运行
  13. 项目 Hub — spark hub registry 管理 references 目录(scan / list / clone / clone-registry);spark hub collections 登记自己开发的 app(registry/apps.json)。不用 submodule

技术栈

  • 语言: Go 1.27+(见 go.mod)
  • CLI 框架: Cobra + Viper
  • 终端 UI: pterm + Bubble Tea + 自定义 TUI 组件
  • 测试框架: Ginkgo + Gomega(BDD 风格,internal/)+ 标准 testing(cmd/)
  • 构建系统: Makefile + Taskfile(跨平台:Windows / Linux / macOS)

项目结构

spark-cli/
├── main.go                  # 入口(调用 cmd.Execute())
├── cmd/
│   ├── root.go              # 根命令、全局 flag、配置加载与 .monolize.yaml 自动迁移
│   ├── version.go           # spark version(打印 version / commit / build date)
│   ├── witr.go              # 桥接到 internal/witr/app.Root()
│   ├── git/                 # Git 仓库管理命令
│   │   ├── git.go           # GitCmd 父命令
│   │   ├── clone.go         # spark git clone
│   │   ├── update.go        # spark git update(--ssh)
│   │   ├── gitcode.go       # spark git gitcode(--url)
│   │   ├── config.go        # spark git config(--username, --email)
│   │   ├── url.go           # spark git url
│   │   ├── batch_clone.go   # spark git batch-clone(GitHub + GitLab)
│   │   ├── issues.go        # spark git issues(-r, -f, -d, -l, --dry-run)
│   │   ├── update_org_status.go  # spark git update-org-status(账号类型自动识别)
│   │   ├── push_all.go      # spark git push-all
│   │   └── scan.go          # spark git scan(-d/--db, --skip-api)
│   ├── magic/               # 系统实用命令
│   │   ├── magic.go         # MagicCmd 父命令
│   │   ├── clean.go         # spark magic clean(-m node|python)
│   │   ├── copy_config.go   # spark magic copy-config(部署 nvim/ghostty 模板)
│   │   ├── flush_dns.go     # spark magic flush-dns
│   │   ├── pip.go           # spark magic pip {list,use,current}
│   │   ├── go.go            # spark magic go {list,use,current}
│   │   └── node.go          # spark magic node {list,use,current}
│   ├── script/              # 脚本管理
│   │   ├── script.go        # ScriptCmd 父命令
│   │   ├── list.go          # spark script list
│   │   └── run.go           # spark script run <name> [args...]
│   ├── hub/                 # registry(references)与 collections(自研 app)
│   │   ├── hub.go           # HubCmd
│   │   ├── scan.go          # spark hub registry scan
│   │   ├── list.go          # spark hub registry list
│   │   ├── clone.go         # spark hub registry clone
│   │   ├── clone_registry.go # spark hub registry clone-registry
│   │   └── register.go      # spark hub collections scan
│   └── docs/                # 文档管理
│       ├── docs.go          # DocsCmd 父命令
│       ├── init.go          # spark docs init(--root)
│       └── site.go          # spark docs site(--root)
├── internal/
│   ├── config/              # 配置加载
│   ├── git/                 # Git 操作封装(finder/updater/pusher + scanner 子包)
│   │   └── scanner/         # git scan 使用的扫描与 SQLite 持久化
│   ├── github/              # GitHub API(org / markdown issue)
│   ├── gitlab/              # GitLab API(batch-clone:URL 解析、token 来源、嵌套群组)
│   ├── registry/            # hub registry 的扫描/读写/merge
│   ├── collection/          # hub collections 的 apps.json 模型、扫描与 merge
│   ├── script/              # 脚本发现(配置 + scripts/ 目录)与执行
│   ├── templates/           # 嵌入的 dotfiles(nvim + ghostty)
│   ├── testreport/          # Ginkgo JSON 报告 → 自包含 HTML 渲染器(含 BDD 测试)
│   └── witr/                # Why-Is-This-Running 进程诊断引擎
├── tools/
│   └── testreport/          # test-report CLI:读取 ginkgo --json-report 输出,生成 HTML
├── pkg/
│   └── witr/model/          # witr 的共享数据模型
├── docs/                    # docmd 文档站点(中英双语)
│   ├── zh/                  # 中文(默认 locale,渲染于根 URL)
│   │   ├── usage/           # 每个命令的使用说明
│   │   ├── spec/            # 规约文档
│   │   ├── features/        # 功能说明
│   │   ├── analysis/        # 架构分析与 RFC
│   │   ├── quick-start/     # 快速开始
│   │   ├── Agents.md        # AGENTS.md 的站点镜像
│   │   └── navigation.json  # 侧边栏导航
│   ├── en/                  # 英文镜像(渲染于 /en/),层级与 zh/ 完全平行
│   └── ...
├── scripts/                 # 默认脚本目录(spark script 使用)+ 安装/校验脚本
├── .vscode/                 # VS Code 配置(tasks/launch/settings)
├── Makefile                 # 构建脚本(build/build-linux/build-darwin/test/lint/clean)
├── Taskfile.yml             # Task 版等价命令(build/install/install-binary/verify-install)
├── docmd.config.js          # docmd 站点配置(i18n.default: zh)
├── go.mod / go.sum
└── package.json             # docmd 依赖

命令层级

spark
├── version                 (显示 spark version / commit / build date)
├── git
│   ├── clone                (<url-or-slug> [directory] [-- <git-args>])
│   ├── update               (-p, --ssh)
│   ├── gitcode              (-p, --url)
│   ├── config               ([repo-path], --username, --email)
│   ├── url                  ([repo-path])
│   ├── batch-clone          (<account-name-or-url>, --ssh, --include, --exclude,
│   │                         --include-forks, -o, --token)
│   ├── issues               (-r, -f, -d, -l, --dry-run)
│   ├── update-org-status    (<account-name-or-url>, --dry-run, -o, --update-dot-github,
│   │                         --section, --skip-push)
│   ├── push-all             (-p)
│   └── scan                 ([folder-path], -d, --db, --skip-api)
├── hub
│   ├── registry
│   │   ├── scan                 ([folder-name], --json, --flatten)
│   │   ├── list                 (-f, --file)
│   │   ├── clone                (-r, --repo, -f, --file, --depth)
│   │   └── clone-registry       (<registry-file>, --out, --dry-run, --depth)
│   └── collections
│       └── scan                 ([path], -o/--output, --merge, --skip-api, --deep, --category)
├── script
│   ├── list
│   └── run
├── magic
│   ├── clean                (-m, --mode)
│   ├── copy-config          ([<user@host:path>])
│   ├── flush-dns
│   ├── pip {list,use,current}
│   ├── go {list,use,current}
│   └── node {list,use,current}
├── docs
│   └── site                 (--root)
└── witr                     (--pid, --port/-o, --file/-f, --container/-c, --tree/-t,
                              --env, --json, --short/-s, --warnings,
                              --verbose, --exact/-x, --no-color)

自动化任务记录

1. BDD 测试集成 (2026-02-26)

  • 任务: 为 internal 包添加 BDD 风格的单元测试。
  • 工具: 引入了 Ginkgo 和 Gomega 框架。
  • 覆盖范围: internal/config、internal/git(含 finder/updater)、internal/script、internal/github(issue/markdown_issue/org)、internal/gitlab、internal/witr/output。
  • 验证: 所有测试已通过 make test-bdd 验证。

2. 跨平台 Makefile 构建 (2026-02-26)

  • 任务: 创建支持 Windows、Linux、macOS 的构建系统。
  • 功能:
    • 自动 OS 检测(ifeq ($(OS),Windows_NT))。
    • 交叉编译支持(build-linux、build-darwin)。
    • 统一的清理与测试接口。
    • 构建产物自动安装到 ~/.local/bin/spark。

3. VS Code 环境标准化 (2026-02-26)

  • 任务: 优化 .vscode 目录配置。
  • 成果:
    • tasks.json: 与 Makefile 深度绑定。
    • launch.json: 提供标准化的调试模板。
    • settings.json: 统一 Go 语言开发规范。

4. GitLab batch-clone 支持

  • 增加 internal/gitlab 子包;
  • spark git batch-clone <git-host>/<group>/<subgroup> 同时支持自托管 GitLab;
  • 支持 --token / GITLAB_TOKEN / GITLAB_PRIVATE_TOKEN 环境变量认证。
  • 详细命令清单见下文「CLI 命令完整参考」。

5. GitLab 嵌套群组与凭证作用域 (2026-09-16)

  • 嵌套群组:detectAccountType / apiGetGroupProjects / apiGetUserProjects 统一用
    escapePath(url.PathEscape)编码路径,include_subgroups=true 递归拉取任意层级子群组;
    ProjectRelativePath 按命名空间相对路径落盘,避免子群组同名项目互相覆盖。
  • 无 scheme 输入:IsGitLabURL 识别 gitlab.com/group/sub,ParseGitLabURL 自动补 https://。
  • 凭证来源可追溯:新增 TokenSourceFlag / TokenSourceConfig / TokenSourceEnv / TokenSourceEnvAlt
    常量与 tokenOrigin(),cmd/git.resolveGitLabToken 按
    --token > gitlab.token > GITLAB_TOKEN > GITLAB_PRIVATE_TOKEN 解析,
    启动打印 Using token from: ...,报错中指明被拒绝凭证的来源。
  • 凭证 host 作用域:新增 gitlab.host 配置,将自动发现的凭证限定到该实例,
    不匹配时打印 Ignoring ...: it is scoped to ... 并跳过;internal/gitlab.ExtractHost
    负责把 URL / 裸域名 / host:port 归一化为小写 host 用于比较。
  • 错误分类:私有实例未认证返回 404 而非 401,detectAccountType 据此区分
    「凭证被拒绝」/「未提供凭证」/「路径不存在」三种提示。
  • 测试:internal/gitlab/gitlab_test.go(含 httptest 模拟嵌套群组与 401/404)与
    cmd/git/batch_clone_test.go(resolveGitLabToken 的 host 作用域表驱动用例)。

6. repo registry 与 magic/docs 命令

  • spark hub registry(cmd/hub/ + internal/registry)管理 references 目录:scan / list / clone / clone-registry。spark hub collections(internal/collection)登记自研 app。不再提供 spark repo、spark skill、spark git submodule。
  • spark magic(cmd/magic/)提供 clean / copy-config / flush-dns / pip|go|node {list,use,current}。
  • spark docs(cmd/docs/)提供 init / site,用于 docmd 站点脚手架。
  • spark version(cmd/version.go)打印 version / commit / build date。

7. Windows 测试与构建根治 (2026-10-20)

  • 构建稳定:go.sum 从 .gitignore 移除并提交(此前 fresh clone 必然构建失败);
    task build 移除 sources/generates 指纹(此前二次运行会跳过安装)。
  • 测试根治(均为 Windows 平台假设错误,非删除用例):
    • TestAddNestedRepos:git update-index --cacheinfo 拒绝反斜杠路径——
      AddExistingRepoAsSubmodule 与 isSubmoduleGitlink 现统一 filepath.ToSlash,
      .gitmodules 始终写正斜杠路径。
    • TestIntegration_ReadProcessNonexistent:PID 0 在 Windows 是可读的 System Idle
      Process——改用 math.MaxInt32(任何平台都不存在)。
    • TestGetCmdlineSelf:ginkgo CLI 编译的测试二进制无 .exe 后缀——同时接受 .test。
  • HTML 测试报告:make test-report / task test-report 全量跑 Ginkgo 并生成
    reports/test-report.html(套件/用例状态、时长、失败详情、状态过滤 + 搜索)。
    Ginkgo 在 Windows 上必须配 --output-dir(其内部 path.Dir bug),由 target 预建目录。

8. pnpm 全局配置、文档补全与全局 innate-fe-cli (2026-10-20)

  • pnpm 全局地址指到 D 盘:store-dir=D:\.pnpm-store、global-dir=D:\pnpm\global、
    global-bin-dir=D:\pnpm\bin,PNPM_HOME=D:\pnpm + 用户 PATH 首位 D:\pnpm\bin
    (用 [Environment]::SetEnvironmentVariable 写入,勿用 setx,1024 字符会截断 PATH)。
    pnpm 升级至 12.14.0。
  • Windows 构建链路:task build 产出 spark.exe 并安装 ~/.local/bin(spark.exe +
    裸名 spark 双份,sha256 校验);pnpm install && pnpm build 产出文档站;修复
    Taskfile.yml 交叉编译 env(per-command env 被忽略导致 build:all 全是 PE)。
  • 文档:README 增补 Quick Start(spark magic flush-dns)与 Test Report 截图
    (docs/assets/test-report.png);新增 docs/{zh,en}/usage/how-to-build.md
    (Go / Node / Bun 构建 + pnpm 环境)与 docs/{zh,en}/quick-start/golang-cli-101.md
    (Golang CLI 入门第一课,含课程 Prompt 与 Skill 模板)。
  • 全局 innate-fe-cli:bun build --compile 打出单二进制后以 NTFS 硬链接装入
    ~/.local/bin(innate-fe / innate-fe-cli 各含 .exe,4 名共享 1 份磁盘数据)。

9. 从零构建教程与 SKILL 文档(2026-10-21,issue #24)

  • 新增手把手教程(与 golang-cli-101 的概念课互补,代码可照抄执行):
    • docs/{zh,en}/quick-start/golang-project-from-scratch.md — Go CLI 项目从零创建:
      go mod init 生成 go.mod 的过程逐行解释、CLI 三件套(Cobra/Viper/PTerm)依赖清单与安装、
      cmd/internal/pkg 标准分层职责、第一个 Cobra 命令、Taskfile 逐段解读 + 最小模板、
      附 SKILL golang-project-from-scratch。
    • docs/{zh,en}/quick-start/docs-site-from-scratch.md — docmd 文档站从零创建:
      npm init 与 package.json 字段逐个解释(含 Go↔Node 对照表)、docmd 安装、
      docmd.config.js 配置讲解、navigation.json 与双语 i18n、GitHub Actions 发布
      GitHub Pages(.github/workflows/docs.yml 全文 + Settings → Pages → Source: GitHub Actions)、
      附 SKILL docs-site-from-scratch。
  • 导航:docs/{zh,en}/navigation.json 快速开始组新增两页;SKILL 以附录代码块形式内嵌
    (沿用 golang-cli-101 附录 B 的安装约定,存入 .claude/skills/<name>/SKILL.md 生效)。

CLI 命令完整参考

全局选项

由 cmd/root.go 通过 rootCmd.PersistentFlags() 定义:

选项 说明
--config 配置文件路径(默认:$HOME/.spark.yaml)
-p, --path 仓库扫描目录路径(StringSlice,默认 ["."],绑定到 viper key repo-path)

配置初始化流程(initConfig + migrateOldConfig):

  1. 若 --config 被指定则使用该文件,否则默认 $HOME/.spark.yaml。
  2. 启动时检测旧配置 ~/.monolize.yaml:存在且 ~/.spark.yaml 不存在则自动重命名迁移。
  3. viper.AutomaticEnv() 启用环境变量覆盖。

spark git — Git 仓库管理

父命令 GitCmd 在 cmd/git/git.go 中定义,包含以下子命令(在 init() 函数中按文件注册):

spark git clone <url-or-slug>    # 通过 gh repo clone(默认 SSH)
spark git update [-p ...]        # 扫描并更新所有仓库(--ssh 强制走 SSH)
spark git gitcode [-p ...]       # 添加 gitcode 远程
spark git config [path]          # 配置 Git 用户信息
spark git url [path]             # 打印当前仓库的远程 URL
spark git batch-clone <account>  # 批量克隆 GitHub/GitLab 账号下仓库
spark git issues                 # 从 Markdown/任务文件创建 GitHub Issue
spark git update-org-status <account> # 生成账号仓库列表并写入 README
spark git push-all [-p ...]      # 扫描并 commit/push 所有仓库
spark git scan [folder-path]     # 扫描仓库并写入 SQLite

spark git update

扫描 repo-path 下所有仓库并更新到最新版本。

spark git update -p /path/to/repos
spark git update -p ~/workspace -p ~/projects
spark git update --ssh                                  # 强制 HTTPS→SSH
选项 说明
-p, --path 扫描目录(可多次指定,覆盖 repo-path)
--ssh 强制将 HTTPS GitHub URL 重写为 SSH 后更新

spark git clone

通过 gh repo clone 克隆 GitHub 仓库,默认走 SSH。

spark git clone https://github.com/Nutlope/pdf-to-interactive-lesson.git
spark git clone Nutlope/pdf-to-interactive-lesson
spark git clone https://github.com/owner/repo.git my-dir -- --branch main --depth 1

支持的输入格式:

  • https://github.com/owner/repo.git
  • git@github.com:owner/repo.git
  • github.com/owner/repo
  • owner/repo

-- 之后的额外参数会透传给 git clone。

spark git gitcode

为 GitHub 仓库批量添加 gitcode 远程(自动将 github.com → gitcode.com)。

spark git gitcode -p /path/to/repos
spark git gitcode --url https://custom.gitcode.url
选项 说明
--url 自定义 Gitcode 远程 URL(默认自动转换)

spark git config [path]

读取默认 git.username/git.email(来自 ~/.spark.yaml),写入本地仓库 git config。

spark git config                                # 查看当前配置
spark git config --username foo --email bar     # 显式覆盖
选项 说明
--username Git 用户名(默认:git.username)
--email Git 邮箱(默认:git.email)

配置优先级:命令行 → ~/.spark.yaml → 提示用户配置。

spark git url [path]

读取并打印当前仓库的 origin URL。

spark git batch-clone

根据输入自动识别 GitHub 或 GitLab,克隆账号下所有仓库。

# GitHub
spark git batch-clone variableway
spark git batch-clone https://github.com/variableway

# GitLab(自托管或 gitlab.com,任意层级嵌套群组均递归支持)
spark git batch-clone https://gitlab.example.com/myorg/mygroup
spark git batch-clone https://gitlab.example.com/myorg/mygroup/subgroup
spark git batch-clone https://gitlab.com/gitlab-com/gl-infra --token <token>
spark git batch-clone gitlab.com/gitlab-com/gl-infra         # 可省略 https://

# 通用
spark git batch-clone variableway --ssh
spark git batch-clone variableway -o ./repos
spark git batch-clone <url> --token <token>      # 也支持 GITLAB_TOKEN / GITLAB_PRIVATE_TOKEN 环境变量

GitLab 实现要点:

  • gitlab.IsGitLabURL 支持无 scheme 输入(gitlab.com/group/sub),只要首个路径段是含 . 的域名即判定为 GitLab;
    gitlab.ParseGitLabURL 自动补 https:// 前缀,因此 spark git batch-clone gitlab.com/gitlab-com/gl-infra 合法
  • 群组路径在调用 API v4 时必须 URL 编码(group/sub/project → group%2Fsub%2Fproject),
    否则嵌套群组会 404,见 internal/gitlab.escapePath(detectAccountType 与两个 projects 拉取函数共用)
  • 解析顺序:有 --token 时优先走 REST API(可给出精确错误),否则回退 glab,最后再试无凭证 API
  • 克隆落盘路径按命名空间相对路径展开(internal/gitlab.ProjectRelativePath),
    如 archiver、observability/tenant-observability/argocd-tenant-plugin,避免子群组同名项目互相覆盖
  • Token 优先级:--token > ~/.spark.yaml 的 gitlab.token(viper.BindPFlag)> GITLAB_TOKEN > GITLAB_PRIVATE_TOKEN;
    来源标签由 cmd/git.resolveGitLabToken 计算,启动时打印 Using token from: ...,
    报错中也带上该来源(配置文件里残留的失效 token 会盖住环境变量,需据此排查)
  • gitlab.host(可选)把自动发现的凭证(配置文件 + 环境变量)限定到该实例:
    host 不匹配时跳过该凭证并提示 Ignoring ...: it is scoped to ...,
    避免自托管实例的 token 被发往 gitlab.com 导致公开群组也鉴权失败;
    --token 属显式指定不受限制,未配置 host 时保持旧行为(见 internal/gitlab.ExtractHost)
  • 私有实例/私有群组未认证时返回 404 而非 401,因此错误信息会区分
    “未提供凭证” / “凭证无效或无权访问” / “路径不存在”三种情况,避免把缺 token 误报成 not found
选项 说明
--ssh 使用 SSH URL(GitHub git@github.com:...,GitLab git@<host>:...)
--include 仅克隆名称含任一模式(逗号分隔)的仓库
--exclude 跳过名称含任一模式(逗号分隔)的仓库
--include-forks 包含 fork 仓库(GitHub 通过 repo.Fork 判定;GitLab 通过 forked_from 字段)
-o, --output 输出目录(默认 .)
--token GitLab 私有 Token(显式指定,不受 gitlab.host 限制;也可由 GITLAB_TOKEN / GITLAB_PRIVATE_TOKEN 环境变量或 ~/.spark.yaml 的 gitlab.token 提供)

Token 来源常量定义在 internal/gitlab.TokenSourceFlag / TokenSourceConfig / TokenSourceEnv / TokenSourceEnvAlt,
由 cmd/git.resolveGitLabToken 按上述优先级返回并打印 Using token from: ...。

spark git issues

两种模式:从目录中每个 Markdown 文件创建 Issue,或从任务文件(## Task <id>...)创建 Issue。

# 目录模式
spark git issues -d ./docs -r owner/repo

# 任务模式
spark git issues -f ./issues/task-bug-fix.md -r owner/repo

# 自动从当前仓库解析 owner/repo(需 GitHub remote)
spark git issues -d ./docs --dry-run

# 加标签
spark git issues -d ./docs -l bug,enhancement
选项 说明
-r, --repo 目标仓库(owner/repo),未指定时从当前 git remote 解析
-d, --dir Markdown 目录(目录模式)
-f, --file 任务文件(任务模式)
-l, --labels Issue 标签(逗号分隔)
--dry-run 仅预览,不创建

-d 与 -f 互斥,必须二选一。

spark git update-org-status

获取 GitHub 账号(组织或用户)下所有仓库,按 star 数排序,写入一个 section 到 README。

spark git update-org-status qdriven                       # 写入 .github/README.md
spark git update-org-status qdriven --update-dot-github   # 直接推送到 profile 仓库
spark git update-org-status qdriven --dry-run
spark git update-org-status qdriven -o ./docs/README.md
spark git update-org-status qdriven --section "Projects"
spark git update-org-status qdriven --skip-push
选项 说明
--dry-run 打印内容,不写入文件
-o, --output 本地模式输出路径(默认:.github/README.md)
--update-dot-github 直接克隆该账号的 profile 仓库并修改
--section 要更新/插入的 section 名(默认:Project List)
--skip-push 跳过 git commit 与 git push

特性:

  • 账号类型自动识别:组织 → profile 仓库为 <org>/.github(README.md + profile/README.md);
    用户 → profile 仓库为 <user>/<user>(仓库根目录 README.md)。因此个人 profile 仓库
    (例如 qdriven/qdriven)用同一条命令即可刷新
  • 仓库列表分页拉取(每页 100 条),不受单页上限影响
  • 设置 GITHUB_TOKEN 可提高 API 速率限制(匿名 60 次/小时)
  • fork 仓库不写入表格,也不计入统计
  • 默认写入本地 .github/README.md;若在 .github 目录下,同时写入 profile/README.md
  • 使用 --update-dot-github 时会克隆到 $TMPDIR/github-repo-<account>-<ts>,写入后再 commit/push

spark git push-all

扫描 repo-path 下所有仓库,对 GitHub 仓库且有更改的执行 git add -A → git commit → git push,冲突时提示。

spark git scan [folder-path]

递归扫描目录中的仓库,可选调用 GitHub/GitLab API 拉取元数据,写入 SQLite。

spark git scan                                # 默认扫描当前目录
spark git scan ~/workspace
spark git scan . --skip-api
spark git scan . --db ~/.innate/feeds.db
选项 说明
[folder-path] 要扫描的目录(位置参数,默认 .)
-d, --db SQLite 数据库路径(默认:~/.innate/feeds.db,可由 git.scanner.db 配置或 ~-prefix 扩展)
--skip-api 跳过 API,仅扫描本地仓库

~/.spark.yaml 中可通过 git.scanner.db 设置默认数据库路径。设置 GITHUB_TOKEN 可提升 GitHub API 速率限制。


spark hub — 项目 Hub

spark hub 有两条互不替代的路径:

  • registry:管理 references 目录。scan 写入 registry_<folder>.yaml,list / clone / clone-registry 按这份 registry 查看和克隆。不用 submodule。
  • collections:登记自己开发的 app。scan 写入 innate-apps collections 目录(registry/apps.json)。

父命令 HubCmd 在 cmd/hub/hub.go。registry 读写在 internal/registry,collections 在 internal/collection。

spark hub registry scan [folder-name]                 # 扫描 references 目录,写入 registry_<folder>.yaml
spark hub registry scan [folder-name] --json          # 同时写出 registry_<folder>.json
spark hub registry list -f registry_<folder>.yaml
spark hub registry clone -f registry_<folder>.yaml
spark hub registry clone -r <name> -f registry_<folder>.yaml
spark hub registry clone-registry <registry-file> [--out <folder>] [--dry-run] [--depth <n>]
spark hub collections scan [path] -o registry/apps.json --merge registry/apps.json

registry 文件头是 # Synced by spark hub registry scan。三分层目录写成 version 2(<分类>/<子分类>/others);没有一级分类目录时回退扁平 projects:。重复扫描按 path、其次按 URL 合并,保留 name/desc,本地缺失的旧条目原样保留。根目录上的仓库打印 WARNING 且不入册。

spark hub registry scan [folder-name]

选项 说明
--json 额外写出格式化 registry_<folder>.json
--flatten 仅旧两层布局:扫描前把超过两层的仓库上移。三分层目录禁用

spark hub registry clone

目标目录名由 registry_<folder>.yaml 推导为 <folder>。已存在的目录 SKIP。按 path 从浅到深执行 git clone。

选项 说明
-r, --repo 只克隆指定名称
-f, --file registry 文件(必填)
--depth 浅克隆深度,默认 0(完整历史)

spark hub registry clone-registry

只克隆本地缺失的条目。registry 文件是位置参数。目标目录默认由文件位置和名称推导。

选项 说明
--out 目标目录
--dry-run 只打印计划
--depth 浅克隆深度,默认 0

spark hub registry list

每行 name<TAB>repo<TAB>path。分类格式先打印 # <分类> / <子分类> (n)。

选项 说明
-f, --file registry 文件(必填)

spark hub collections scan [path]

扫描自己开发的 app,默认只扫顶层。--merge 保留 title/category/status/url/description,刷新 repo/local_path/visibility/language,未扫到的旧条目原样保留。

选项 说明
-o, --output 输出文件(默认 stdout;只给 --merge 时写回该文件)
--merge 与已有 collections 文件合并
--skip-api 跳过 GitHub API
--deep 包含嵌套仓库
--category 新条目分类(base/app/reference)

设置 GITHUB_TOKEN 可提升 API 速率限制。


spark script — 脚本管理

spark script list
spark script run <script-name> [args...]

spark script list

列出所有可用脚本,按来源分组(~/.spark.yaml 中的 spark.scripts 与 scripts/ 目录)。

spark script run

按以下顺序搜索脚本:

  1. ~/.spark.yaml 中的 spark.scripts
  2. 当前目录(可通过 --scripts-dir 或 spark.scripts_dir 配置,默认为 scripts/)下的脚本文件

支持的脚本类型(根据扩展名识别):.sh .bash .zsh .py .rb .pl .ps1 .bat .cmd

仓库内置脚本示例:

  • copy-template.sh
  • list-dirs.sh

spark magic — 系统工具

spark magic clean                  # 清理 node_modules 和 .venv
spark magic copy-config            # 部署内置 nvim + ghostty 模板
spark magic flush-dns              # 刷新 DNS(macOS/Windows/Linux)
spark magic pip {list,use,current}
spark magic go {list,use,current}
spark magic node {list,use,current}

spark magic clean

递归清理 repo-path(或 .)下所有 node_modules 与 .venv 目录;遇到 .git 子目录跳过递归。

spark magic clean                  # 默认两者都清理
spark magic clean -m node          # 仅 node_modules
spark magic clean -m python        # 仅 .venv
选项 说明
-m, --mode node 或 python(默认两者都清理)

spark magic copy-config [<user@host:path>]

将编译时通过 embed.FS 打包进二进制的 nvim 和 ghostty 模板部署到目标位置。优先使用 rsync,缺失时回退到 cp。

spark magic copy-config                                # 部署到本机 ~/.config/{nvim,ghostty}
spark magic copy-config user@192.168.1.100:~/          # 通过 SSH 部署到远端 PC
spark magic copy-config /mnt/usb/backup/               # 部署到自定义本地路径

来源模板位于 internal/templates/dotfiles/,构建时通过 //go:embed 嵌入。

spark magic flush-dns

按操作系统自动选择刷新命令:

  • macOS:sudo dscacheutil -flushcache + sudo killall -HUP mDNSResponder
  • Windows:ipconfig /flushdns
  • Linux:依次尝试 systemctl restart systemd-resolved、service nscd restart、service dnsmasq restart、rndc flush

spark magic pip {list,use,current}

切换 pip 镜像源,支持:default / tsinghua / aliyun / douban / ustc / tencent。配置写入 ~/.pip/pip.conf 的 [global].index-url,并设置 trusted-host。

spark magic go {list,use,current}

切换 Go module proxy,支持:default / aliyun / tsinghua / goproxy / ustc / nju。通过 go env -w GOPROXY=... 设置。

spark magic node {list,use,current}

切换 npm registry,支持:default / taobao / aliyun / tencent / huawei / ustc。通过 npm config set registry <url> 设置。


spark docs — 文档管理

spark docs init                    # 创建 docs 目录结构
spark docs site                    # 初始化 docmd 站点配置
选项(共享) 说明
--root 项目根目录(默认 .)

spark docs init

创建标准结构(跳过已存在的目录/文件):

docs/
├── analysis/
├── features/
├── quick-start/
├── spec/
├── tips/
├── usage/
├── index.md
└── README.md

spark docs site

从 git remote get-url origin 自动检测项目标题与 GitHub Pages URL,写入 docmd.config.js;若 docmd 未安装则通过 npm install -g @docmd/core 全局安装;缺 package.json 时通过 npm init -y 初始化;执行 npm install。


spark witr — 进程诊断

进程为何在运行的诊断工具,来自 internal/witr/app:

spark witr nginx
spark witr --pid 1234
spark witr --port 8080
spark witr --file /var/lib/dpkg/lock
spark witr --container redis
spark witr nginx --tree
spark witr nginx --env
spark witr nginx --json
选项 说明
--pid 按 PID 查找(可多次)
--port / -o 按端口查找(可多次)
--file / -f 按文件查找(可多次)
--container / -c 按容器名查找(可多次)
--tree / -t 显示进程祖先树
--env 显示进程环境变量
--json JSON 输出
--short / -s 单行简短输出
--warnings 仅显示可疑的环境/参数/父进程
--verbose 扩展信息(内存、I/O、fd)
--exact / -x 精确匹配
--no-color 禁用颜色

子包:launchd、output、pipeline、proc、source、target、tools、tui、version;模型在 pkg/witr/model。


构建与测试

构建命令

注意:go.sum 必须提交到仓库(不得加入 .gitignore),否则全新 clone 后 go build 会因
“missing go.sum entry” 失败,导致 Windows/macOS/Linux 构建都不稳定。

make build          # 编译到 bin/ + 打 version/commit/date ldflags + 安装到 ~/.local/bin/spark
make build-linux    # 交叉编译 Linux amd64 (bin/spark_linux)
make build-darwin   # 交叉编译 macOS amd64 (bin/spark_darwin)
make clean          # 移除二进制与构建产物
make install        # 同 build
make install-only   # 仅复制已有二进制到 ~/.local/bin(不动 source)
make verify-install # 对比 ~/.local/bin/spark 与源 spark.exe 的 sha256/大小/mtime

task (Taskfile) 的等价命令:

task build          # 等价 make build(产物在 bin/;Windows 同步 spark.exe + spark 到 ~/.local/bin)
task install        # 等价 task build
task install-binary # 仅复制已有 spark.exe 到 ~/.local/bin(不动 source)
task verify-install # 等价 make verify-install
task build:windows        # 交叉编译 Windows amd64 (bin/spark_windows.exe)
task build:windows:arm64  # 交叉编译 Windows arm64 (bin/spark_windows_arm64.exe)
task build:all            # 全平台交叉编译(产物全部进 bin/,clean 删除 bin/)

Windows 安装原理:~/.local/bin 下同时存在 spark.exe 与去后缀名 spark,因为 bash 优先命中后者;install 步骤原子刷新两个文件,并立即打印 sha256 对比避免 stale 影子。

自检:

spark version       # -> spark v0.3.3 / commit xxx / build date ...
spark --version     # -> spark version v0.3.3

文档站构建(Node 体系)

文档站是独立的 Node 项目(package.json + pnpm + docmd),与 Go 二进制构建并行:

pnpm install        # 依赖硬链接自 pnpm store(Windows 可把 store 指到 D:\.pnpm-store)
pnpm build          # docmd build -> site/(zh 在根,en 在 /en/)
pnpm dev            # 本地预览

完整构建指南(Go 二进制 / 文档站 / Bun 单二进制 / Windows pnpm 全局地址)见
usage/how-to-build.md。
pnpm 全局地址(省 C 盘):store-dir=D:\.pnpm-store、global-dir=D:\pnpm\global、
global-bin-dir=D:\pnpm\bin、PNPM_HOME=D:\pnpm。
另:Bun 的 bun build --compile 也可像 Go 一样从 TS 产出单二进制(如
innate-fe-cli),详见 how-to-build 第三节。

测试命令

make test           # go test ./... -v
make test-bdd       # ginkgo -v ./internal/...
make test-report    # ginkgo 全量 + 生成 HTML 报告 reports/test-report.html
make lint           # go vet ./...

test-report 原理:Ginkgo CLI 以 --output-dir=reports --json-report=ginkgo-report.json
跑 ./...(注意必须先用 --output-dir 预建目录——Ginkgo 内部用 path.Dir 而非
filepath.Dir 创建目录,在 Windows 上直接写 reports/xxx.json 会因目录不存在而失败),
再由 tools/testreport 把 JSON 渲染为自包含 HTML(汇总卡片、套件/用例表格、
失败详情、按状态过滤 + 文本搜索)。渲染器在 internal/testreport(含 BDD 测试)。

运行单个测试:

go test ./internal/git/... -v -run TestUpdateRepository

配置文件

配置文件位于 ~/.spark.yaml,从旧版 ~/.monolize.yaml 自动迁移。完整示例见 .spark.yaml.example:

repo-path:
  - ~/workspace
  - ~/projects
  - ./repos

git:
  username: your-name              # spark git config 默认值
  email: your-email@example.com
  scanner:
    db: ~/.innate/feeds.db         # spark git scan 默认 SQLite 路径

gitlab:
  host: gitlab.example.com         # 可选:把自动发现的凭证限定到该实例(不配则发给任何实例)
  token: glpat-xxxx                # spark git batch-clone 的 GitLab Token(= --token = GITLAB_TOKEN)
                                   # 需 read_api 权限,克隆私有仓库还需 read_repository
                                   # 优先级高于环境变量,残留的失效 token 会盖住 GITLAB_TOKEN


default_branch: main
auto_commit: true

配置键与实际读取位置(viper.GetString / viper.GetStringSlice):

配置键 读取处
repo-path(全局 -p, --path) cmd/git/update.go、push_all.go、magic/clean.go、gitcode.go
git.username / git.email cmd/git/config.go
git.scanner.db(--db) cmd/git/scan.go
gitlab.host / gitlab.token(--token) cmd/git/batch_clone.go
spark.scripts_dir cmd/script/list.go、cmd/script/run.go

GitLab Token 的环境变量为何用 os.Getenv 而非 viper.AutomaticEnv():viper 会把 gitlab.token
映射为 GITLAB.TOKEN,而 shell 无法定义含 . 的变量名,因此环境变量一律走显式 os.Getenv。

配置键命名转换:~/.spark.yaml 中使用 snake_case,通过 viper.BindPFlag 与持久 flag 关联。结构体 tag 中习惯仍使用 camelCase。脚本相关:

spark:
  scripts_dir: scripts            # spark script run 搜索目录
  scripts:
    - name: hello
      content: |
        #!/bin/bash
        echo "Hello, World!"
    - name: deploy
      content: |
        #!/bin/bash
        echo "Deploying to $1 environment..."

配置键优先级:--config flag → $HOME/.spark.yaml → 环境变量(viper.AutomaticEnv)。


助手指令参考

本项目保持高内聚、低耦合的 Go 代码风格。在进行后续开发时,请务必:

  1. 代码风格

    • 遵循 Go 标准代码规范
    • 不添加注释(除非明确要求)
    • 使用现有的库和工具模式(Cobra + Viper + PTerm + Ginkgo/Gomega)
  2. 测试要求

    • 新功能必须添加 BDD 风格测试
    • 测试文件以 _test.go 结尾,测试套件(*_suite_test.go)注册 Ginkgo runner
    • 测试覆盖:config / git(含 scanner)/ script / github / gitlab / witr/output
    • cmd/ 下的纯函数(如 resolveGitLabToken、parseRepoSlug)用标准 testing 直接覆盖即可
  3. 构建一致性

    • 优先更新 Makefile 以保持构建一致性
    • 确保 .vscode 配置的通用性
    • 提交前运行 make lint 和 make test
  4. 文档更新

    • 新增/修改命令时同步更新 docs/zh/usage/ 与 docs/en/usage/ 对应文档与本 AGENTS.md
    • docs/zh/Agents.md、docs/en/Agents.md 是站点内的镜像,同样需要跟进
    • 保持 AGENTS.md 命令层级与实际 cmd/ / internal/ 实现一致
    • 文档主要为中文 UI;命令行/代码标识符保持英文
    • 文档站点 docs/ 已支持中英双语(docmd.config.js 中 i18n.default: 'zh'):
      中文为默认 locale 渲染于根 URL;英文镜像在 /en/
      新增/修改文档需同时更新 docs/zh/... 与 docs/en/...,并保持
      docs/zh/navigation.json / docs/en/navigation.json 与 i18n locales 配置一致
      docmd 在非默认 locale 缺失页面时会自动 fallback 到默认 locale,
      翻译完成前可暂时依赖该行为