-
Notifications
You must be signed in to change notification settings - Fork 23
/
main.go
266 lines (220 loc) · 4.89 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package main
import (
"fmt"
"os"
"path"
"sort"
"strconv"
"strings"
"text/template"
"time"
"gitlab.com/golang-commonmark/markdown"
)
type ADRMeta struct {
Index int
Authors []string
Date time.Time
Status string
Tags []string
Path string
}
type ADR struct {
Heading string
Meta ADRMeta
}
var (
validStatus = []string{"Approved", "Partially Implemented", "Implemented", "Deprecated"}
)
func parseCommaList(l string) []string {
tags := strings.Split(l, ",")
res := []string{}
for _, t := range tags {
res = append(res, strings.TrimSpace(t))
}
return res
}
func parseADR(adrPath string) (*ADR, error) {
body, err := os.ReadFile(adrPath)
if err != nil {
panic(err)
}
md := markdown.New()
tokens := md.Parse(body)
in1stHdr := false
in1stTbl := false
curHdrKey := ""
adr := ADR{
Meta: ADRMeta{
Path: adrPath,
},
}
base := strings.TrimSuffix(path.Base(adrPath), path.Ext(adrPath))
parts := strings.Split(base, "-")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid filename %s in %s", base, adrPath)
}
idx, err := strconv.Atoi(parts[1])
if err != nil {
return nil, fmt.Errorf("invalid file sequence %s in %s", parts[1], adrPath)
}
adr.Meta.Index = idx
for _, t := range tokens {
switch tok := t.(type) {
case *markdown.Inline:
switch {
case in1stHdr:
adr.Heading = tok.Content
case curHdrKey == "Date":
t, err := time.Parse("2006-01-02", tok.Content)
if err != nil {
return nil, fmt.Errorf("invalid date format, not YYYY-MM-DD: %s", err)
}
adr.Meta.Date = t
case curHdrKey == "Author":
adr.Meta.Authors = parseCommaList(tok.Content)
case curHdrKey == "Status":
adr.Meta.Status = tok.Content
case curHdrKey == "Tags":
adr.Meta.Tags = parseCommaList(tok.Content)
case in1stTbl:
curHdrKey = tok.Content
}
case *markdown.TbodyOpen:
in1stTbl = true
case *markdown.HeadingOpen:
if adr.Heading == "" {
in1stHdr = true
}
case *markdown.TbodyClose:
in1stTbl = false
// stop parsing after first table
break
case *markdown.HeadingClose:
in1stHdr = false
case *markdown.TableClose:
in1stTbl = false
case *markdown.TrClose:
curHdrKey = ""
}
}
if adr.Meta.Index == 0 {
return nil, fmt.Errorf("invalid ADR Index in %s", adr.Meta.Path)
}
if adr.Meta.Date.IsZero() {
return nil, fmt.Errorf("date is required in %s", adr.Meta.Path)
}
if !isValidStatus(adr.Meta.Status) {
return nil, fmt.Errorf("invalid status %q, must be one of: %s in %s", adr.Meta.Status, strings.Join(validStatus, ", "), adr.Meta.Path)
}
if len(adr.Meta.Authors) == 0 {
return nil, fmt.Errorf("authors is required in %s", adr.Meta.Path)
}
if len(adr.Meta.Tags) == 0 {
return nil, fmt.Errorf("tags is required in %s", adr.Meta.Path)
}
return &adr, nil
}
func isValidStatus(status string) bool {
for _, s := range validStatus {
if status == s {
return true
}
}
return false
}
func verifyUniqueIndexes(adrs []*ADR) error {
indexes := map[int]string{}
for _, a := range adrs {
path, ok := indexes[a.Meta.Index]
if ok {
return fmt.Errorf("duplicate index %d, conflict between %s and %s", a.Meta.Index, a.Meta.Path, path)
}
indexes[a.Meta.Index] = a.Meta.Path
}
return nil
}
func renderIndexes(adrs []*ADR) error {
tags := map[string]int{}
for _, adr := range adrs {
for _, tag := range adr.Meta.Tags {
tags[tag] = 1
}
}
tagsList := []string{}
hasDeprecated := false
for k := range tags {
if k == "deprecated" {
hasDeprecated = true
continue
}
tagsList = append(tagsList, k)
}
sort.Strings(tagsList)
if hasDeprecated {
tagsList = append(tagsList, "deprecated")
}
type tagAdrs struct {
Tag string
Adrs []*ADR
}
renderList := []tagAdrs{}
for _, tag := range tagsList {
matched := []*ADR{}
for _, adr := range adrs {
for _, mt := range adr.Meta.Tags {
if tag == mt {
matched = append(matched, adr)
}
}
}
sort.Slice(matched, func(i, j int) bool {
return matched[i].Meta.Index < matched[j].Meta.Index
})
renderList = append(renderList, tagAdrs{Tag: tag, Adrs: matched})
}
funcMap := template.FuncMap{
"join": func(i []string) string {
return strings.Join(i, ", ")
},
"title": func(i string) string {
return strings.Title(i)
},
}
readme, err := template.New(".readme.templ").Funcs(funcMap).ParseFiles(".readme.templ")
if err != nil {
return err
}
err = readme.Execute(os.Stdout, renderList)
if err != nil {
return err
}
return nil
}
func main() {
dir, err := os.ReadDir("adr")
if err != nil {
panic(err)
}
adrs := []*ADR{}
for _, mdf := range dir {
if mdf.IsDir() {
continue
}
if path.Ext(mdf.Name()) != ".md" {
continue
}
adr, err := parseADR(path.Join("adr", mdf.Name()))
if err != nil {
panic(err)
}
adrs = append(adrs, adr)
}
err = verifyUniqueIndexes(adrs)
if err != nil {
panic(err)
}
err = renderIndexes(adrs)
if err != nil {
panic(err)
}
}