forked from s-mang/test2doc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc.go
79 lines (63 loc) · 1.32 KB
/
doc.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
package doc
import (
"bytes"
"errors"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/happyreturns/test2doc/doc/parse"
)
var (
docTmpl *template.Template
docFmt = `{{with .Group}}{{.Render}}{{end}}`
)
func init() {
docTmpl = template.Must(template.New("doc").Parse(docFmt))
}
type Doc struct {
Group ResourceGroup
file *os.File
}
type Metadata struct {
Format string
Host string
}
func NewDoc(pkgDir string) (doc *Doc, err error) {
pkgDoc, err := parse.NewPackageDoc(pkgDir)
if err != nil {
return doc, err
} else if pkgDoc == nil {
return doc, errors.New("Found 0 packages, expected 1.")
}
fiPath := filepath.Join(pkgDir, pkgDoc.Name+".apib")
fi, err := os.Create(fiPath)
if err != nil {
return doc, err
}
doc = &Doc{
Group: ResourceGroup{
Title: strings.Title(pkgDoc.Name),
},
file: fi,
}
return
}
// TODO: add Resource to appropriate ResourceGroup,
// not just to ResourceGroups[0]
func (d *Doc) AddResource(resource *Resource) {
d.Group.Resources = append(d.Group.Resources, *resource)
}
func (d *Doc) Write() error {
return docTmpl.Execute(d.file, d)
}
func getPayload(req *http.Request) (body []byte, err error) {
body, err = ioutil.ReadAll(req.Body)
if err != nil {
return
}
req.Body = ioutil.NopCloser(bytes.NewBuffer(body))
return
}