From 9bf23ea88ae32e3bd9d779f117bc53f14cfdb597 Mon Sep 17 00:00:00 2001 From: ZhengYang Date: Thu, 21 Aug 2014 10:19:44 +0800 Subject: [PATCH 1/4] help/error message correction and import lib/pq --- g.go | 2 +- g_appcode.go | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/g.go b/g.go index 5c45ea4..e221c40 100644 --- a/g.go +++ b/g.go @@ -23,7 +23,7 @@ var cmdGenerate = &Command{ bee generate scaffold [scaffoldname] [-fields=""] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] The generate scaffold command will do a number of things for you. -fields: a list of table fields. Format: field:type, ... - -driver: [mysql | postgresql | sqlite], the default is mysql + -driver: [mysql | postgres | sqlite], the default is mysql -conn: the connection string used by the driver, the default is root:@tcp(127.0.0.1:3306)/test example: bee generate scaffold post -fields="title:string,body:text" diff --git a/g_appcode.go b/g_appcode.go index 6cdf29b..c235902 100644 --- a/g_appcode.go +++ b/g_appcode.go @@ -25,6 +25,7 @@ import ( "strings" _ "github.com/go-sql-driver/mysql" + _ "github.com/lib/pq" ) const ( @@ -223,7 +224,7 @@ func generateAppcode(driver, connStr, level, tables, currpath string) { func gen(dbms, connStr string, mode byte, selectedTableNames map[string]bool, currpath string) { db, err := sql.Open(dbms, connStr) if err != nil { - ColorLog("[ERRO] Could not connect to %s: %s\n", dbms, connStr) + ColorLog("[ERRO] Could not connect to %s database: %s %s\n", dbms, connStr, err) os.Exit(2) } defer db.Close() From 4fe9c4ddcdb51731b5689539b901477041a3cf43 Mon Sep 17 00:00:00 2001 From: ZhengYang Date: Thu, 21 Aug 2014 10:45:52 +0800 Subject: [PATCH 2/4] refactor long if to switch case, more error messages --- g_appcode.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/g_appcode.go b/g_appcode.go index c235902..829bae3 100644 --- a/g_appcode.go +++ b/g_appcode.go @@ -198,13 +198,14 @@ func (tag *OrmTag) String() string { func generateAppcode(driver, connStr, level, tables, currpath string) { var mode byte - if level == "1" { + switch level { + case "1": mode = O_MODEL - } else if level == "2" { + case "2": mode = O_MODEL | O_CONTROLLER - } else if level == "3" { + case "3": mode = O_MODEL | O_CONTROLLER | O_ROUTER - } else { + default: ColorLog("[ERRO] Invalid 'level' option: %s\n", level) ColorLog("[HINT] Level must be either 1, 2 or 3\n") os.Exit(2) @@ -216,6 +217,19 @@ func generateAppcode(driver, connStr, level, tables, currpath string) { selectedTables[v] = true } } + switch driver { + case "mysql": + fmt.Println("MySQL") + case "postgres": + fmt.Println("PostgreSQL") + case "sqlite": + ColorLog("[ERRO] Generating app code from SQLite database is not supported yet.\n") + os.Exit(2) + default: + ColorLog("[ERRO] Unknown database driver: %s\n", driver) + ColorLog("[HINT] Driver must be one of mysql, postgres or sqlite\n") + os.Exit(2) + } gen(driver, connStr, mode, selectedTables, currpath) } From f2528fcf799de47407edac26bc62dc78a125251d Mon Sep 17 00:00:00 2001 From: ZhengYang Date: Thu, 21 Aug 2014 10:54:21 +0800 Subject: [PATCH 3/4] minor clean up --- g_appcode.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/g_appcode.go b/g_appcode.go index 829bae3..80c2403 100644 --- a/g_appcode.go +++ b/g_appcode.go @@ -219,9 +219,7 @@ func generateAppcode(driver, connStr, level, tables, currpath string) { } switch driver { case "mysql": - fmt.Println("MySQL") case "postgres": - fmt.Println("PostgreSQL") case "sqlite": ColorLog("[ERRO] Generating app code from SQLite database is not supported yet.\n") os.Exit(2) From 5c85c10b18cd6a5f8411c2aa7b4befecf1438a1e Mon Sep 17 00:00:00 2001 From: ZhengYang Date: Thu, 21 Aug 2014 11:01:40 +0800 Subject: [PATCH 4/4] minor changes --- g_appcode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/g_appcode.go b/g_appcode.go index 80c2403..d94fbb1 100644 --- a/g_appcode.go +++ b/g_appcode.go @@ -236,7 +236,7 @@ func generateAppcode(driver, connStr, level, tables, currpath string) { func gen(dbms, connStr string, mode byte, selectedTableNames map[string]bool, currpath string) { db, err := sql.Open(dbms, connStr) if err != nil { - ColorLog("[ERRO] Could not connect to %s database: %s %s\n", dbms, connStr, err) + ColorLog("[ERRO] Could not connect to %s database: %s, %s\n", dbms, connStr, err) os.Exit(2) } defer db.Close()