-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
105 lines (91 loc) · 2.19 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"golang.org/x/mod/modfile"
)
var (
dstModPath string
dstTypes []string
)
var (
fileName = flag.String("f", "", "file path")
title = flag.String("t", "", "file title")
tagType = flag.String("tag", "json", "struct tag name")
showVersion = flag.Bool("v", false, "show version")
)
func main() {
flag.Parse()
if *showVersion {
PrintVersion()
os.Exit(0)
}
dstModPath = os.Args[len(os.Args)-2]
dstTypes = strings.Split(os.Args[len(os.Args)-1], ",")
rootMod, _, _ := getModPath()
log.Println("Current Module:", rootMod)
parser, err := NewParser(*tagType, dstModPath)
if err != nil {
log.Fatalf("new parser %v", err)
return
}
for _, tp := range dstTypes {
log.Printf("start generate %s.%s\n", dstModPath, tp)
fs := parser.Parse(dstModPath, tp)
md := Markdown{
Title: *title,
MainStructName: dstModPath + "." + tp,
ObjTitleFunc: func(modPath string, typeName string) string {
modPath = strings.TrimPrefix(modPath, dstModPath)
modPath = strings.TrimPrefix(modPath, rootMod)
modPath = strings.TrimLeft(modPath, "./")
if modPath == "" {
return typeName
} else {
return fmt.Sprintf("%s.%s", modPath, typeName)
}
},
}
if md.Title == "" {
md.Title = tp + " Doc"
}
data := md.Generate(fs)
filename := *fileName
if filename == "" {
filename = tp + "_doc.md"
}
_ = os.MkdirAll(filepath.Dir(filename), 0755)
log.Printf("start to save to %s\n", filename)
_ = os.WriteFile(filename, data, 0655)
}
}
func getModPath() (string, string, error) {
current, _ := os.Getwd()
var path, _ = filepath.Abs(current)
for {
_, err := os.Stat(filepath.Join(path, "go.mod"))
if err != nil {
if !os.IsNotExist(err) {
return "", "", err
}
path, _ = filepath.Abs(filepath.Join(path, "../"))
if path == "" {
return "", "", nil
}
continue
}
content, err := os.ReadFile(filepath.Join(path, "go.mod"))
if err != nil {
return "", "", err
}
f, err := modfile.Parse("go.mod", content, nil)
if err != nil {
return "", "", err
}
return f.Module.Mod.Path, strings.Replace(current, path, f.Module.Mod.Path, 1), nil
}
}