-
Notifications
You must be signed in to change notification settings - Fork 51
/
main.go
209 lines (192 loc) · 4.67 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
// nolint
package main
import (
"fmt"
"html"
"os"
"path/filepath"
"sort"
"strings"
"github.com/alecthomas/hcl"
"github.com/alecthomas/kong"
"github.com/cashapp/hermit/manifest"
)
var cli struct {
Weight int `default:"401"`
Dest string `arg:"" type:"existingdir" required:""`
}
func main() {
ctx := kong.Parse(&cli)
schema, err := hcl.Schema(&manifest.Manifest{})
ctx.FatalIfErrorf(err)
weight := cli.Weight
path := filepath.Join(cli.Dest, "manifest.md")
blocks := map[string]*hcl.Block{}
backrefs := map[string][]string{}
for _, entry := range schema.Entries {
if entry.Block != nil {
backrefs[entry.Block.Name] = []string{"manifest"}
}
}
err = hcl.Visit(schema, func(node hcl.Node, next func() error) error {
if block, ok := node.(*hcl.Block); ok {
if len(block.Body) == 1 && block.Body[0].RecursiveSchema {
return nil
}
blocks[block.Name] = block
for _, entry := range block.Body {
if entry.Block != nil {
backrefs[entry.Block.Name] = append(backrefs[entry.Block.Name], block.Name)
}
}
}
return next()
})
ctx.FatalIfErrorf(err)
fmt.Println(path)
w, err := os.Create(path)
ctx.FatalIfErrorf(err)
defer w.Close()
fmt.Fprintf(w, `+++
title = "<manifest>.hcl"
weight = %d
+++
Each Hermit package manifest is a nested structure containing OS/architecture-specific configuration.
`, weight)
err = writeEntries(w, schema.Entries)
for block, refs := range backrefs {
backrefs[block] = dedupeBackrefs(refs)
}
blockSlice := make([]*hcl.Block, 0, len(blocks))
for _, block := range blocks {
blockSlice = append(blockSlice, block)
}
// Do some naive sorting to put event blocks below "on".
sort.Slice(blockSlice, func(i, j int) bool {
iname := blockSlice[i].Name
if len(backrefs[iname]) == 1 {
iname = backrefs[iname][0] + " > " + iname
}
jname := blockSlice[j].Name
if len(backrefs[jname]) == 1 {
jname = backrefs[jname][0] + " > " + jname
}
return iname < jname
})
for _, block := range blockSlice {
weight++
err = writeBlock(weight, block, backrefs[block.Name])
ctx.FatalIfErrorf(err)
}
}
func writeBlock(weight int, block *hcl.Block, backrefs []string) error {
path := filepath.Join(cli.Dest, block.Name+".md")
fmt.Println(path)
w, err := os.Create(path)
if err != nil {
return err
}
defer w.Close()
title := blockTitle(block)
if len(backrefs) == 1 && backrefs[0] != "manifest" {
title = backrefs[0] + " > " + title
}
fmt.Fprintf(w, `+++
title = %q
weight = %d
+++
%s
`, title, weight, html.EscapeString(strings.Join(block.Comments, "\n")))
if len(backrefs) > 0 && (len(backrefs) > 1 || backrefs[0] != block.Name) {
seen := map[string]bool{block.Name: true}
fmt.Fprintf(w, "Used by:")
for _, backref := range backrefs {
if seen[backref] {
continue
}
seen[backref] = true
title := backref
if backref == "manifest" {
title = "<manifest>"
}
fmt.Fprintf(w, " [%s](../%s#blocks)", title, backref)
}
fmt.Fprintf(w, "\n\n")
}
return writeEntries(w, block.Body)
}
func dedupeBackrefs(backrefs []string) []string {
seenBackref := map[string]bool{}
var backrefsSet []string
for _, backref := range backrefs {
if seenBackref[backref] {
continue
}
seenBackref[backref] = true
backrefsSet = append(backrefsSet, backref)
}
sort.Strings(backrefsSet)
return backrefsSet
}
func blockTitle(block *hcl.Block) string {
title := block.Name
for _, label := range block.Labels {
title += " <" + label + ">"
}
return title
}
func writeEntries(w *os.File, entries []*hcl.Entry) error {
var (
blocks []*hcl.Block
attrs []*hcl.Attribute
)
for _, entry := range entries {
if entry.Attribute != nil {
attrs = append(attrs, entry.Attribute)
} else if entry.Block != nil {
blocks = append(blocks, entry.Block)
}
}
sort.Slice(blocks, func(i, j int) bool {
return blocks[i].Name < blocks[j].Name
})
sort.Slice(attrs, func(i, j int) bool {
return attrs[i].Key < attrs[j].Key
})
if len(blocks) > 0 {
fmt.Fprintf(w, `
## Blocks
| Block | Description |
|--------|-------------|
`)
for _, block := range blocks {
description := block.Name
for _, label := range block.Labels {
description += " <" + label + ">"
}
description += " { … }"
fmt.Fprintf(w, "| [`%s`](../%s) | %s |\n",
description,
block.Name,
html.EscapeString(strings.Join(block.Comments, " ")))
}
}
if len(attrs) > 0 {
fmt.Fprintf(w, `
## Attributes
| Attribute | Type | Description |
|-----------|------|-------------|
`)
for _, attr := range attrs {
typ := attr.Value.String()
if attr.Optional {
typ += "?"
}
fmt.Fprintf(w, "| `%s` | `%s` | %s |\n",
attr.Key,
typ,
html.EscapeString(strings.Join(attr.Comments, " ")))
}
}
return nil
}