mirror of
				https://github.com/beego/bee.git
				synced 2025-11-03 23:03:27 +00:00 
			
		
		
		
	ask whether overwrite the file
This commit is contained in:
		
							
								
								
									
										72
									
								
								g_models.go
									
									
									
									
									
								
							
							
						
						
									
										72
									
								
								g_models.go
									
									
									
									
									
								
							@@ -451,10 +451,26 @@ func writeModelFiles(tables []*Table, mPath string, selectedTables map[string]bo
 | 
			
		||||
		}
 | 
			
		||||
		filename := getFileName(tb.Name)
 | 
			
		||||
		fpath := path.Join(mPath, filename+".go")
 | 
			
		||||
		f, err := os.OpenFile(fpath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			ColorLog("[WARN] %v\n", err)
 | 
			
		||||
			continue
 | 
			
		||||
		var f *os.File
 | 
			
		||||
		var err error
 | 
			
		||||
		if isExist(fpath) {
 | 
			
		||||
			ColorLog("[WARN] %v is exist, do you want to overwrite it? Yes or No?\n", fpath)
 | 
			
		||||
			if askForConfirmation() {
 | 
			
		||||
				f, err = os.OpenFile(fpath, os.O_RDWR, 0666)
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					ColorLog("[WARN] %v\n", err)
 | 
			
		||||
					continue
 | 
			
		||||
				}
 | 
			
		||||
			} else {
 | 
			
		||||
				ColorLog("[WARN] skip create file\n")
 | 
			
		||||
				continue
 | 
			
		||||
			}
 | 
			
		||||
		} else {
 | 
			
		||||
			f, err = os.OpenFile(fpath, os.O_CREATE, 0666)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				ColorLog("[WARN] %v\n", err)
 | 
			
		||||
				continue
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		template := ""
 | 
			
		||||
		if tb.Pk == "" {
 | 
			
		||||
@@ -488,10 +504,26 @@ func writeControllerFiles(tables []*Table, cPath string, selectedTables map[stri
 | 
			
		||||
		}
 | 
			
		||||
		filename := getFileName(tb.Name)
 | 
			
		||||
		fpath := path.Join(cPath, filename+".go")
 | 
			
		||||
		f, err := os.OpenFile(fpath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			ColorLog("[WARN] %v\n", err)
 | 
			
		||||
			continue
 | 
			
		||||
		var f *os.File
 | 
			
		||||
		var err error
 | 
			
		||||
		if isExist(fpath) {
 | 
			
		||||
			ColorLog("[WARN] %v is exist, do you want to overwrite it? Yes or No?\n", fpath)
 | 
			
		||||
			if askForConfirmation() {
 | 
			
		||||
				f, err = os.OpenFile(fpath, os.O_RDWR, 0666)
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					ColorLog("[WARN] %v\n", err)
 | 
			
		||||
					continue
 | 
			
		||||
				}
 | 
			
		||||
			} else {
 | 
			
		||||
				ColorLog("[WARN] skip create file\n")
 | 
			
		||||
				continue
 | 
			
		||||
			}
 | 
			
		||||
		} else {
 | 
			
		||||
			f, err = os.OpenFile(fpath, os.O_CREATE, 0666)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				ColorLog("[WARN] %v\n", err)
 | 
			
		||||
				continue
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		fileStr := strings.Replace(CTRL_TPL, "{{ctrlName}}", camelCase(tb.Name), -1)
 | 
			
		||||
		if _, err := f.WriteString(fileStr); err != nil {
 | 
			
		||||
@@ -527,10 +559,26 @@ func writeRouterFile(tables []*Table, rPath string, selectedTables map[string]bo
 | 
			
		||||
	routerStr := strings.Replace(ROUTER_TPL, "{{nameSpaces}}", strings.Join(nameSpaces, ""), 1)
 | 
			
		||||
	_, projectName := path.Split(path.Dir(rPath))
 | 
			
		||||
	routerStr = strings.Replace(routerStr, "{{projectName}}", projectName, 1)
 | 
			
		||||
	f, err := os.OpenFile(fpath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		ColorLog("[WARN] %v\n", err)
 | 
			
		||||
		return
 | 
			
		||||
	var f *os.File
 | 
			
		||||
	var err error
 | 
			
		||||
	if isExist(fpath) {
 | 
			
		||||
		ColorLog("[WARN] %v is exist, do you want to overwrite it? Yes or No?\n", fpath)
 | 
			
		||||
		if askForConfirmation() {
 | 
			
		||||
			f, err = os.OpenFile(fpath, os.O_RDWR, 0666)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				ColorLog("[WARN] %v\n", err)
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
		} else {
 | 
			
		||||
			ColorLog("[WARN] skip create file\n")
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
	} else {
 | 
			
		||||
		f, err = os.OpenFile(fpath, os.O_CREATE, 0666)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			ColorLog("[WARN] %v\n", err)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	if _, err := f.WriteString(routerStr); err != nil {
 | 
			
		||||
		ColorLog("[ERRO] Could not write router file to %s\n", fpath)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										33
									
								
								util.go
									
									
									
									
									
								
							
							
						
						
									
										33
									
								
								util.go
									
									
									
									
									
								
							@@ -16,6 +16,7 @@ package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"log"
 | 
			
		||||
	"os"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
	"runtime"
 | 
			
		||||
@@ -170,3 +171,35 @@ func GetGOPATHs() []string {
 | 
			
		||||
	}
 | 
			
		||||
	return paths
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// askForConfirmation uses Scanln to parse user input. A user must type in "yes" or "no" and
 | 
			
		||||
// then press enter. It has fuzzy matching, so "y", "Y", "yes", "YES", and "Yes" all count as
 | 
			
		||||
// confirmations. If the input is not recognized, it will ask again. The function does not return
 | 
			
		||||
// until it gets a valid response from the user. Typically, you should use fmt to print out a question
 | 
			
		||||
// before calling askForConfirmation. E.g. fmt.Println("WARNING: Are you sure? (yes/no)")
 | 
			
		||||
func askForConfirmation() bool {
 | 
			
		||||
	var response string
 | 
			
		||||
	_, err := fmt.Scanln(&response)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
	okayResponses := []string{"y", "Y", "yes", "Yes", "YES"}
 | 
			
		||||
	nokayResponses := []string{"n", "N", "no", "No", "NO"}
 | 
			
		||||
	if containsString(okayResponses, response) {
 | 
			
		||||
		return true
 | 
			
		||||
	} else if containsString(nokayResponses, response) {
 | 
			
		||||
		return false
 | 
			
		||||
	} else {
 | 
			
		||||
		fmt.Println("Please type yes or no and then press enter:")
 | 
			
		||||
		return askForConfirmation()
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func containsString(slice []string, element string) bool {
 | 
			
		||||
	for _, elem := range slice {
 | 
			
		||||
		if elem == element {
 | 
			
		||||
			return true
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return false
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user