forked from openshift/osde2e
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.go
More file actions
92 lines (81 loc) · 2.41 KB
/
parse.go
File metadata and controls
92 lines (81 loc) · 2.41 KB
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
package main
import (
"fmt"
"go/ast"
"go/build"
"go/parser"
"go/token"
"log"
"reflect"
"strings"
"github.com/openshift/osde2e/pkg/load"
)
const (
// configTypeName is the the name of the struct containing configuration.
configTypeName = "Config"
// configFileSuffix is the suffix of the file containing the Config type.
configFileSuffix = "/config.go"
)
// returns options separated by section
func parseOpts(dir string) (opts map[string]Options) {
// get package details based on current wd
pkg, err := build.Import(".", dir, build.ImportComment)
if err != nil {
log.Fatal(err)
}
// parse AST
fs := token.NewFileSet()
pkgs, err := parser.ParseDir(fs, pkg.Dir, nil, parser.ParseComments)
if err != nil {
log.Fatalf("Failed to parse options from '%s': %v", dir, err)
} else if len(pkgs) != 1 {
log.Fatalf("there should be exactly 1 package in the config dir, found %d", len(pkgs))
}
astPkg := pkgs[pkg.Name]
// collect every field by section
opts = make(map[string]Options, 20)
for f, v := range astPkg.Files {
if strings.HasSuffix(f, configFileSuffix) {
for d := range v.Decls {
if decl, ok := v.Decls[d].(*ast.GenDecl); ok {
for s := range decl.Specs {
// look for Config type declaration
if typSpec, ok := decl.Specs[s].(*ast.TypeSpec); ok && typSpec.Name.String() == configTypeName {
for _, field := range typSpec.Type.(*ast.StructType).Fields.List {
// documented options should have tags
if field.Tag == nil {
continue
}
// only document options exposed with tags as Environment Variables
tagStr := strings.Trim(field.Tag.Value, "`")
tag := reflect.StructTag(tagStr)
if env, hasEnvTag := tag.Lookup(load.EnvVarTag); hasEnvTag {
section := tag.Get(load.SectionTag)
defaultValue, _ := tag.Lookup(load.DefaultTag)
opts[section] = append(opts[section], Option{
Variable: env,
Description: field.Doc.Text(),
Type: getFieldType(field.Type),
DefaultValue: defaultValue,
})
}
}
}
}
}
}
}
}
return
}
func getFieldType(expr ast.Expr) string {
switch t := expr.(type) {
case *ast.Ident:
return t.String()
case *ast.ArrayType:
arrTyp := t.Elt
return fmt.Sprintf("[]%s", getFieldType(arrTyp))
}
typErr := fmt.Sprintf("encountered unexpected AST type while parsing: %T", expr)
panic(typErr)
}