Skip to content

Commit

Permalink
Refactored CLI arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
jamius19 committed Nov 22, 2024
1 parent 47fd02f commit 5add82e
Showing 1 changed file with 14 additions and 31 deletions.
45 changes: 14 additions & 31 deletions cmd/jet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ func init() {
flag.StringVar(&schemaName, "schema", "public", `Database schema name. (default "public")(PostgreSQL only)`)
flag.StringVar(&params, "params", "", "Additional connection string parameters(optional). Used only if dsn is not set.")
flag.StringVar(&sslmode, "sslmode", "disable", `Whether or not to use SSL. Used only if dsn is not set. (optional)(default "disable")(PostgreSQL only)`)
flag.StringVar(&ignoreTables, "ignore-tables", "", `Comma-separated list of tables to ignore`)
flag.StringVar(&ignoreViews, "ignore-views", "", `Comma-separated list of views to ignore`)
flag.StringVar(&ignoreEnums, "ignore-enums", "", `Comma-separated list of enums to ignore`)

flag.StringVar(&destDir, "path", "", "Destination dir for files generated.")
flag.StringVar(&modelPkg, "model-pkg", "", "Package name for the model files generated")
flag.StringVar(&tablePkg, "table-pkg", "", "Package name for the table files generated")
flag.StringVar(&viewPkg, "view-pkg", "", "Package name for the view files generated")
flag.StringVar(&enumPkg, "enum-pkg", "", "Package name for the enum files generated")
flag.StringVar(&ignoreTables, "ignore-tables", "", `Comma-separated list of tables to ignore.`)
flag.StringVar(&ignoreViews, "ignore-views", "", `Comma-separated list of views to ignore.`)
flag.StringVar(&ignoreEnums, "ignore-enums", "", `Comma-separated list of enums to ignore.`)

flag.StringVar(&destDir, "path", "", "Destination directory for files generated.")
flag.StringVar(&modelPkg, "model-pkg", "model", "Relative path for the Model files package from the destination directory.")
flag.StringVar(&tablePkg, "table-pkg", "table", "Relative path for the Table files package from the destination directory.")
flag.StringVar(&viewPkg, "view-pkg", "view", "Relative path for the View files package from the destination directory.")
flag.StringVar(&enumPkg, "enum-pkg", "enum", "Relative path for the Enum files package from the destination directory.")
}

func main() {
Expand Down Expand Up @@ -195,6 +195,7 @@ func usage() {
$ jet -source=postgres -dsn="user=jet password=jet host=localhost port=5432 dbname=jetdb" -schema=dvds -path=./gen
$ jet -source=mysql -host=localhost -port=3306 -user=jet -password=jet -dbname=jetdb -path=./gen
$ jet -source=sqlite -dsn="file://path/to/sqlite/database/file" -path=./gen
$ jet -source=sqlite -dsn="file://path/to/sqlite/database/file" -path=./gen -model-pkg=./entity
`)
}

Expand Down Expand Up @@ -252,16 +253,10 @@ func genTemplate(dialect jet.Dialect, ignoreTables []string, ignoreViews []strin
return strslice.Contains(ignoreEnums, strings.ToLower(enum.Name))
}

model := template.DefaultModel()

if modelPkg != "" {
model = model.UsePath("/" + modelPkg)
}

return template.Default(dialect).
UseSchema(func(schemaMetaData metadata.Schema) template.Schema {
return template.DefaultSchema(schemaMetaData).
UseModel(model.
UseModel(template.DefaultModel().UsePath(modelPkg).
UseTable(func(table metadata.Table) template.TableModel {
if shouldSkipTable(table) {
return template.TableModel{Skip: true}
Expand All @@ -287,33 +282,21 @@ func genTemplate(dialect jet.Dialect, ignoreTables []string, ignoreViews []strin
return template.TableSQLBuilder{Skip: true}
}

if tablePkg != "" {
return template.DefaultTableSQLBuilder(table).UsePath("/" + tablePkg)
}

return template.DefaultTableSQLBuilder(table)
return template.DefaultTableSQLBuilder(table).UsePath(tablePkg)
}).
UseView(func(table metadata.Table) template.ViewSQLBuilder {
if shouldSkipView(table) {
return template.ViewSQLBuilder{Skip: true}
}

if viewPkg != "" {
return template.DefaultViewSQLBuilder(table).UsePath("/" + viewPkg)
}

return template.DefaultViewSQLBuilder(table)
return template.DefaultViewSQLBuilder(table).UsePath(viewPkg)
}).
UseEnum(func(enum metadata.Enum) template.EnumSQLBuilder {
if shouldSkipEnum(enum) {
return template.EnumSQLBuilder{Skip: true}
}

if enumPkg != "" {
return template.DefaultEnumSQLBuilder(enum).UsePath("/" + enumPkg)
}

return template.DefaultEnumSQLBuilder(enum)
return template.DefaultEnumSQLBuilder(enum).UsePath(enumPkg)
}),
)
})
Expand Down

0 comments on commit 5add82e

Please sign in to comment.