1
0

generate.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //go:generate go run .
  2. package main
  3. import (
  4. "flag"
  5. "fmt"
  6. "path/filepath"
  7. "runtime"
  8. "github.com/0xJacky/Nginx-UI/model"
  9. "github.com/0xJacky/Nginx-UI/settings"
  10. "github.com/uozi-tech/cosy/logger"
  11. cSettings "github.com/uozi-tech/cosy/settings"
  12. "gorm.io/driver/sqlite"
  13. "gorm.io/gen"
  14. "gorm.io/gorm"
  15. gormlogger "gorm.io/gorm/logger"
  16. )
  17. func main() {
  18. logger.Init("release")
  19. _, file, _, ok := runtime.Caller(0)
  20. if !ok {
  21. logger.Error("Unable to get the current file")
  22. return
  23. }
  24. basePath := filepath.Join(filepath.Dir(file), "../../")
  25. // specify the output directory (default: "./query")
  26. // ### if you want to query without context constrain, set mode gen.WithoutContext ###
  27. g := gen.NewGenerator(gen.Config{
  28. OutPath: filepath.Join(basePath, "query"),
  29. Mode: gen.WithoutContext | gen.WithDefaultQuery,
  30. //if you want the nullable field generation property to be pointer type, set FieldNullable true
  31. FieldNullable: true,
  32. //if you want to assign field which has the default value in `Create` API, set FieldCoverable true, reference: https://gorm.io/docs/create.html#Default-Values
  33. FieldCoverable: true,
  34. // if you want to generate field with an unsigned integer type, set FieldSignable true
  35. /* FieldSignable: true,*/
  36. //if you want to generate index tags from the database, set FieldWithIndexTag true
  37. /* FieldWithIndexTag: true,*/
  38. //if you want to generate type tags from the database, set FieldWithTypeTag true
  39. /* FieldWithTypeTag: true,*/
  40. //if you need unit tests for query code, set WithUnitTest true
  41. /* WithUnitTest: true, */
  42. })
  43. // reuse the database connection in Project or create a connection here
  44. // if you want to use GenerateModel/GenerateModelAs, UseDB is necessary, or it will panic
  45. var confPath string
  46. flag.StringVar(&confPath, "config", "app.ini", "Specify the configuration file")
  47. flag.Parse()
  48. cSettings.Init(confPath)
  49. dbPath := filepath.Join(filepath.Dir(confPath), fmt.Sprintf("%s.db", settings.DatabaseSettings.Name))
  50. var err error
  51. db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{
  52. Logger: gormlogger.Default.LogMode(gormlogger.Info),
  53. PrepareStmt: true,
  54. DisableForeignKeyConstraintWhenMigrating: true,
  55. })
  56. if err != nil {
  57. logger.Fatalf("failed to open database: %v", err)
  58. }
  59. g.UseDB(db)
  60. // apply basic crud api on structs or table models which is specified by table name with function
  61. // GenerateModel/GenerateModelAs. And the generator will generate table models' code when calling Excute.
  62. g.ApplyBasic(model.GenerateAllModel()...)
  63. // apply diy interfaces on structs or table models
  64. g.ApplyInterface(func(method model.Method) {}, model.GenerateAllModel()...)
  65. // execute the action of code generation
  66. g.Execute()
  67. }