1
0
mirror of https://github.com/beego/bee.git synced 2024-11-05 01:40:54 +00:00
This commit is contained in:
Deleted user 2018-07-21 14:59:27 +00:00 committed by GitHub
commit ff311e1811
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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}}
$ 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)
}

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

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

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 {