-
Notifications
You must be signed in to change notification settings - Fork 2
/
update.go
64 lines (59 loc) · 1.97 KB
/
update.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
package main
import (
"github.com/whaios/goshowdoc/log"
"github.com/whaios/goshowdoc/parser"
"github.com/whaios/goshowdoc/runapi"
)
// Update 更新文档
func Update(searchDir string) {
log.Info("解析Go源码文件 %s", searchDir)
p := parser.NewParser()
if err := p.ParseApiDoc(searchDir); err != nil {
log.Error(err.Error())
return
}
max := len(p.Docs)
for i, doc := range p.Docs {
if err := runapi.UpdateByApi(doc.Catalog, doc.Title, doc.Order, apiDocToPageContent(doc).String()); err != nil {
log.Error("更新文档[%s/%s]失败: %s", doc.Catalog, doc.Title, err.Error())
return
}
log.DrawProgressBar("更新文档", i+1, max)
}
log.Success("更新完成")
return
}
func apiDocToPageContent(doc *parser.ApiDoc) *runapi.PageContent {
content := runapi.NewPageContent(doc.Request.Method, doc.Request.Url)
content.Info.Title = doc.Title
content.Info.Description = doc.Description
content.Info.Remark = doc.Remark
content.Info.ApiStatus = doc.Request.ApiStatus
if len(doc.Request.Headers) > 0 {
content.Request.Headers = doc.Request.Headers
}
content.Request.PathVariable = doc.Request.PathVariable
content.SetQuery(doc.Request.Query)
content.Request.Params.Mode = doc.Request.ParamMode
if len(doc.Request.Params) > 0 {
switch content.Request.Params.Mode {
case runapi.ParamModeUrlEncoded:
content.Request.Params.Urlencoded = doc.Request.Params
case runapi.ParamModeFormData:
content.Request.Params.Formdata = doc.Request.Params
case runapi.ParamModeJson:
content.Request.Params.JsonDesc = doc.Request.Params
}
}
content.Request.Params.Json = doc.Request.ParamJson
content.Response.ResponseExample = doc.Response.Example
if len(doc.Response.Params) > 0 {
content.Response.ResponseParamsDesc = doc.Response.Params
}
content.Response.ResponseFailExample = doc.ResponseFail.Example
if len(doc.ResponseFail.Params) > 0 {
content.Response.ResponseFailParamsDesc = doc.ResponseFail.Params
}
content.Response.Remark = doc.Remark
return content
}