1
0
mirror of https://github.com/beego/bee.git synced 2024-11-22 10:10:53 +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"
"io/ioutil"
"github.com/astaxie/beego/swagger"
"github.com/astaxie/beego/utils"
)
@ -87,39 +85,57 @@ func init() {
parsePackagesFromDir(curPath)
}
func parsePackagesFromDir(path string) {
parsePackageFromDir(path)
list, err := ioutil.ReadDir(path)
if err != nil {
logger.Fatalf("Cannot read directory '%s': %s", path, err)
}
for _, item := range list {
if item.IsDir() && item.Name() != "vendor" {
parsePackagesFromDir(path + "/" + item.Name())
}
func parsePackagesFromDir(dirpath string) {
c := make(chan error)
go func() {
filepath.Walk(dirpath, func(fpath string, fileInfo os.FileInfo, err error) error {
if err != nil {
return nil
}
if !fileInfo.IsDir() {
return nil
}
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()
folderPkgs, err := parser.ParseDir(fileSet, path, func(info os.FileInfo) bool {
name := info.Name()
return !info.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go")
}, parser.ParseComments)
if err != nil {
logger.Fatalf("Error while parsing dir at '%s': %s", path, err)
return err
}
for k, v := range folderPkgs {
astPkgs[k] = v
}
return nil
}
func generateDocs(curpath string) {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, path.Join(curpath, "routers", "router.go"), nil, parser.ParseComments)
if err != nil {
logger.Fatalf("Error while parsing router.go: %s", err)
}
@ -147,13 +163,13 @@ func generateDocs(curpath string) {
rootapi.Infos.Contact.URL = strings.TrimSpace(s[len("@URL"):])
} else if strings.HasPrefix(s, "@LicenseUrl") {
if rootapi.Infos.License == nil {
rootapi.Infos.License = &swagger.License{URL: strings.TrimSpace(s[len("@LicenseUrl"):])}
rootapi.Infos.License = &swagger.License{URL: strings.TrimSpace(s[len("@LicenseUrl"):])}
} else {
rootapi.Infos.License.URL = strings.TrimSpace(s[len("@LicenseUrl"):])
rootapi.Infos.License.URL = strings.TrimSpace(s[len("@LicenseUrl"):])
}
} else if strings.HasPrefix(s, "@License") {
if rootapi.Infos.License == nil {
rootapi.Infos.License = &swagger.License{Name: strings.TrimSpace(s[len("@License"):])}
rootapi.Infos.License = &swagger.License{Name: strings.TrimSpace(s[len("@License"):])}
} else {
rootapi.Infos.License.Name = strings.TrimSpace(s[len("@License"):])
}