diff --git a/tools/goctl/api/docgen/doc.go b/tools/goctl/api/docgen/doc.go index 1f5cb55a3e76..5ad264a14734 100644 --- a/tools/goctl/api/docgen/doc.go +++ b/tools/goctl/api/docgen/doc.go @@ -24,9 +24,10 @@ const ( - Request: {{.requestType}} - Response: {{.responseType}} +2. 请求定义 +{{.requestContent}} -2. 类型定义 - +3. 返回定义 {{.responseContent}} ` @@ -46,7 +47,12 @@ func genDoc(api *spec.ApiSpec, dir string, filename string) error { routeComment = "N/A" } - responseContent, err := responseBody(api, route) + requestContent, err := buildDoc(route.RequestType) + if err != nil { + return err + } + + responseContent, err := buildDoc(route.ResponseType) if err != nil { return err } @@ -60,6 +66,7 @@ func genDoc(api *spec.ApiSpec, dir string, filename string) error { "uri": route.Path, "requestType": "`" + stringx.TakeOne(route.RequestTypeName(), "-") + "`", "responseType": "`" + stringx.TakeOne(route.ResponseTypeName(), "-") + "`", + "requestContent": requestContent, "responseContent": responseContent, }) if err != nil { @@ -72,14 +79,14 @@ func genDoc(api *spec.ApiSpec, dir string, filename string) error { return err } -func responseBody(api *spec.ApiSpec, route spec.Route) (string, error) { - if len(route.ResponseTypeName()) == 0 { +func buildDoc(route spec.Type) (string, error) { + if route == nil || len(route.Name()) == 0 { return "", nil } var tps = make([]spec.Type, 0) - tps = append(tps, route.ResponseType) - if definedType, ok := route.ResponseType.(spec.DefineStruct); ok { + tps = append(tps, route) + if definedType, ok := route.(spec.DefineStruct); ok { associatedTypes(definedType, &tps) } value, err := gogen.BuildTypes(tps) @@ -91,7 +98,17 @@ func responseBody(api *spec.ApiSpec, route spec.Route) (string, error) { } func associatedTypes(tp spec.DefineStruct, tps *[]spec.Type) { - *tps = append(*tps, tp) + var hasAdded = false + for _, item := range *tps { + if item.Name() == tp.Name() { + hasAdded = true + break + } + } + if !hasAdded { + *tps = append(*tps, tp) + } + for _, item := range tp.Members { if definedType, ok := item.Type.(spec.DefineStruct); ok { associatedTypes(definedType, tps) diff --git a/tools/goctl/api/docgen/gen.go b/tools/goctl/api/docgen/gen.go index c0fb8dad3d27..840ab8a89941 100644 --- a/tools/goctl/api/docgen/gen.go +++ b/tools/goctl/api/docgen/gen.go @@ -8,43 +8,50 @@ import ( "strings" "github.com/tal-tech/go-zero/tools/goctl/api/parser" + "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/urfave/cli" ) -var docDir = "doc" - func DocCommand(c *cli.Context) error { dir := c.String("dir") if len(dir) == 0 { return errors.New("missing -dir") } - files, err := filePathWalkDir(dir) + outputDir := c.String("o") + if len(outputDir) == 0 { + var err error + outputDir, err = os.Getwd() + if err != nil { + return err + } + } + + if !util.FileExists(dir) { + return errors.New(fmt.Sprintf("dir %s not exsit", dir)) + } + + dir, err := filepath.Abs(dir) if err != nil { - return fmt.Errorf("dir %s not exist", dir) + return err } - err = os.RemoveAll(dir + "/" + docDir + "/") + files, err := filePathWalkDir(dir) if err != nil { return err } - for _, f := range files { - api, err := parser.Parse(f) + + for _, path := range files { + api, err := parser.Parse(path) if err != nil { - return fmt.Errorf("parse file: %s, err: %s", f, err.Error()) + return fmt.Errorf("parse file: %s, err: %s", path, err.Error()) } - index := strings.Index(f, dir) - if index < 0 { - continue - } - dst := dir + "/" + docDir + f[index+len(dir):] - index = strings.LastIndex(dst, "/") - if index < 0 { - continue + err = genDoc(api, filepath.Dir(filepath.Join(outputDir, path[len(dir):])), + strings.Replace(path[len(filepath.Dir(path)):], ".api", ".md", 1)) + if err != nil { + return err } - dir := dst[:index] - genDoc(api, dir, strings.Replace(dst[index+1:], ".api", ".md", 1)) } return nil } diff --git a/tools/goctl/goctl.go b/tools/goctl/goctl.go index 997a40f7197c..66b0219d7635 100644 --- a/tools/goctl/goctl.go +++ b/tools/goctl/goctl.go @@ -89,6 +89,11 @@ var ( Name: "dir", Usage: "the target dir", }, + cli.StringFlag{ + Name: "o", + Required: false, + Usage: "the output markdown directory", + }, }, Action: docgen.DocCommand, },