You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.2 KiB
Go
62 lines
1.2 KiB
Go
package docgen
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/tal-tech/go-zero/tools/goctl/api/parser"
|
|
"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)
|
|
}
|
|
|
|
err = os.RemoveAll(dir + "/" + docDir + "/")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, f := range files {
|
|
api, err := parser.Parse(f)
|
|
if err != nil {
|
|
return fmt.Errorf("parse file: %s, err: %s", f, 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
|
|
}
|
|
dir := dst[:index]
|
|
genDoc(api, dir, strings.Replace(dst[index+1:], ".api", ".md", 1))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func filePathWalkDir(root string) ([]string, error) {
|
|
var files []string
|
|
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
|
|
if !info.IsDir() && strings.HasSuffix(path, ".api") {
|
|
files = append(files, path)
|
|
}
|
|
return nil
|
|
})
|
|
return files, err
|
|
}
|