-
Notifications
You must be signed in to change notification settings - Fork 78
/
main.go
81 lines (71 loc) · 1.88 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
// gen generates ec2config documentation.
package main
import (
"bytes"
"fmt"
"io/ioutil"
"reflect"
"strings"
"github.com/aws/aws-k8s-tester/ec2config"
"github.com/olekukonko/tablewriter"
)
func main() {
doc := createDoc()
if err := ioutil.WriteFile("ec2config/README.md", []byte("\n```\n"+doc+"```\n"), 0666); err != nil {
panic(err)
}
fmt.Println("generated")
}
func createDoc() string {
b := strings.Builder{}
b.WriteString(writeDoc(ec2config.AWS_K8S_TESTER_EC2_PREFIX, &ec2config.Config{}))
b.WriteString("\n\n")
b.WriteString(writeDoc(ec2config.AWS_K8S_TESTER_EC2_PREFIX+"S3_", &ec2config.S3{}))
b.WriteString("\n\n")
b.WriteString(writeDoc(ec2config.AWS_K8S_TESTER_EC2_PREFIX+"ROLE_", &ec2config.Role{}))
b.WriteString("\n\n")
b.WriteString(writeDoc(ec2config.AWS_K8S_TESTER_EC2_PREFIX+"VPC_", &ec2config.VPC{}))
b.WriteString("\n\n")
return b.String()
}
var columns = []string{
"environmental variable",
"read only",
"type",
"go type",
}
func writeDoc(pfx string, st interface{}) string {
buf := bytes.NewBuffer(nil)
tb := tablewriter.NewWriter(buf)
tb.SetAutoWrapText(false)
tb.SetAlignment(tablewriter.ALIGN_LEFT)
tb.SetColWidth(1500)
tb.SetCenterSeparator("*")
tb.SetHeader(columns)
ts := reflect.TypeOf(st)
tp, vv := reflect.TypeOf(st).Elem(), reflect.ValueOf(st).Elem()
for i := 0; i < tp.NumField(); i++ {
jv := tp.Field(i).Tag.Get("json")
if jv == "" {
continue
}
if vv.Field(i).Type().Kind() == reflect.Ptr {
continue
}
readOnly := "false"
if tp.Field(i).Tag.Get("read-only") == "true" {
readOnly = "true"
}
jv = strings.Replace(jv, ",omitempty", "", -1)
jv = strings.ToUpper(strings.Replace(jv, "-", "_", -1))
env := pfx + jv
tb.Append([]string{
env,
fmt.Sprintf("read-only %q", readOnly),
fmt.Sprintf("%s.%s", ts, tp.Field(i).Name),
fmt.Sprintf("%s", vv.Field(i).Type()),
})
}
tb.Render()
return buf.String()
}