fix: if current path is in GOPATH, create api in current path

This commit is contained in:
astaxie 2016-08-19 23:31:52 +08:00
parent 6ccd0e7d9f
commit 31eaaa14e7
2 changed files with 16 additions and 16 deletions

View File

@ -645,9 +645,19 @@ func checkEnv(appname string) (apppath, packpath string, err error) {
ColorLog("[ERRO] Fail to start[ %s ]\n", "GOPATH environment variable is not set or empty") ColorLog("[ERRO] Fail to start[ %s ]\n", "GOPATH environment variable is not set or empty")
os.Exit(2) os.Exit(2)
} }
currpath, _ := os.Getwd()
currpath = path.Join(currpath, appname)
for _, gpath := range gps {
gsrcpath := path.Join(gpath, "src")
if strings.HasPrefix(currpath, gsrcpath) {
return currpath, currpath[len(gsrcpath):], nil
}
}
// In case of multiple paths in the GOPATH, by default // In case of multiple paths in the GOPATH, by default
// we use the first path // we use the first path
gopath := gps[0] gopath := gps[0]
ColorLog("[%s]You current workdir is not a $GOPATH/src, bee will create the application in GOPATH: %s\n", WARN, gopath)
Debugf("GOPATH: %s", gopath) Debugf("GOPATH: %s", gopath)
gosrcpath := path.Join(gopath, "src") gosrcpath := path.Join(gopath, "src")

22
new.go
View File

@ -56,26 +56,16 @@ func init() {
func createApp(cmd *Command, args []string) int { func createApp(cmd *Command, args []string) int {
ShowShortVersionBanner() ShowShortVersionBanner()
w := NewColorWriter(os.Stdout) w := NewColorWriter(os.Stdout)
if len(args) != 1 { if len(args) != 1 {
ColorLog("[ERRO] Argument [appname] is missing\n") ColorLog("[ERRO] Argument [appname] is missing\n")
os.Exit(2) os.Exit(2)
} }
apppath, packpath, err := checkEnv(args[0])
gps := GetGOPATHs() if err != nil {
if len(gps) == 0 { fmt.Println(err)
ColorLog("[ERRO] Fail to start[ %s ]\n", "GOPATH environment variable is not set or empty")
os.Exit(2) os.Exit(2)
} }
// In case of multiple paths in the GOPATH, by default
// we use the first path
gopath := gps[0]
Debugf("GOPATH: %s", gopath)
gosrcpath := path.Join(gopath, "src") // User's workspace
apppath := path.Join(gosrcpath, args[0])
if isExist(apppath) { if isExist(apppath) {
ColorLog("[ERRO] Path (%s) already exists\n", apppath) ColorLog("[ERRO] Path (%s) already exists\n", apppath)
@ -119,13 +109,13 @@ func createApp(cmd *Command, args []string) int {
WriteToFile(path.Join(apppath, "views", "index.tpl"), indextpl) WriteToFile(path.Join(apppath, "views", "index.tpl"), indextpl)
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "routers", "router.go"), "\x1b[0m") fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "routers", "router.go"), "\x1b[0m")
WriteToFile(path.Join(apppath, "routers", "router.go"), strings.Replace(router, "{{.Appname}}", strings.Join(strings.Split(apppath[len(gosrcpath)+1:], string(path.Separator)), "/"), -1)) WriteToFile(path.Join(apppath, "routers", "router.go"), strings.Replace(router, "{{.Appname}}", packpath, -1))
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "tests", "default_test.go"), "\x1b[0m") fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "tests", "default_test.go"), "\x1b[0m")
WriteToFile(path.Join(apppath, "tests", "default_test.go"), strings.Replace(test, "{{.Appname}}", strings.Join(strings.Split(apppath[len(gosrcpath)+1:], string(path.Separator)), "/"), -1)) WriteToFile(path.Join(apppath, "tests", "default_test.go"), strings.Replace(test, "{{.Appname}}", packpath, -1))
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "main.go"), "\x1b[0m") fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "main.go"), "\x1b[0m")
WriteToFile(path.Join(apppath, "main.go"), strings.Replace(maingo, "{{.Appname}}", strings.Join(strings.Split(apppath[len(gosrcpath)+1:], string(path.Separator)), "/"), -1)) WriteToFile(path.Join(apppath, "main.go"), strings.Replace(maingo, "{{.Appname}}", packpath, -1))
ColorLog("[SUCC] New application successfully created!\n") ColorLog("[SUCC] New application successfully created!\n")
return 0 return 0