Generator clean up.
This commit is contained in:
parent
4ab9d73a8b
commit
91dc633b45
8 changed files with 98 additions and 68 deletions
|
|
@ -10,6 +10,7 @@ import (
|
|||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/lib/pq"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
@ -28,16 +29,16 @@ var (
|
|||
)
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&source, "source", postgres.Dialect.Name(), "Database name")
|
||||
flag.StringVar(&source, "source", "", "Database system name (PostgreSQL or MySQL)")
|
||||
|
||||
flag.StringVar(&host, "host", "", "Database host path (Example: localhost)")
|
||||
flag.IntVar(&port, "port", 0, "Database port")
|
||||
flag.StringVar(&user, "user", "", "Database user")
|
||||
flag.StringVar(&password, "password", "", "The user’s password")
|
||||
flag.StringVar(&sslmode, "sslmode", "disable", "Whether or not to use SSL(optional)")
|
||||
flag.StringVar(¶ms, "params", "", "Additional connection string parameters(optional)")
|
||||
flag.StringVar(&dbName, "dbname", "", "name of the database")
|
||||
flag.StringVar(&schemaName, "schema", "public", "Database schema name.")
|
||||
flag.StringVar(&dbName, "dbname", "", "Database name")
|
||||
flag.StringVar(&schemaName, "schema", "public", `Database schema name. (default "public") (ignored for MySQL)`)
|
||||
flag.StringVar(&sslmode, "sslmode", "disable", `Whether or not to use SSL(optional)(default "disable") (ignored for MySQL)`)
|
||||
|
||||
flag.StringVar(&destDir, "path", "", "Destination dir for files generated.")
|
||||
}
|
||||
|
|
@ -46,7 +47,11 @@ func main() {
|
|||
|
||||
flag.Usage = func() {
|
||||
_, _ = fmt.Fprint(os.Stdout, `
|
||||
Usage of jet:
|
||||
Jet generator 2.0.0
|
||||
|
||||
Usage:
|
||||
-source string
|
||||
Database system name (PostgreSQL or MySQL)
|
||||
-host string
|
||||
Database host path (Example: localhost)
|
||||
-port int
|
||||
|
|
@ -56,13 +61,13 @@ Usage of jet:
|
|||
-password string
|
||||
The user’s password
|
||||
-dbname string
|
||||
name of the database
|
||||
Database name
|
||||
-params string
|
||||
Additional connection string parameters(optional)
|
||||
-schema string
|
||||
Database schema name. (default "public")
|
||||
Database schema name. (default "public") (ignored for MySQL)
|
||||
-sslmode string
|
||||
Whether or not to use SSL(optional) (default "disable")
|
||||
Whether or not to use SSL(optional) (default "disable") (ignored for MySQL)
|
||||
-path string
|
||||
Destination dir for files generated.
|
||||
`)
|
||||
|
|
@ -70,16 +75,14 @@ Usage of jet:
|
|||
|
||||
flag.Parse()
|
||||
|
||||
if source == "" || host == "" || port == 0 || user == "" || dbName == "" {
|
||||
printErrorAndExit("\nERROR: required flag(s) missing")
|
||||
}
|
||||
|
||||
var err error
|
||||
|
||||
switch source {
|
||||
case postgres.Dialect.Name():
|
||||
if host == "" || port == 0 || user == "" || dbName == "" || schemaName == "" {
|
||||
fmt.Println("\njet: required flag missing")
|
||||
flag.Usage()
|
||||
os.Exit(-2)
|
||||
}
|
||||
|
||||
switch strings.ToLower(strings.TrimSpace(source)) {
|
||||
case strings.ToLower(postgres.Dialect.Name()):
|
||||
genData := postgresgen.DBConnection{
|
||||
Host: host,
|
||||
Port: port,
|
||||
|
|
@ -94,12 +97,7 @@ Usage of jet:
|
|||
|
||||
err = postgresgen.Generate(destDir, genData)
|
||||
|
||||
case mysql.Dialect.Name():
|
||||
if host == "" || port == 0 || user == "" || dbName == "" {
|
||||
fmt.Println("\njet: required flag missing")
|
||||
flag.Usage()
|
||||
os.Exit(-2)
|
||||
}
|
||||
case strings.ToLower(mysql.Dialect.Name()):
|
||||
|
||||
dbConn := mysqlgen.DBConnection{
|
||||
Host: host,
|
||||
|
|
@ -112,10 +110,19 @@ Usage of jet:
|
|||
}
|
||||
|
||||
err = mysqlgen.Generate(destDir, dbConn)
|
||||
default:
|
||||
fmt.Println("ERROR: unsupported source " + source + ". " + postgres.Dialect.Name() + " and " + mysql.Dialect.Name() + " are currently supported.")
|
||||
os.Exit(-4)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
os.Exit(-1)
|
||||
os.Exit(-5)
|
||||
}
|
||||
}
|
||||
|
||||
func printErrorAndExit(error string) {
|
||||
fmt.Println(error)
|
||||
flag.Usage()
|
||||
os.Exit(-2)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue