mirror of
https://github.com/beego/bee.git
synced 2024-11-21 23:50:54 +00:00
ask whether overwrite the file
This commit is contained in:
parent
5ffc17febb
commit
25fccbdf91
54
g_models.go
54
g_models.go
@ -451,11 +451,27 @@ 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)
|
||||
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 == "" {
|
||||
template = STRUCT_MODEL_TPL
|
||||
@ -488,11 +504,27 @@ 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)
|
||||
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 {
|
||||
ColorLog("[ERRO] Could not write controller file to %s\n", fpath)
|
||||
@ -527,11 +559,27 @@ 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)
|
||||
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)
|
||||
os.Exit(2)
|
||||
|
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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user