From e233aa87a7c703f1c1c967d66570db2ee23ea920 Mon Sep 17 00:00:00 2001 From: neontx Date: Wed, 17 Jan 2018 11:46:22 +0800 Subject: [PATCH] Add override param to bee generate appcode Add override param to bee generate appcode, then users could make a decision to override generated files in command line --- cmd/commands/generate/generate.go | 9 ++++++++- generate/g.go | 1 + generate/g_appcode.go | 10 ++++------ utils/utils.go | 6 ++++++ 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/cmd/commands/generate/generate.go b/cmd/commands/generate/generate.go index 2fa8cbe..a54652c 100644 --- a/cmd/commands/generate/generate.go +++ b/cmd/commands/generate/generate.go @@ -59,7 +59,7 @@ var CmdGenerate = &commands.Command{ ▶ {{"To generate appcode based on an existing database:"|bold}} - $ bee generate appcode [-tables=""] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] [-level=3] + $ bee generate appcode [-tables=""] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] [-level=3] [-override=n] `, PreRun: func(cmd *commands.Command, args []string) { version.ShowShortVersionBanner() }, Run: GenerateCode, @@ -72,6 +72,7 @@ func init() { CmdGenerate.Flag.Var(&generate.Level, "level", "Either 1, 2 or 3. i.e. 1=models; 2=models and controllers; 3=models, controllers and routers.") CmdGenerate.Flag.Var(&generate.Fields, "fields", "List of table Fields.") CmdGenerate.Flag.Var(&generate.DDL, "ddl", "Generate DDL Migration") + CmdGenerate.Flag.Var(&generate.Override, "override", "Override generated files or not.") commands.AvailableCommands = append(commands.AvailableCommands, CmdGenerate) } @@ -160,10 +161,16 @@ func appCode(cmd *commands.Command, args []string, currpath string) { if generate.Level == "" { generate.Level = "3" } + + if generate.Override == "" { + generate.Override = "n" + } + beeLogger.Log.Infof("Using '%s' as 'SQLDriver'", generate.SQLDriver) beeLogger.Log.Infof("Using '%s' as 'SQLConn'", generate.SQLConn) beeLogger.Log.Infof("Using '%s' as 'Tables'", generate.Tables) beeLogger.Log.Infof("Using '%s' as 'Level'", generate.Level) + beeLogger.Log.Infof("Using '%s' as 'Override'", generate.Override) generate.GenerateAppcode(generate.SQLDriver.String(), generate.SQLConn.String(), generate.Level.String(), generate.Tables.String(), currpath) } func migration(cmd *commands.Command, args []string, currpath string) { diff --git a/generate/g.go b/generate/g.go index 691acc7..7f31121 100644 --- a/generate/g.go +++ b/generate/g.go @@ -22,3 +22,4 @@ var Level utils.DocValue var Tables utils.DocValue var Fields utils.DocValue var DDL utils.DocValue +var Override utils.DocValue diff --git a/generate/g_appcode.go b/generate/g_appcode.go index 155bad5..b0e5de7 100644 --- a/generate/g_appcode.go +++ b/generate/g_appcode.go @@ -732,6 +732,7 @@ func writeSourceFiles(pkgPath string, tables []*Table, mode byte, paths *MvcPath if (OModel & mode) == OModel { beeLogger.Log.Info("Creating model files...") writeModelFiles(tables, paths.ModelPath, selectedTables) + } if (OController & mode) == OController { beeLogger.Log.Info("Creating controller files...") @@ -759,8 +760,7 @@ func writeModelFiles(tables []*Table, mPath string, selectedTables map[string]bo var f *os.File var err error if utils.IsExist(fpath) { - beeLogger.Log.Warnf("'%s' already exists. Do you want to overwrite it? [Yes|No] ", fpath) - if utils.AskForConfirmation() { + if Override == "y" || utils.WarningBeforeAskForConfirmation("'%s' already exists. Do you want to overwrite it? [Yes|No] ", fpath) { f, err = os.OpenFile(fpath, os.O_RDWR|os.O_TRUNC, 0666) if err != nil { beeLogger.Log.Warnf("%s", err) @@ -824,8 +824,7 @@ func writeControllerFiles(tables []*Table, cPath string, selectedTables map[stri var f *os.File var err error if utils.IsExist(fpath) { - beeLogger.Log.Warnf("'%s' already exists. Do you want to overwrite it? [Yes|No] ", fpath) - if utils.AskForConfirmation() { + if Override == "y" || utils.WarningBeforeAskForConfirmation("'%s' already exists. Do you want to overwrite it? [Yes|No] ", fpath) { f, err = os.OpenFile(fpath, os.O_RDWR|os.O_TRUNC, 0666) if err != nil { beeLogger.Log.Warnf("%s", err) @@ -880,8 +879,7 @@ func writeRouterFile(tables []*Table, rPath string, selectedTables map[string]bo var f *os.File var err error if utils.IsExist(fpath) { - beeLogger.Log.Warnf("'%s' already exists. Do you want to overwrite it? [Yes|No] ", fpath) - if utils.AskForConfirmation() { + if Override == "y" || utils.WarningBeforeAskForConfirmation("'%s' already exists. Do you want to overwrite it? [Yes|No] ", fpath) { f, err = os.OpenFile(fpath, os.O_RDWR|os.O_TRUNC, 0666) if err != nil { beeLogger.Log.Warnf("%s", err) diff --git a/utils/utils.go b/utils/utils.go index 13e50b1..d5e6876 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -157,6 +157,12 @@ func AskForConfirmation() bool { } } +// print a warning message before calling AskForConfirmation() +func WarningBeforeAskForConfirmation(warning string, fpath string) bool { + beeLogger.Log.Warnf(warning, fpath) + return AskForConfirmation() +} + func containsString(slice []string, element string) bool { for _, elem := range slice { if elem == element {