/
struct_markdown.go
225 lines (194 loc) · 5.31 KB
/
struct_markdown.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package struct_markdown
import (
_ "embed"
"go/ast"
"go/parser"
"go/token"
"log"
"os"
"path/filepath"
"strings"
"github.com/fatih/camelcase"
"github.com/fatih/structtag"
)
var (
//go:embed README.md
readme string
)
type Command struct {
}
func (cmd *Command) Help() string {
return `Usage: packer-sdc struct-markdown
` + readme
}
func (cmd *Command) Run(args []string) int {
if len(args) == 0 {
// Default: process the file
args = []string{os.Getenv("GOFILE")}
}
fname := args[0]
absFilePath, err := filepath.Abs(fname)
if err != nil {
log.Fatalf("%v", err)
}
var projectRoot, docsFolder, filePath string
for dir := filepath.Dir(absFilePath); len(dir) > 0 && projectRoot == ""; dir = filepath.Dir(dir) {
base := filepath.Base(dir)
if base == "packer" || base == "packer-internal" {
projectRoot = dir
filePath, _ = filepath.Rel(projectRoot, absFilePath)
docsFolder = filepath.Join("website", "content", "partials")
break
}
if base == "packer-plugin-sdk" {
projectRoot = dir
filePath, _ = filepath.Rel(projectRoot, absFilePath)
docsFolder = filepath.Join("cmd", "packer-sdc", "internal", "renderdocs", "docs-partials", "packer-plugin-sdk")
break
}
if strings.HasPrefix(base, "packer-plugin-") {
projectRoot = dir
filePath, _ = filepath.Rel(projectRoot, absFilePath)
docsFolder = filepath.Join("docs-partials")
break
}
}
if projectRoot == "" {
log.Fatal("Failed to guess project ROOT. If this is a Packer plugin project please make sure the root directory begins with`packer-plugin-*`")
}
b, err := os.ReadFile(fname)
if err != nil {
log.Fatalf("ReadFile: %+v", err)
}
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, fname, b, parser.ParseComments)
if err != nil {
log.Fatalf("ParseFile: %+v", err)
}
canonicalFilePath := filepath.ToSlash(filePath)
for _, decl := range f.Decls {
typeDecl, ok := decl.(*ast.GenDecl)
if !ok {
continue
}
typeSpec, ok := typeDecl.Specs[0].(*ast.TypeSpec)
if !ok {
continue
}
structDecl, ok := typeSpec.Type.(*ast.StructType)
if !ok {
continue
}
fields := structDecl.Fields.List
header := Struct{
SourcePath: canonicalFilePath,
Name: typeSpec.Name.Name,
Filename: typeSpec.Name.Name + ".mdx",
Header: strings.TrimSpace(typeDecl.Doc.Text()),
}
dataSourceOutput := Struct{
SourcePath: canonicalFilePath,
Name: typeSpec.Name.Name,
Filename: typeSpec.Name.Name + ".mdx",
}
required := Struct{
SourcePath: canonicalFilePath,
Name: typeSpec.Name.Name,
Filename: typeSpec.Name.Name + "-required.mdx",
}
notRequired := Struct{
SourcePath: canonicalFilePath,
Name: typeSpec.Name.Name,
Filename: typeSpec.Name.Name + "-not-required.mdx",
}
for _, field := range fields {
if len(field.Names) == 0 || field.Tag == nil {
continue
}
tag := field.Tag.Value[1:]
tag = tag[:len(tag)-1]
tags, err := structtag.Parse(tag)
if err != nil {
log.Fatalf("structtag.Parse(%s): err: %v", field.Tag.Value, err)
}
// Leave undocumented tags out of markdown. This is useful for
// fields which exist for backwards compatability, or internal-use
// only fields
undocumented, _ := tags.Get("undocumented")
if undocumented != nil {
if undocumented.Name == "true" {
continue
}
}
mstr, err := tags.Get("mapstructure")
if err != nil {
continue
}
name := mstr.Name
if name == "" {
continue
}
var docs string
if field.Doc != nil {
docs = field.Doc.Text()
} else {
docs = strings.Join(camelcase.Split(field.Names[0].Name), " ")
}
if strings.Contains(docs, "TODO") {
continue
}
fieldType := string(b[field.Type.Pos()-1 : field.Type.End()-1])
fieldType = strings.ReplaceAll(fieldType, "*", `\*`)
switch fieldType {
case "time.Duration":
fieldType = `duration string | ex: "1h5m2s"`
case "config.Trilean":
fieldType = `boolean`
case "config.NameValues":
fieldType = `[]{name string, value string}`
case "config.KeyValues":
fieldType = `[]{key string, value string}`
}
field := Field{
Name: name,
Type: fieldType,
Docs: docs,
}
if typeSpec.Name.Name == "DatasourceOutput" {
dataSourceOutput.Fields = append(dataSourceOutput.Fields, field)
continue
}
if req, err := tags.Get("required"); err == nil && req.Value() == "true" {
required.Fields = append(required.Fields, field)
} else {
notRequired.Fields = append(notRequired.Fields, field)
}
}
dir := filepath.Join(projectRoot, docsFolder, filepath.Dir(filePath))
if err := os.MkdirAll(dir, 0755); err != nil {
log.Fatalf("mkdir of %q failed: %v", dir, err)
}
for _, str := range []Struct{header, dataSourceOutput, required, notRequired} {
if len(str.Fields) == 0 && len(str.Header) == 0 {
continue
}
outputPath := filepath.Join(dir, str.Filename)
outputFile, err := os.Create(outputPath)
if err != nil {
log.Fatalf(err.Error())
return 1
}
defer outputFile.Close()
err = structDocsTemplate.Execute(outputFile, str)
if err != nil {
log.Fatalf("%v", err)
}
}
}
return 0
}
func (cmd *Command) Synopsis() string {
return "[//go:generate command] Generates markdown files from the comments of in a struct config."
}