Code optimized (#474)

* optimized markdown generator

* optimized markdown generator

* optimized markdown generator

* optimized markdown generator
This commit is contained in:
kingxt
2021-02-18 15:08:20 +08:00
committed by GitHub
parent 8f1c88e07d
commit f14ab70035
3 changed files with 56 additions and 27 deletions

View File

@@ -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)
if err != nil {
return fmt.Errorf("dir %s not exist", dir)
outputDir := c.String("o")
if len(outputDir) == 0 {
var err error
outputDir, err = os.Getwd()
if err != nil {
return err
}
}
err = os.RemoveAll(dir + "/" + docDir + "/")
if !util.FileExists(dir) {
return errors.New(fmt.Sprintf("dir %s not exsit", dir))
}
dir, err := filepath.Abs(dir)
if err != nil {
return err
}
for _, f := range files {
api, err := parser.Parse(f)
files, err := filePathWalkDir(dir)
if err != nil {
return err
}
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
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
}
dst := dir + "/" + docDir + f[index+len(dir):]
index = strings.LastIndex(dst, "/")
if index < 0 {
continue
}
dir := dst[:index]
genDoc(api, dir, strings.Replace(dst[index+1:], ".api", ".md", 1))
}
return nil
}