From 287af20d1a3e4c0a263f7848009f7b8aa20fc1b6 Mon Sep 17 00:00:00 2001 From: Faissal Elamraoui Date: Mon, 26 Sep 2016 20:25:15 +0200 Subject: [PATCH 1/2] fix: This fixes #247 Moved isBeegoProject(app) method from pack.go to util.go. Rewrite it to walk the application folder tree looking for main files. --- pack.go | 19 ------------------- util.go | 39 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/pack.go b/pack.go index bdf3581..d6d84e4 100644 --- a/pack.go +++ b/pack.go @@ -21,7 +21,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "os" "os/exec" path "path/filepath" @@ -454,24 +453,6 @@ func packDirectory(excludePrefix []string, excludeSuffix []string, return } -func isBeegoProject(thePath string) bool { - fh, _ := os.Open(thePath) - fis, _ := fh.Readdir(-1) - regex := regexp.MustCompile(`(?s)package main.*?import.*?\(.*?github.com/astaxie/beego".*?\).*func main()`) - for _, fi := range fis { - if fi.IsDir() == false && strings.HasSuffix(fi.Name(), ".go") { - data, err := ioutil.ReadFile(path.Join(thePath, fi.Name())) - if err != nil { - continue - } - if len(regex.Find(data)) > 0 { - return true - } - } - } - return false -} - func packApp(cmd *Command, args []string) int { ShowShortVersionBanner() diff --git a/util.go b/util.go index fd2e1ce..32db2f5 100644 --- a/util.go +++ b/util.go @@ -15,14 +15,16 @@ package main import ( + "fmt" + "io/ioutil" "log" "os" + "path" "path/filepath" + "regexp" "runtime" "strings" "time" - "path" - "fmt" ) // Go is a basic promise implementation: it wraps calls a function in a goroutine @@ -35,7 +37,7 @@ func Go(f func() error) chan error { return ch } -// if os.env DEBUG set, debug is on +// Debugf outputs a formtted debug message, when os.env DEBUG is set. func Debugf(format string, a ...interface{}) { if os.Getenv("DEBUG") != "" { _, file, line, ok := runtime.Caller(1) @@ -174,6 +176,37 @@ func GetGOPATHs() []string { return paths } +func isBeegoProject(thePath string) bool { + mainFiles := []string{} + hasBeegoRegex := regexp.MustCompile(`(?s)package main.*?import.*?\(.*?github.com/astaxie/beego".*?\).*func main()`) + // Walk the application path tree to look for main files. + // Main files must satisfy the 'hasBeegoRegex' regular expression. + err := filepath.Walk(thePath, func(fpath string, f os.FileInfo, err error) error { + if !f.IsDir() { // Skip sub-directories + data, _err := ioutil.ReadFile(fpath) + if _err != nil { + return _err + } + if len(hasBeegoRegex.Find(data)) > 0 { + mainFiles = append(mainFiles, fpath) + } + } + return nil + }) + + if err != nil { + log.Fatalf("Unable to walk '%s' tree: %v", thePath, err) + return false + } + + if len(mainFiles) > 0 { + return true + } + return false +} + +// SearchGOPATHs searchs the user GOPATH(s) for the specified application name. +// It returns a boolean, the application's GOPATH and its full path. func SearchGOPATHs(app string) (bool, string, string) { gps := GetGOPATHs() if len(gps) == 0 { From 56568f4d2c60703d10b3bfb4e7326a9a6244a639 Mon Sep 17 00:00:00 2001 From: Faissal Elamraoui Date: Mon, 26 Sep 2016 20:32:14 +0200 Subject: [PATCH 2/2] tweak: Exit with message instead of a panic In case no application is found in GOPATH(s), exit with a friendly message instead of a panic. Also removed useless else block in watch.go. --- run.go | 8 ++++---- watch.go | 3 +-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/run.go b/run.go index 8aab6b5..b84e199 100644 --- a/run.go +++ b/run.go @@ -87,7 +87,7 @@ func runApp(cmd *Command, args []string) int { currentGoPath = _gopath appname = path.Base(currpath) } else { - panic(fmt.Sprintf("No Beego application '%s' found in your GOPATH", args[0])) + exitPrint(fmt.Sprintf("No Beego application '%s' found in your GOPATH", args[0])) } ColorLog("[INFO] Using '%s' as 'appname'\n", appname) @@ -103,13 +103,13 @@ func runApp(cmd *Command, args []string) int { Debugf("current path:%s\n", currpath) - if runmode == "prod" || runmode == "dev"{ + if runmode == "prod" || runmode == "dev" { os.Setenv("BEEGO_RUNMODE", runmode) ColorLog("[INFO] Using '%s' as 'runmode'\n", os.Getenv("BEEGO_RUNMODE")) - }else if runmode != ""{ + } else if runmode != "" { os.Setenv("BEEGO_RUNMODE", runmode) ColorLog("[WARN] Using '%s' as 'runmode'\n", os.Getenv("BEEGO_RUNMODE")) - }else if os.Getenv("BEEGO_RUNMODE") != ""{ + } else if os.Getenv("BEEGO_RUNMODE") != "" { ColorLog("[WARN] Using '%s' as 'runmode'\n", os.Getenv("BEEGO_RUNMODE")) } diff --git a/watch.go b/watch.go index efde48a..1c8f593 100644 --- a/watch.go +++ b/watch.go @@ -240,9 +240,8 @@ func shouldIgnoreFile(filename string) bool { } if r.MatchString(filename) { return true - } else { - continue } + continue } return false }