diff --git a/cmd/commands/generate/generate.go b/cmd/commands/generate/generate.go index 993ff3f..2bf4cbf 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,14 @@ 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) } diff --git a/generate/g.go b/generate/g.go index 691acc7..f412c4d 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 \ No newline at end of file diff --git a/generate/g_appcode.go b/generate/g_appcode.go index 260472a..07e30f2 100644 --- a/generate/g_appcode.go +++ b/generate/g_appcode.go @@ -753,8 +753,7 @@ func writeModelFiles(tables []*Table, mPath string) { 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) @@ -812,8 +811,7 @@ func writeControllerFiles(tables []*Table, cPath string, pkgPath string) { 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) @@ -862,8 +860,7 @@ func writeRouterFile(tables []*Table, rPath string, pkgPath string) { 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 {