-
Notifications
You must be signed in to change notification settings - Fork 31
/
gen_config_doc.go
95 lines (75 loc) · 1.62 KB
/
gen_config_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//go:build ignore
// +build ignore
package main
import (
"log"
"os"
"path"
"strings"
"text/template"
"time"
"github.com/choria-io/go-choria/build"
"github.com/choria-io/go-choria/config"
"github.com/choria-io/go-choria/confkey"
"github.com/choria-io/go-choria/internal/fs"
"github.com/choria-io/go-choria/internal/util"
)
type configs struct {
Keys [][]string
Docs []*confkey.Doc
Now string
Version string
}
func panicIfErr(err error) {
if err != nil {
panic(err)
}
}
func main() {
cfg, err := config.NewDefaultConfig()
panicIfErr(err)
keys, err := cfg.ConfigKeys(".")
panicIfErr(err)
if len(keys) == 0 {
panic("no configuration keys found")
}
docs := configs{
Docs: []*confkey.Doc{},
Now: time.Now().UTC().Format(time.RFC822),
Version: build.Version,
}
util.SliceGroups(keys, 2, func(grp []string) {
docs.Keys = append(docs.Keys, grp)
})
for _, key := range keys {
doc := cfg.DocForConfigKey(key)
if doc == nil {
continue
}
docs.Docs = append(docs.Docs, doc)
}
if len(docs.Docs) == 0 {
panic("no documentation strings were generated")
}
funcs := template.FuncMap{
"gha": func(s string) string {
return strings.ReplaceAll(s, ".", "")
},
}
templ, err := fs.FS.ReadFile("misc/config_doc.templ")
if err != nil {
return
}
t, err := template.New("templates").Funcs(funcs).Delims("<<", ">>").Parse(string(templ))
if err != nil {
panic(err)
}
outfile := "docs/content/configuration/_index.md"
out, err := os.Create(path.Join(outfile))
if err != nil {
panic(err)
}
defer out.Close()
err = t.Execute(out, docs)
log.Println("Generated " + outfile)
}