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

Add override param to bee generate appcode, then users could make a decision to override generated files in command line
This commit is contained in:
neontx 2018-01-17 11:46:22 +08:00
parent e90da8f77b
commit e233aa87a7
4 changed files with 19 additions and 7 deletions

View File

@ -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) {

View File

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

View File

@ -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)

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 {
for _, elem := range slice {
if elem == element {