2019-06-06 10:29:47 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"go/parser"
|
|
|
|
"go/token"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/fatih/camelcase"
|
|
|
|
"github.com/fatih/structtag"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
args := flag.Args()
|
|
|
|
if len(args) == 0 {
|
|
|
|
// Default: process the file
|
|
|
|
args = []string{os.Getenv("GOFILE")}
|
|
|
|
}
|
|
|
|
fname := args[0]
|
|
|
|
|
|
|
|
absFilePath, err := filepath.Abs(fname)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
paths := strings.SplitAfter(absFilePath, "packer"+string(os.PathSeparator))
|
|
|
|
packerDir := paths[0]
|
|
|
|
builderName, _ := filepath.Split(paths[1])
|
|
|
|
builderName = strings.Trim(builderName, string(os.PathSeparator))
|
|
|
|
|
|
|
|
b, err := ioutil.ReadFile(fname)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("ReadFile: %+v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
fset := token.NewFileSet()
|
|
|
|
f, err := parser.ParseFile(fset, fname, b, parser.ParseComments)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("ParseFile: %+v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2020-02-14 02:50:03 -05:00
|
|
|
sourcePath := filepath.ToSlash(paths[1])
|
2019-06-12 10:25:08 -04:00
|
|
|
header := Struct{
|
2020-02-14 02:50:03 -05:00
|
|
|
SourcePath: sourcePath,
|
2019-06-12 10:25:08 -04:00
|
|
|
Name: typeSpec.Name.Name,
|
2020-04-01 23:30:51 -04:00
|
|
|
Filename: typeSpec.Name.Name + ".mdx",
|
2020-07-13 06:33:16 -04:00
|
|
|
Header: strings.TrimSpace(typeDecl.Doc.Text()),
|
2019-06-12 10:25:08 -04:00
|
|
|
}
|
2019-06-06 10:29:47 -04:00
|
|
|
required := Struct{
|
2020-02-14 02:50:03 -05:00
|
|
|
SourcePath: sourcePath,
|
2019-06-06 10:29:47 -04:00
|
|
|
Name: typeSpec.Name.Name,
|
2020-04-01 23:30:51 -04:00
|
|
|
Filename: typeSpec.Name.Name + "-required.mdx",
|
2019-06-06 10:29:47 -04:00
|
|
|
}
|
|
|
|
notRequired := Struct{
|
2020-02-14 02:50:03 -05:00
|
|
|
SourcePath: sourcePath,
|
2019-06-06 10:29:47 -04:00
|
|
|
Name: typeSpec.Name.Name,
|
2020-04-01 23:30:51 -04:00
|
|
|
Filename: typeSpec.Name.Name + "-not-required.mdx",
|
2019-06-06 10:29:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
fmt.Printf("structtag.Parse(%s): err: %v", field.Tag.Value, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2020-05-01 13:31:38 -04:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
2019-06-06 10:29:47 -04:00
|
|
|
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), " ")
|
|
|
|
}
|
2019-08-27 07:55:00 -04:00
|
|
|
|
|
|
|
if strings.Contains(docs, "TODO") {
|
|
|
|
continue
|
|
|
|
}
|
2019-08-27 07:25:52 -04:00
|
|
|
fieldType := string(b[field.Type.Pos()-1 : field.Type.End()-1])
|
|
|
|
fieldType = strings.ReplaceAll(fieldType, "*", `\*`)
|
2019-10-31 06:31:17 -04:00
|
|
|
switch fieldType {
|
2019-10-31 10:49:34 -04:00
|
|
|
case "time.Duration":
|
|
|
|
fieldType = `duration string | ex: "1h5m2s"`
|
2020-01-31 10:42:16 -05:00
|
|
|
case "config.Trilean":
|
|
|
|
fieldType = `boolean`
|
2020-03-17 06:50:16 -04:00
|
|
|
case "hcl2template.NameValues":
|
2020-03-17 09:59:52 -04:00
|
|
|
fieldType = `[]{name string, value string}`
|
2020-04-21 06:22:37 -04:00
|
|
|
case "hcl2template.KeyValues":
|
|
|
|
fieldType = `[]{key string, value string}`
|
2019-10-31 06:31:17 -04:00
|
|
|
}
|
2019-06-06 10:29:47 -04:00
|
|
|
|
|
|
|
field := Field{
|
|
|
|
Name: name,
|
2019-08-27 07:25:52 -04:00
|
|
|
Type: fieldType,
|
2019-06-06 10:29:47 -04:00
|
|
|
Docs: docs,
|
|
|
|
}
|
|
|
|
if req, err := tags.Get("required"); err == nil && req.Value() == "true" {
|
|
|
|
required.Fields = append(required.Fields, field)
|
|
|
|
} else {
|
|
|
|
notRequired.Fields = append(notRequired.Fields, field)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 23:30:51 -04:00
|
|
|
dir := filepath.Join(packerDir, "website", "pages", "partials", builderName)
|
2019-06-06 10:29:47 -04:00
|
|
|
os.MkdirAll(dir, 0755)
|
|
|
|
|
2019-06-12 10:25:08 -04:00
|
|
|
for _, str := range []Struct{header, required, notRequired} {
|
|
|
|
if len(str.Fields) == 0 && len(str.Header) == 0 {
|
2019-06-06 10:29:47 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
outputPath := filepath.Join(dir, str.Filename)
|
|
|
|
|
|
|
|
outputFile, err := os.Create(outputPath)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer outputFile.Close()
|
|
|
|
|
|
|
|
err = structDocsTemplate.Execute(outputFile, str)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("%v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|