1
0
mirror of https://github.com/beego/bee.git synced 2024-11-22 15:10:54 +00:00

Add override param to bee generate appcode

This commit is contained in:
neontx 2018-01-21 22:15:42 +08:00
parent 4ca7777c32
commit 32fc8f4b1a
4 changed files with 16 additions and 7 deletions

View File

@ -59,7 +59,7 @@ var CmdGenerate = &commands.Command{
{{"To generate appcode based on an existing database:"|bold}} {{"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() }, PreRun: func(cmd *commands.Command, args []string) { version.ShowShortVersionBanner() },
Run: GenerateCode, 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.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.Fields, "fields", "List of table Fields.")
CmdGenerate.Flag.Var(&generate.DDL, "ddl", "Generate DDL Migration") 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) commands.AvailableCommands = append(commands.AvailableCommands, CmdGenerate)
} }
@ -160,10 +161,14 @@ func appCode(cmd *commands.Command, args []string, currpath string) {
if generate.Level == "" { if generate.Level == "" {
generate.Level = "3" generate.Level = "3"
} }
if generate.Override == "" {
generate.Override = "n"
}
beeLogger.Log.Infof("Using '%s' as 'SQLDriver'", generate.SQLDriver) beeLogger.Log.Infof("Using '%s' as 'SQLDriver'", generate.SQLDriver)
beeLogger.Log.Infof("Using '%s' as 'SQLConn'", generate.SQLConn) beeLogger.Log.Infof("Using '%s' as 'SQLConn'", generate.SQLConn)
beeLogger.Log.Infof("Using '%s' as 'Tables'", generate.Tables) beeLogger.Log.Infof("Using '%s' as 'Tables'", generate.Tables)
beeLogger.Log.Infof("Using '%s' as 'Level'", generate.Level) 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) generate.GenerateAppcode(generate.SQLDriver.String(), generate.SQLConn.String(), generate.Level.String(), generate.Tables.String(), currpath)
} }

View File

@ -22,3 +22,4 @@ var Level utils.DocValue
var Tables utils.DocValue var Tables utils.DocValue
var Fields utils.DocValue var Fields utils.DocValue
var DDL utils.DocValue var DDL utils.DocValue
var Override utils.DocValue

View File

@ -753,8 +753,7 @@ func writeModelFiles(tables []*Table, mPath string) {
var f *os.File var f *os.File
var err error var err error
if utils.IsExist(fpath) { if utils.IsExist(fpath) {
beeLogger.Log.Warnf("'%s' already exists. Do you want to overwrite it? [Yes|No] ", fpath) if Override == "y" || utils.WarningBeforeAskForConfirmation("'%s' already exists. Do you want to overwrite it? [Yes|No] ", fpath) {
if utils.AskForConfirmation() {
f, err = os.OpenFile(fpath, os.O_RDWR|os.O_TRUNC, 0666) f, err = os.OpenFile(fpath, os.O_RDWR|os.O_TRUNC, 0666)
if err != nil { if err != nil {
beeLogger.Log.Warnf("%s", err) beeLogger.Log.Warnf("%s", err)
@ -812,8 +811,7 @@ func writeControllerFiles(tables []*Table, cPath string, pkgPath string) {
var f *os.File var f *os.File
var err error var err error
if utils.IsExist(fpath) { if utils.IsExist(fpath) {
beeLogger.Log.Warnf("'%s' already exists. Do you want to overwrite it? [Yes|No] ", fpath) if Override == "y" || utils.WarningBeforeAskForConfirmation("'%s' already exists. Do you want to overwrite it? [Yes|No] ", fpath) {
if utils.AskForConfirmation() {
f, err = os.OpenFile(fpath, os.O_RDWR|os.O_TRUNC, 0666) f, err = os.OpenFile(fpath, os.O_RDWR|os.O_TRUNC, 0666)
if err != nil { if err != nil {
beeLogger.Log.Warnf("%s", err) beeLogger.Log.Warnf("%s", err)
@ -862,8 +860,7 @@ func writeRouterFile(tables []*Table, rPath string, pkgPath string) {
var f *os.File var f *os.File
var err error var err error
if utils.IsExist(fpath) { if utils.IsExist(fpath) {
beeLogger.Log.Warnf("'%s' already exists. Do you want to overwrite it? [Yes|No] ", fpath) if Override == "y" || utils.WarningBeforeAskForConfirmation("'%s' already exists. Do you want to overwrite it? [Yes|No] ", fpath) {
if utils.AskForConfirmation() {
f, err = os.OpenFile(fpath, os.O_RDWR|os.O_TRUNC, 0666) f, err = os.OpenFile(fpath, os.O_RDWR|os.O_TRUNC, 0666)
if err != nil { if err != nil {
beeLogger.Log.Warnf("%s", err) beeLogger.Log.Warnf("%s", err)

View File

@ -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 { func containsString(slice []string, element string) bool {
for _, elem := range slice { for _, elem := range slice {
if elem == element { if elem == element {