Files
easygoadmin-wiki/EasyGoAdmin_GoFrame_EleVue/zh/generator/cli.md
T
2026-10-20 21:19:44 +08:00

69 lines
2.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 10.2 CLI 命令行使用
:::tip 概述
CLI 入口 cmd/generator:`go run ./cmd/generator -tables "表名|注释"`。支持预览(-preview)与批量统计(-batch),读取同一份 config.yaml,产出与 HTTP 入口完全一致。
:::
## 命令格式
```sh
# 一键生成(文件 + 菜单 + 字典 + 路由,全部幂等)
go run ./cmd/generator -tables "goframe_example|案例演示表"
# 多表(逗号分隔)
go run ./cmd/generator -tables "goframe_example|案例演示表,goframe_file_template|文件模板"
# 仅预览(不落盘,打印文件清单)
go run ./cmd/generator -tables "goframe_example|案例演示表" -preview
# 批量统计(逐表输出生成/跳过/覆盖/失败计数)
go run ./cmd/generator -tables "goframe_example|案例演示表" -batch
```
`-tables` 格式:`"表名|表注释"`——注释可省略(`|` 后内容被忽略);表注释用于模块中文名(日志标题/字典名)。
## 执行流程(CLI 侧)
```text
1. 读取 manifest/config/config.yaml(generator.* 配置)
2. 空白 import internal/logic → 全部服务 init 注册
3. 调用 service.Generator().Preview / Generate / BatchGenerate
——与 HTTP 入口同一套核心逻辑
4. 输出分组结果:
+ 生成(Generated) - 跳过(Skipped) * 覆盖(Overwritten) ! 失败(Failed)
5. 提示:"下一步: go build ./... 并重启服务"
```
## 生成后必做
```sh
go build ./... && ./bin/admin-server.exe # 编译并重启(init 注册/路由生效)
```
然后:
```text
1. 登录 → 系统管理 → 角色管理 → 为角色勾选新模块权限节点
2. 访问新菜单验证增删改查
3. 租户级新表:把表名加入 internal/model/hooks 的 tenantTables(重启生效)
```
## CLI 与 HTTP 的选择
| 场景 | 推荐 |
|------|------|
| 批量生成多表 | CLI(命令行批量更顺手) |
| 团队协作/远程环境 | HTTP 管理页(可视化预览) |
| CI 流水线 | CLI |
| 预览确认再生成 | 两者皆可(-preview / preview 接口) |
## 常见问题
| 症状 | 原因 |
|------|------|
| `unknown table` | 表不存在或库连接错(查 config.yaml database) |
| 注释/枚举缺失 | 非 MySQL 库内省不到注释——建议 MySQL 上生成或人工补注释 |
| 提示"路由已存在" | registerRouter 幂等——正常(router.go 已含该注册行) |
| 生成后编译失败 | gofmt 失败会报错中断;查输出 Failed 项 |
| CLI panic 注册冲突 | RegisterJobHandler 重名等 init 冲突——查 internal/task |