-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
167 lines (140 loc) · 3.13 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
package main
import (
"bytes"
"embed"
"fmt"
"os"
"regexp"
"strings"
"text/template"
"github.com/Masterminds/sprig/v3"
"github.com/futurice/jalapeno/internal/cli"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
type Flag struct {
Name string
Shorthand string
Default string
Description string
Type string
}
type CommandInfo struct {
Name string
Aliases []string
Description string
Usage string
Example string
Flags []Flag
}
//go:embed all:templates
var tmpls embed.FS
const (
referenceDocPath = "./docs/site/docs/api.mdx"
changelogSource = "./CHANGELOG.md"
changelogTarget = "./docs/site/docs/changelog.mdx"
)
var templates = template.Must(template.
New("doc").
Funcs(sprig.FuncMap()).
ParseFS(tmpls, "templates/*"),
)
// This is the entrypoint for generating API reference documentation
func main() {
err := GenerateReferenceDoc()
checkErr(err)
fmt.Println("Reference documentation generated")
err = GenerateChangelog()
checkErr(err)
fmt.Println("Changelog generated")
}
func GenerateChangelog() error {
changelog, err := os.ReadFile(changelogSource)
if err != nil {
return err
}
var b bytes.Buffer
err = templates.ExecuteTemplate(&b, "changelog.tmpl", map[string]interface{}{
"Changelog": string(changelog),
})
if err != nil {
return err
}
// Write the changelog to the target file
return os.WriteFile(changelogTarget, b.Bytes(), 0644)
}
func GenerateReferenceDoc() error {
rootCmd := cli.NewRootCmd()
subCmds := rootCmd.Commands()
var b bytes.Buffer
err := templates.ExecuteTemplate(&b, "reference.tmpl", map[string]interface{}{
"Commands": mapCommandInfos(subCmds),
})
if err != nil {
return err
}
err = os.WriteFile(referenceDocPath, b.Bytes(), 0644)
if err != nil {
return err
}
return nil
}
func checkErr(err error) {
if err != nil {
fmt.Printf("%+v", err)
os.Exit(1)
}
}
func mapCommandInfos(cmds []*cobra.Command) []CommandInfo {
infos := make([]CommandInfo, len(cmds))
for i, c := range cmds {
description := replaceAdmonition(c.Long)
info := CommandInfo{
Name: c.Name(),
Aliases: c.Aliases,
Description: description,
Usage: c.Use,
Example: c.Example,
Flags: make([]Flag, 0),
}
c.Flags().VisitAll(func(f *pflag.Flag) {
info.Flags = append(info.Flags, Flag{
Name: f.Name,
Shorthand: f.Shorthand,
Default: f.DefValue,
Type: valueTypeToString(f.Value),
Description: f.Usage,
})
})
infos[i] = info
}
return infos
}
func valueTypeToString(v pflag.Value) string {
switch t := v.Type(); t {
case "stringArray":
return "[]string"
default:
return t
}
}
var admonitionRegExp *regexp.Regexp = regexp.MustCompile("\n((Note|Tip|Info|Warning|Danger): (.+))")
func replaceAdmonition(s string) string {
description := s
admonitions := admonitionRegExp.FindAllStringSubmatch(description, -1)
if len(admonitions) > 0 {
for _, a := range admonitions {
description = strings.Replace(
description,
a[0],
fmt.Sprintf(
"\n:::%s\n\n%s\n\n:::",
strings.ToLower(a[2]),
a[3],
),
1,
)
}
}
return description
}