Merge pull request #287 from amrfaissal/fix-multiple-main

Fix & Tweaks
This commit is contained in:
astaxie 2016-09-27 10:04:17 +08:00 committed by GitHub
commit 178e74db40
4 changed files with 41 additions and 28 deletions

19
pack.go
View File

@ -21,7 +21,6 @@ import (
"flag" "flag"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
path "path/filepath" path "path/filepath"
@ -454,24 +453,6 @@ func packDirectory(excludePrefix []string, excludeSuffix []string,
return 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 { func packApp(cmd *Command, args []string) int {
ShowShortVersionBanner() ShowShortVersionBanner()

8
run.go
View File

@ -87,7 +87,7 @@ func runApp(cmd *Command, args []string) int {
currentGoPath = _gopath currentGoPath = _gopath
appname = path.Base(currpath) appname = path.Base(currpath)
} else { } 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) 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) Debugf("current path:%s\n", currpath)
if runmode == "prod" || runmode == "dev"{ if runmode == "prod" || runmode == "dev" {
os.Setenv("BEEGO_RUNMODE", runmode) os.Setenv("BEEGO_RUNMODE", runmode)
ColorLog("[INFO] Using '%s' as 'runmode'\n", os.Getenv("BEEGO_RUNMODE")) ColorLog("[INFO] Using '%s' as 'runmode'\n", os.Getenv("BEEGO_RUNMODE"))
}else if runmode != ""{ } else if runmode != "" {
os.Setenv("BEEGO_RUNMODE", runmode) os.Setenv("BEEGO_RUNMODE", runmode)
ColorLog("[WARN] Using '%s' as 'runmode'\n", os.Getenv("BEEGO_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")) ColorLog("[WARN] Using '%s' as 'runmode'\n", os.Getenv("BEEGO_RUNMODE"))
} }

39
util.go
View File

@ -15,14 +15,16 @@
package main package main
import ( import (
"fmt"
"io/ioutil"
"log" "log"
"os" "os"
"path"
"path/filepath" "path/filepath"
"regexp"
"runtime" "runtime"
"strings" "strings"
"time" "time"
"path"
"fmt"
) )
// Go is a basic promise implementation: it wraps calls a function in a goroutine // 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 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{}) { func Debugf(format string, a ...interface{}) {
if os.Getenv("DEBUG") != "" { if os.Getenv("DEBUG") != "" {
_, file, line, ok := runtime.Caller(1) _, file, line, ok := runtime.Caller(1)
@ -174,6 +176,37 @@ func GetGOPATHs() []string {
return paths 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) { func SearchGOPATHs(app string) (bool, string, string) {
gps := GetGOPATHs() gps := GetGOPATHs()
if len(gps) == 0 { if len(gps) == 0 {

View File

@ -240,9 +240,8 @@ func shouldIgnoreFile(filename string) bool {
} }
if r.MatchString(filename) { if r.MatchString(filename) {
return true return true
} else {
continue
} }
continue
} }
return false return false
} }