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
1 changed files with 34 additions and 18 deletions

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)
if err != nil { go func() {
logger.Fatalf("Cannot read directory '%s': %s", path, err) filepath.Walk(dirpath, func(fpath string, fileInfo os.FileInfo, err error) error {
} if err != nil {
for _, item := range list { return nil
if item.IsDir() && item.Name() != "vendor" { }
parsePackagesFromDir(path + "/" + item.Name()) 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() 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)
} }
@ -147,13 +163,13 @@ func generateDocs(curpath string) {
rootapi.Infos.Contact.URL = strings.TrimSpace(s[len("@URL"):]) rootapi.Infos.Contact.URL = strings.TrimSpace(s[len("@URL"):])
} else if strings.HasPrefix(s, "@LicenseUrl") { } else if strings.HasPrefix(s, "@LicenseUrl") {
if rootapi.Infos.License == nil { 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 { } 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") { } else if strings.HasPrefix(s, "@License") {
if rootapi.Infos.License == nil { 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 { } else {
rootapi.Infos.License.Name = strings.TrimSpace(s[len("@License"):]) rootapi.Infos.License.Name = strings.TrimSpace(s[len("@License"):])
} }