forked from kubermatic/kubermatic
/
main.go
112 lines (85 loc) · 2.57 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
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"bytes"
"errors"
"fmt"
"go/ast"
"go/format"
"go/token"
"log"
"reflect"
"strings"
"golang.org/x/tools/go/packages"
"github.com/kubermatic/kubermatic/api/pkg/addon"
)
var packageCache = map[string]*packages.Package{}
func main() {
t := addon.TemplateData{}
snippets := generateDocumentation([]string{}, reflect.ValueOf(t))
fmt.Println(strings.Join(snippets, "\n\n"))
}
func generateDocumentation(docs []string, v reflect.Value) []string {
t := v.Type()
if v.Kind() == reflect.Struct {
pkgName := t.PkgPath()
symbol := t.Name()
log.Printf("Documenting %s.%s ...", pkgName, symbol)
pkg, err := loadPackage(pkgName)
if err != nil {
log.Fatalf("Failed to load package: %v", err)
}
doc, err := getDocumentation(pkg, symbol)
if err != nil {
log.Fatalf("Failed to generate docs: %v", err)
}
docs = append(docs, doc)
for n := 0; n < v.NumField(); n++ {
docs = generateDocumentation(docs, v.Field(n))
}
}
return docs
}
func loadPackage(name string) (*packages.Package, error) {
if p, ok := packageCache[name]; ok {
return p, nil
}
cfg := packages.Config{
Mode: packages.NeedName | packages.NeedFiles | packages.NeedImports | packages.NeedDeps | packages.NeedTypes | packages.NeedSyntax,
}
pkgs, err := packages.Load(&cfg, name)
if err != nil {
return nil, err
}
if len(pkgs) != 1 {
return nil, fmt.Errorf("expected to load 1 package, but got %d", len(pkgs))
}
pkg := pkgs[0]
packageCache[name] = pkg
return pkg, nil
}
func getDocumentation(pkg *packages.Package, symbolName string) (string, error) {
for _, s := range pkg.Syntax {
for _, d := range s.Decls {
if genDecl, ok := d.(*ast.GenDecl); ok && genDecl.Tok == token.TYPE && len(genDecl.Specs) == 1 {
if typeSpec, ok := genDecl.Specs[0].(*ast.TypeSpec); ok && typeSpec.Name.String() == symbolName {
var buf bytes.Buffer
err := format.Node(&buf, pkg.Fset, genDecl)
return buf.String(), err
}
}
}
}
return "", errors.New("symbol not found")
}