检查并修复项目
fastkit doctor 会检查生成的项目中是否存在新版 fastkit 模板已经修复的问题,例如 .env 被提交到了仓库、仍在使用示例密钥,或者 shell 脚本使用了 CRLF 换行符。在你要求时,它会修复其中可以安全修复的问题,并且从不打印密钥的值。升级 fastkit 永远不会改动已有的项目,因此升级之后,请在你的各个项目中运行 doctor。
运行检查
在项目文件夹中运行 fastkit doctor,或者把项目文件夹作为参数传给它。它会打印一张检查项表格;对于刚创建的项目,每个检查项要么通过,要么被跳过:
$ fastkit init --level layered --name shop --no-venv -y
...
$ cd shop
$ fastkit doctor
fastkit doctor
template fastapi-default, preset classic-layered, generated with fastkit 2.0.0
...
╭────────────── Success ───────────────╮
│ ✨ No problems found. │
╰──────────────────────────────────────╯
检查项不适用时会被跳过,例如在 fastapi-mcp 以外的项目中,MCP 相关的检查项都会被跳过。CLI 参考列出了每个检查项及其严重级别。
阅读检查结果
下面的项目被放进了一个 git 仓库,并且误把 .env 提交了上去(git add -f .env),同时 scripts/test.sh 被保存为 CRLF 换行符。doctor 会报告每个问题,列出涉及的文件和修复方法,然后打印检查项表格和汇总:
$ fastkit doctor
fastkit doctor
template fastapi-default, preset classic-layered, generated with fastkit 2.0.0
HIGH (1) ───────────────────────────────────────────────────────────────────────
[env-tracked] .env is committed to git
- .env is in the git index and is pushed with every commit.
How to fix:
Run `git rm --cached <file>` for each file listed and commit; the file stays
...
MEDIUM (1) ─────────────────────────────────────────────────────────────────────
[crlf-scripts] Shell scripts or Dockerfiles have CRLF line endings
- scripts/test.sh has CRLF line endings.
How to fix:
Convert the files to LF (the fix does) and add .gitattributes so Git keeps
them that way. With CRLF, `bash` fails with `$'\r': command not found` and
containers with `env: 'bash\r': No such file or directory`.
Fixable with `fastkit doctor --fix`.
...
╭──────────────────────────────── Warning ─────────────────────────────────╮
│ ⚠️ 2 problem(s) found (1 high, 1 medium). 1 can be fixed automatically: │
│ run `fastkit doctor --fix` to review and apply the changes. │
╰──────────────────────────────────────────────────────────────────────────╯
修复可以安全修复的问题
--fix 会列出它将要做的修改,并询问一次;--dry-run(或 --diff)只列出这些修改,不写入任何文件。修复完成后,doctor 会再检查一遍,并列出剩下的问题。仅提示的问题(例如已提交的 .env)永远不会被自动修改:
$ fastkit doctor --fix
...
Planned changes ────────────────────────────────────────────────────────────────
modify scripts/test.sh (convert scripts/test.sh from CRLF to LF line endings)
Apply these 1 file change(s)? [y/N]: y
updated scripts/test.sh
...
╭────────────── Warning ───────────────╮
│ ⚠️ 1 problem(s) found (1 high). │
╰──────────────────────────────────────╯
请手动完成修复步骤(git rm --cached .env、提交、轮换密钥),之后再运行 fastkit doctor,结尾就会显示 No problems found.
在 CI 中使用
没有终端时,--fix 需要加上 --yes 才会写入文件,或者加上 --dry-run 进行预览;否则它不做任何修改,并以退出状态码 2 退出。--format json 会为脚本打印一个 JSON 文档,其中各字段的说明请参阅 fastkit doctor --help。下面是那个提交了 .env 的项目的汇总部分:
$ fastkit doctor --format json
{
"schema_version": 1,
"fastkit_version": "2.0.0",
...
"summary": {
"problems": 1,
"fixable": 0,
...
退出状态码会告诉 CI 作业发生了什么:没有发现问题(或者所有问题都已修复)时为 0,仍有问题时为 1,doctor 无法运行时为 2。
没有 fastkit 元数据的项目
doctor 会读取 fastkit 写入 pyproject.toml 的 [tool.fastapi-fastkit] 表。对于在 fastkit 1.4.0 之前生成的项目,或者不是由 fastkit 生成的项目,它会以退出状态码 2 停止,并建议使用 --force。fastkit doctor --force 会检查这类项目,并跳过需要元数据的检查项。