1
0
mirror of https://github.com/beego/bee.git synced 2024-11-26 01:11:30 +00:00

Enhances the package dir parser

The function parsePackagesFromDir() was panicing when it encounter some
"invalid" Go files. Instead, this will allow it to display a warning and
continue the execution of the command.
This commit is contained in:
Faissal Elamraoui 2016-11-20 11:46:42 +01:00
parent eac9dc25d3
commit ab39926c41

View File

@ -32,8 +32,6 @@ import (
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"io/ioutil"
"github.com/astaxie/beego/swagger" "github.com/astaxie/beego/swagger"
"github.com/astaxie/beego/utils" "github.com/astaxie/beego/utils"
) )
@ -87,39 +85,57 @@ func init() {
parsePackagesFromDir(curPath) parsePackagesFromDir(curPath)
} }
func parsePackagesFromDir(path string) { func parsePackagesFromDir(dirpath string) {
parsePackageFromDir(path) c := make(chan error)
list, err := ioutil.ReadDir(path)
go func() {
filepath.Walk(dirpath, func(fpath string, fileInfo os.FileInfo, err error) error {
if err != nil { if err != nil {
logger.Fatalf("Cannot read directory '%s': %s", path, err) return nil
} }
for _, item := range list { if !fileInfo.IsDir() {
if item.IsDir() && item.Name() != "vendor" { return nil
parsePackagesFromDir(path + "/" + item.Name())
} }
if fileInfo.Name() != "vendor" {
err = parsePackageFromDir(fpath)
if err != nil {
// Send the error to through the channel and continue walking
c <- fmt.Errorf("Error while parsing directory: %s", err.Error())
return nil
}
}
return nil
})
close(c)
}()
for err := range c {
logger.Warnf("%s", err)
} }
} }
func parsePackageFromDir(path string) { func parsePackageFromDir(path string) error {
fileSet := token.NewFileSet() fileSet := token.NewFileSet()
folderPkgs, err := parser.ParseDir(fileSet, path, func(info os.FileInfo) bool { folderPkgs, err := parser.ParseDir(fileSet, path, func(info os.FileInfo) bool {
name := info.Name() name := info.Name()
return !info.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go") return !info.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go")
}, parser.ParseComments) }, parser.ParseComments)
if err != nil { if err != nil {
logger.Fatalf("Error while parsing dir at '%s': %s", path, err) return err
} }
for k, v := range folderPkgs { for k, v := range folderPkgs {
astPkgs[k] = v astPkgs[k] = v
} }
return nil
} }
func generateDocs(curpath string) { func generateDocs(curpath string) {
fset := token.NewFileSet() fset := token.NewFileSet()
f, err := parser.ParseFile(fset, path.Join(curpath, "routers", "router.go"), nil, parser.ParseComments) f, err := parser.ParseFile(fset, path.Join(curpath, "routers", "router.go"), nil, parser.ParseComments)
if err != nil { if err != nil {
logger.Fatalf("Error while parsing router.go: %s", err) logger.Fatalf("Error while parsing router.go: %s", err)
} }