mirror of
https://github.com/casbin/bee.git
synced 2025-07-18 10:24:21 +08:00
This fixes #201
This commit is contained in:
51
new.go
51
new.go
@ -55,7 +55,6 @@ func init() {
|
||||
}
|
||||
|
||||
func createApp(cmd *Command, args []string) int {
|
||||
curpath, _ := os.Getwd()
|
||||
if len(args) != 1 {
|
||||
ColorLog("[ERRO] Argument [appname] is missing\n")
|
||||
os.Exit(2)
|
||||
@ -65,40 +64,12 @@ func createApp(cmd *Command, args []string) int {
|
||||
Debugf("gopath:%s", gopath)
|
||||
if gopath == "" {
|
||||
ColorLog("[ERRO] $GOPATH not found\n")
|
||||
ColorLog("[HINT] Set $GOPATH in your environment vairables\n")
|
||||
os.Exit(2)
|
||||
}
|
||||
haspath := false
|
||||
appsrcpath := ""
|
||||
|
||||
wgopath := path.SplitList(gopath)
|
||||
for _, wg := range wgopath {
|
||||
|
||||
wg = path.Join(wg, "src")
|
||||
|
||||
if strings.HasPrefix(strings.ToLower(curpath), strings.ToLower(wg)) {
|
||||
haspath = true
|
||||
appsrcpath = wg
|
||||
break
|
||||
}
|
||||
|
||||
wg, _ = path.EvalSymlinks(wg)
|
||||
|
||||
if strings.HasPrefix(strings.ToLower(curpath), strings.ToLower(wg)) {
|
||||
haspath = true
|
||||
appsrcpath = wg
|
||||
break
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if !haspath {
|
||||
ColorLog("[ERRO] Unable to create an application outside of $GOPATH%ssrc(%s%ssrc)\n", string(path.Separator), gopath, string(path.Separator))
|
||||
ColorLog("[HINT] Change your work directory by `cd ($GOPATH%ssrc)`\n", string(path.Separator))
|
||||
ColorLog("[HINT] Set $GOPATH in your environment variables\n")
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
apppath := path.Join(curpath, args[0])
|
||||
gosrcpath := path.Join(gopath, "src") // User's workspace
|
||||
apppath := path.Join(gosrcpath, args[0])
|
||||
|
||||
if isExist(apppath) {
|
||||
ColorLog("[ERRO] Path (%s) already exists\n", apppath)
|
||||
@ -133,22 +104,22 @@ func createApp(cmd *Command, args []string) int {
|
||||
fmt.Println(path.Join(apppath, "views") + string(path.Separator))
|
||||
os.Mkdir(path.Join(apppath, "views"), 0755)
|
||||
fmt.Println(path.Join(apppath, "conf", "app.conf"))
|
||||
writetofile(path.Join(apppath, "conf", "app.conf"), strings.Replace(appconf, "{{.Appname}}", args[0], -1))
|
||||
WriteToFile(path.Join(apppath, "conf", "app.conf"), strings.Replace(appconf, "{{.Appname}}", path.Base(args[0]), -1))
|
||||
|
||||
fmt.Println(path.Join(apppath, "controllers", "default.go"))
|
||||
writetofile(path.Join(apppath, "controllers", "default.go"), controllers)
|
||||
WriteToFile(path.Join(apppath, "controllers", "default.go"), controllers)
|
||||
|
||||
fmt.Println(path.Join(apppath, "views", "index.tpl"))
|
||||
writetofile(path.Join(apppath, "views", "index.tpl"), indextpl)
|
||||
WriteToFile(path.Join(apppath, "views", "index.tpl"), indextpl)
|
||||
|
||||
fmt.Println(path.Join(apppath, "routers", "router.go"))
|
||||
writetofile(path.Join(apppath, "routers", "router.go"), strings.Replace(router, "{{.Appname}}", strings.Join(strings.Split(apppath[len(appsrcpath)+1:], string(path.Separator)), "/"), -1))
|
||||
WriteToFile(path.Join(apppath, "routers", "router.go"), strings.Replace(router, "{{.Appname}}", strings.Join(strings.Split(apppath[len(gosrcpath)+1:], string(path.Separator)), "/"), -1))
|
||||
|
||||
fmt.Println(path.Join(apppath, "tests", "default_test.go"))
|
||||
writetofile(path.Join(apppath, "tests", "default_test.go"), strings.Replace(test, "{{.Appname}}", strings.Join(strings.Split(apppath[len(appsrcpath)+1:], string(path.Separator)), "/"), -1))
|
||||
WriteToFile(path.Join(apppath, "tests", "default_test.go"), strings.Replace(test, "{{.Appname}}", strings.Join(strings.Split(apppath[len(gosrcpath)+1:], string(path.Separator)), "/"), -1))
|
||||
|
||||
fmt.Println(path.Join(apppath, "main.go"))
|
||||
writetofile(path.Join(apppath, "main.go"), strings.Replace(maingo, "{{.Appname}}", strings.Join(strings.Split(apppath[len(appsrcpath)+1:], string(path.Separator)), "/"), -1))
|
||||
WriteToFile(path.Join(apppath, "main.go"), strings.Replace(maingo, "{{.Appname}}", strings.Join(strings.Split(apppath[len(gosrcpath)+1:], string(path.Separator)), "/"), -1))
|
||||
|
||||
ColorLog("[SUCC] New application successfully created!\n")
|
||||
return 0
|
||||
@ -336,11 +307,11 @@ var indextpl = `<!DOCTYPE html>
|
||||
</html>
|
||||
`
|
||||
|
||||
func writetofile(filename, content string) {
|
||||
func WriteToFile(filename, content string) {
|
||||
f, err := os.Create(filename)
|
||||
defer f.Close()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer f.Close()
|
||||
f.WriteString(content)
|
||||
}
|
||||
|
Reference in New Issue
Block a user