-
Notifications
You must be signed in to change notification settings - Fork 7
/
docs.go
58 lines (51 loc) · 1.31 KB
/
docs.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
package plugin
import (
"fmt"
"os"
"text/template"
"time"
"github.com/Masterminds/sprig/v3"
"github.com/cludden/protoc-gen-go-temporal/internal/plugin/docs"
"github.com/cludden/protoc-gen-go-temporal/internal/templates"
"github.com/gosimple/slug"
"github.com/hako/durafmt"
"google.golang.org/protobuf/compiler/protogen"
)
func renderDocs(p *protogen.Plugin, cfg *Config) error {
data, err := docs.Parse(p)
if err != nil {
return fmt.Errorf("error extracting template data: %v", err)
}
var raw []byte
switch cfg.DocsTemplate {
case "basic":
raw, err = templates.Templates.ReadFile("basic.tpl")
default:
raw, err = os.ReadFile(cfg.DocsTemplate)
}
if err != nil {
return fmt.Errorf("error reading template: %v", err)
}
tpl, err := template.New("docs").
Funcs(template.FuncMap{
"fmtduration": func(d time.Duration) string {
return durafmt.Parse(d).String()
},
"slug": func(input string) string {
return slug.Make(input)
},
}).
Funcs(sprig.FuncMap()).
Parse(string(raw))
if err != nil {
return fmt.Errorf("error parsing template: %v", err)
}
docs, err := os.Create(cfg.DocsOut)
if err != nil {
return fmt.Errorf("error creating doc file: %w", err)
}
if err := tpl.Execute(docs, data); err != nil {
return fmt.Errorf("error executing template: %v", err)
}
return nil
}