feat: support generating Swagger docs by tags (#4)

This commit is contained in:
xiao-kong-long
2024-01-21 16:57:08 +08:00
committed by GitHub
parent ce32a9616d
commit 9d16ab1b8b
2 changed files with 50 additions and 2 deletions

View File

@ -81,12 +81,30 @@ func GenerateCode(cmd *commands.Command, args []string) int {
beeLogger.Log.Fatal("Command is missing") beeLogger.Log.Fatal("Command is missing")
} }
// get tags value
tagsIndex := -1
for i, arg := range args {
if arg == "--tags" && i+1 < len(args) {
tagsIndex = i + 1
break
}
}
var tags []string
if tagsIndex != -1 {
tags = strings.Split(args[tagsIndex], ",")
for i, str := range tags {
tags[i] = strings.TrimSpace(str)
}
} else {
tags = []string{}
}
gcmd := args[0] gcmd := args[0]
switch gcmd { switch gcmd {
case "scaffold": case "scaffold":
scaffold(cmd, args, currpath) scaffold(cmd, args, currpath)
case "docs": case "docs":
swaggergen.GenerateDocs(currpath) swaggergen.GenerateDocs(currpath, tags)
case "appcode": case "appcode":
appCode(cmd, args, currpath) appCode(cmd, args, currpath)
case "migration": case "migration":

View File

@ -157,7 +157,7 @@ func parsePackageFromDir(path string) error {
} }
// GenerateDocs generates documentations for a given path. // GenerateDocs generates documentations for a given path.
func GenerateDocs(curpath string) { func GenerateDocs(curpath string, tags []string) {
pkgspath := curpath pkgspath := curpath
workspace := os.Getenv("BeeWorkspace") workspace := os.Getenv("BeeWorkspace")
if workspace != "" { if workspace != "" {
@ -272,6 +272,36 @@ func GenerateDocs(curpath string) {
} }
analyseControllerPkg(localName, im.Path.Value) analyseControllerPkg(localName, im.Path.Value)
} }
// fileter the controllerList by tags
if len(tags)>0 {
for _, controllerValue := range controllerList {
for routerPath, item := range controllerValue {
var tags_ []string
if item.Get != nil {
tags_ = item.Get.Tags
} else {
tags_ = item.Post.Tags
}
isContained := false
for _, tag := range tags {
for _, tag_ := range tags_{
if strings.EqualFold(tag, tag_) {
isContained = true
break
}
}
if isContained {
break
}
}
if !isContained {
delete(controllerValue, routerPath)
}
}
}
}
for _, d := range f.Decls { for _, d := range f.Decls {
switch specDecl := d.(type) { switch specDecl := d.(type) {
case *ast.FuncDecl: case *ast.FuncDecl: