From 5bc1c82a2b0d9aa16172663fd07e63cecb52e094 Mon Sep 17 00:00:00 2001 From: Faissal Elamraoui Date: Fri, 29 Jul 2016 17:45:15 +0200 Subject: [PATCH] Added check for multiple paths in GOPATH (instead of the first path in the list) --- run.go | 32 ++++++++++++++++---------------- util.go | 31 ++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/run.go b/run.go index db1ceed..a3af6c3 100644 --- a/run.go +++ b/run.go @@ -49,6 +49,8 @@ var ( exit chan bool // Flag to watch the vendor folder vendorWatch bool + // Current user workspace + currentGoPath string ) func init() { @@ -65,26 +67,24 @@ func init() { func runApp(cmd *Command, args []string) int { ShowShortVersionBanner() - gps := GetGOPATHs() - if len(gps) == 0 { - ColorLog("[ERRO] Fail to start[ %s ]\n", "$GOPATH is not set or empty") - os.Exit(2) - } - gopath := gps[0] - if len(args) == 0 || args[0] == "watchall" { currpath, _ = os.Getwd() + + if !isBeegoProject(currpath) { + exitPrint(fmt.Sprintf("Bee does not support non Beego project: %s", currpath)) + } + + _, currentGoPath, _ = SearchGOPATHs(currpath) appname = path.Base(currpath) ColorLog("[INFO] Uses '%s' as 'appname'\n", appname) } else { - gopathsrc := path.Join(gopath, "src") - currpath = path.Join(gopathsrc, args[0]) - appname = path.Base(currpath) - - // Check if passed Bee application path/name exists - // in $GOPATH/src workspace - if !isExist(currpath) { - panic(fmt.Sprintf("No Beego application '%s' found in GOPATH: %s", args[0], gopathsrc)) + // Check if passed Bee application path/name exists in the GOPATH(s) + if ok, _gopath, _path := SearchGOPATHs(args[0]); ok { + currpath = _path + currentGoPath = _gopath + appname = path.Base(currpath) + } else { + panic(fmt.Sprintf("No Beego application '%s' found in your GOPATH", args[0])) } ColorLog("[INFO] Uses '%s' as 'appname'\n", appname) @@ -110,7 +110,7 @@ func runApp(cmd *Command, args []string) int { // Because monitor files has some issues, we watch current directory // and ignore non-go files. for _, p := range conf.DirStruct.Others { - paths = append(paths, strings.Replace(p, "$GOPATH", gopath, -1)) + paths = append(paths, strings.Replace(p, "$GOPATH", currentGoPath, -1)) } files := []string{} diff --git a/util.go b/util.go index 0f314df..2cd76d3 100644 --- a/util.go +++ b/util.go @@ -15,13 +15,14 @@ package main import ( - "fmt" "log" "os" "path/filepath" "runtime" "strings" "time" + "path" + "fmt" ) // Go is a basic promise implementation: it wraps calls a function in a goroutine @@ -173,6 +174,34 @@ func GetGOPATHs() []string { return paths } +func SearchGOPATHs(app string) (bool, string, string) { + gps := GetGOPATHs() + if len(gps) == 0 { + ColorLog("[ERRO] Fail to start [ %s ]\n", "GOPATH environment variable is not set or empty") + os.Exit(2) + } + + // Lookup the application inside the user workspace(s) + for _, gopath := range gps { + var currentPath string + + if !strings.Contains(app, "src") { + gopathsrc := path.Join(gopath, "src") + currentPath = path.Join(gopathsrc, app) + } else { + currentPath = app + } + + if isExist(currentPath) { + if !isBeegoProject(currentPath) { + continue + } + return true, gopath, currentPath + } + } + return false, "", "" +} + // 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