-
Notifications
You must be signed in to change notification settings - Fork 804
/
main.go
227 lines (196 loc) · 6.36 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package main
import (
"bytes"
"flag"
"fmt"
"os"
"path"
"path/filepath"
"github.com/Jeffail/benthos/v3/internal/bundle"
"github.com/Jeffail/benthos/v3/internal/docs"
"github.com/Jeffail/benthos/v3/internal/template"
"github.com/Jeffail/benthos/v3/lib/buffer"
"github.com/Jeffail/benthos/v3/lib/cache"
"github.com/Jeffail/benthos/v3/lib/condition"
"github.com/Jeffail/benthos/v3/lib/input"
"github.com/Jeffail/benthos/v3/lib/metrics"
"github.com/Jeffail/benthos/v3/lib/output"
"github.com/Jeffail/benthos/v3/lib/processor"
"github.com/Jeffail/benthos/v3/lib/ratelimit"
"github.com/Jeffail/benthos/v3/lib/tracer"
"github.com/Jeffail/benthos/v3/lib/util/config"
_ "github.com/Jeffail/benthos/v3/public/components/all"
)
//------------------------------------------------------------------------------
func create(t, path string, resBytes []byte) {
if existing, err := os.ReadFile(path); err == nil {
if bytes.Equal(existing, resBytes) {
return
}
}
if err := os.WriteFile(path, resBytes, 0o644); err != nil {
panic(err)
}
fmt.Printf("Documentation for '%v' has changed, updating: %v\n", t, path)
}
func render(dir string, embed bool, confSanit interface{}, spec docs.ComponentSpec) {
if s, ok := confSanit.(config.Sanitised); ok {
confSanit = map[string]interface{}(s)
}
mdSpec, err := spec.AsMarkdown(embed, confSanit)
if err != nil {
panic(fmt.Sprintf("Failed to generate docs for '%v': %v", spec.Name, err))
}
create(spec.Name, dir, mdSpec)
}
func main() {
docsDir := "./website/docs/components"
flag.StringVar(&docsDir, "dir", docsDir, "The directory to write docs to")
flag.Parse()
if _, err := template.InitTemplates(); err != nil {
panic(err)
}
doInputs(docsDir)
doBuffers(docsDir)
doCaches(docsDir)
// Note, disabling condition docs generation now as a convenience, but we
// can add it back in if there are automated changes required.
// TODO: V4 Delete entirely
// doConditions(docsDir)
doMetrics(docsDir)
doOutputs(docsDir)
doProcessors(docsDir)
doRateLimits(docsDir)
doTracers(docsDir)
// Bloblang stuff
doBloblang(docsDir)
// Template docs
doTemplates(docsDir)
}
func doInputs(docsDir string) {
for _, v := range bundle.AllInputs.Docs() {
conf := input.NewConfig()
conf.Type = v.Name
confSanit, err := input.SanitiseConfig(conf)
if err != nil {
panic(fmt.Sprintf("Failed to generate docs for '%v': %v", v.Name, err))
}
render(path.Join(docsDir, "./inputs", v.Name+".md"), true, confSanit, v)
}
}
func doBuffers(docsDir string) {
for _, v := range bundle.AllBuffers.Docs() {
conf := buffer.NewConfig()
conf.Type = v.Name
confSanit, err := buffer.SanitiseConfig(conf)
if err != nil {
panic(fmt.Sprintf("Failed to generate docs for '%v': %v", v.Name, err))
}
render(path.Join(docsDir, "./buffers", v.Name+".md"), true, confSanit, v)
}
}
func doCaches(docsDir string) {
for _, v := range bundle.AllCaches.Docs() {
conf := cache.NewConfig()
conf.Type = v.Name
confSanit, err := cache.SanitiseConfig(conf)
if err != nil {
panic(fmt.Sprintf("Failed to generate docs for '%v': %v", v.Name, err))
}
render(path.Join(docsDir, "./caches", v.Name+".md"), false, confSanit, v)
}
}
func doConditions(docsDir string) {
for k, v := range condition.Constructors {
spec := docs.ComponentSpec{
Type: "condition",
Name: k,
Summary: v.Summary,
Description: v.Description,
Footnotes: v.Footnotes,
Config: docs.FieldComponent().WithChildren(v.FieldSpecs...),
Status: v.Status,
}
conf := condition.NewConfig()
conf.Type = k
confSanit, err := condition.SanitiseConfig(conf)
if err != nil {
panic(fmt.Sprintf("Failed to generate docs for '%v': %v", k, err))
}
render(path.Join(docsDir, "./conditions", k+".md"), false, confSanit, spec)
}
}
func doMetrics(docsDir string) {
for _, v := range bundle.AllMetrics.Docs() {
conf := metrics.NewConfig()
conf.Type = v.Name
confSanit, err := metrics.SanitiseConfig(conf)
if err != nil {
panic(fmt.Sprintf("Failed to generate docs for '%v': %v", v.Name, err))
}
render(path.Join(docsDir, "./metrics", v.Name+".md"), true, confSanit, v)
}
}
func doOutputs(docsDir string) {
for _, v := range bundle.AllOutputs.Docs() {
conf := output.NewConfig()
conf.Type = v.Name
confSanit, err := output.SanitiseConfig(conf)
if err != nil {
panic(fmt.Sprintf("Failed to generate docs for '%v': %v", v.Name, err))
}
render(path.Join(docsDir, "./outputs", v.Name+".md"), true, confSanit, v)
}
}
func doProcessors(docsDir string) {
for _, v := range bundle.AllProcessors.Docs() {
conf := processor.NewConfig()
conf.Type = v.Name
confSanit, err := processor.SanitiseConfig(conf)
if err != nil {
panic(fmt.Sprintf("Failed to generate docs for '%v': %v", v.Name, err))
}
render(path.Join(docsDir, "./processors", v.Name+".md"), false, confSanit, v)
}
}
func doRateLimits(docsDir string) {
for _, v := range bundle.AllRateLimits.Docs() {
conf := ratelimit.NewConfig()
conf.Type = v.Name
confSanit, err := ratelimit.SanitiseConfig(conf)
if err != nil {
panic(fmt.Sprintf("Failed to generate docs for '%v': %v", v.Name, err))
}
render(path.Join(docsDir, "./rate_limits", v.Name+".md"), false, confSanit, v)
}
}
func doTracers(docsDir string) {
for _, v := range bundle.AllTracers.Docs() {
conf := tracer.NewConfig()
conf.Type = v.Name
confSanit, err := tracer.SanitiseConfig(conf)
if err != nil {
panic(fmt.Sprintf("Failed to generate docs for '%v': %v", v.Name, err))
}
render(path.Join(docsDir, "./tracers", v.Name+".md"), true, confSanit, v)
}
}
func doBloblang(dir string) {
mdSpec, err := docs.BloblangFunctionsMarkdown()
if err != nil {
panic(fmt.Sprintf("Failed to generate docs for bloblang functions: %v", err))
}
create("bloblang functions", filepath.Join(dir, "..", "guides", "bloblang", "functions.md"), mdSpec)
if mdSpec, err = docs.BloblangMethodsMarkdown(); err != nil {
panic(fmt.Sprintf("Failed to generate docs for bloblang methods: %v", err))
}
create("bloblang methods", filepath.Join(dir, "..", "guides", "bloblang", "methods.md"), mdSpec)
}
func doTemplates(dir string) {
mdSpec, err := template.DocsMarkdown()
if err != nil {
panic(fmt.Sprintf("Failed to generate docs for templates: %v", err))
}
create("template docs", filepath.Join(dir, "..", "configuration", "templating.md"), mdSpec)
}
//------------------------------------------------------------------------------