Files
atomctl/cmd/gen_model.go
Rogee e1f83ae469 feat: 重构 pkg/ast/provider 模块,优化代码组织逻辑和功能实现
## 主要改进

### 架构重构
- 将单体 provider.go 拆分为多个专门的模块文件
- 实现了清晰的职责分离和模块化设计
- 遵循 SOLID 原则,提高代码可维护性

### 新增功能
- **验证规则系统**: 实现了完整的 provider 验证框架
- **报告生成器**: 支持多种格式的验证报告 (JSON/HTML/Markdown/Text)
- **解析器优化**: 重新设计了解析流程,提高性能和可扩展性
- **错误处理**: 增强了错误处理和诊断能力

### 修复关键 Bug
- 修复 @provider(job) 注解缺失 __job 注入参数的问题
- 统一了 job 和 cronjob 模式的处理逻辑
- 确保了 provider 生成的正确性和一致性

### 代码质量提升
- 添加了完整的测试套件
- 引入了 golangci-lint 代码质量检查
- 优化了代码格式和结构
- 增加了详细的文档和规范

### 文件结构优化
```
pkg/ast/provider/
├── types.go              # 类型定义
├── parser.go             # 解析器实现
├── validator.go          # 验证规则
├── report_generator.go   # 报告生成
├── renderer.go           # 渲染器
├── comment_parser.go     # 注解解析
├── modes.go             # 模式定义
├── errors.go            # 错误处理
└── validator_test.go    # 测试文件
```

### 兼容性
- 保持向后兼容性
- 支持现有的所有 provider 模式
- 优化了 API 设计和用户体验

This completes the implementation of T025-T029 tasks following TDD principles,
including validation rules implementation and critical bug fixes.
2025-09-19 18:58:30 +08:00

67 lines
1.7 KiB
Go
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.
package cmd
import (
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
apg "go.ipao.vip/atomctl/v2/pkg/postgres"
"go.ipao.vip/atomctl/v2/pkg/utils/gomod"
"go.ipao.vip/gen"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
func CommandGenModel(root *cobra.Command) {
cmd := &cobra.Command{
Use: "model",
Aliases: []string{"m"},
Short: "Generate models",
Long: `根据数据库连接配置生成模型代码,输出到 ./database并支持基于 ./database/.transform.yaml 的类型/命名转换。
配置:通过 -c/--config 指定配置文件(默认 config.toml从 [Database] 段读取:
Username, Password, Database, Host, Port, Schema, SslMode, TimeZone
行为:
- 解析 go.mod 以识别项目模块名
- 连接 PostgreSQLgorm + postgres driver校验连通性
- 调用 go.ipao.vip/gen 生成模型与相关文件到 ./database
- 根据 .transform.yaml 应用转换规则
示例:
atomctl gen -c config.toml model`,
RunE: commandGenModelE,
}
root.AddCommand(cmd)
}
func commandGenModelE(cmd *cobra.Command, args []string) error {
if err := gomod.Parse("go.mod"); err != nil {
return errors.Wrap(err, "parse go.mod")
}
cfgFile := cmd.Flag("config").Value.String()
if cfgFile == "" {
cfgFile = "config.toml"
}
sqlDB, conf, err := apg.GetDB(cfgFile)
if err != nil {
return errors.Wrap(err, "load database config")
}
defer sqlDB.Close()
dsn := conf.DSN()
log.Infof("parsed DSN: %s (schema=%s)", dsn, conf.Schema)
db, err := gorm.Open(postgres.New(postgres.Config{DSN: dsn}))
if err != nil {
return errors.Wrapf(err, "open database with dsn: %s", dsn)
}
// 默认同包同目录生成到 ./database
gen.GenerateWithDefault(db, "./database/.transform.yaml")
return nil
}