forked from openshift/osde2e
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
108 lines (95 loc) · 2.34 KB
/
main.go
File metadata and controls
108 lines (95 loc) · 2.34 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"bytes"
"flag"
"go/build"
"io/ioutil"
"log"
"os"
"path/filepath"
"text/template"
)
const (
pkg = "github.com/openshift/osde2e"
genCmd = "make generate"
)
var (
docsTmplFile, outputFile, configPkgDir string
check bool
base = filepath.Join(getGopath(), "src", pkg)
docsTmpl *template.Template
original []byte
docs = DocsData{
Title: "osde2e Options",
Sections: []Section{
{
Name: "required",
Description: "These options are required to run osde2e.",
},
{
Name: "tests",
},
{
Name: "environment",
},
{
Name: "cluster",
},
{
Name: "version",
},
{
Name: "upgrade",
},
{
Name: "metrics",
},
{
Name: "addons",
},
},
}
)
func init() {
flag.StringVar(&docsTmplFile, "in", filepath.Join(base, "cmd/osde2e-docs/Options.md.tmpl"), "docs template file")
flag.StringVar(&outputFile, "out", filepath.Join(base, "docs/Options.md"), "rendered docs file")
flag.StringVar(&configPkgDir, "pkg-dir", filepath.Join(base, "pkg/config"), "Go package with struct named Config")
flag.BoolVar(&check, "check", false, "check docs are updated (doesn't modify out)")
flag.Parse()
docsTmpl = template.Must(template.New("Options.md.tmpl").ParseFiles(docsTmplFile))
}
func main() {
var err error
// read generated documentation when checking if update is required
if check {
if original, err = ioutil.ReadFile(outputFile); err != nil {
log.Fatalf("couldn't read rendered output file '%s' to compare against: %v", outputFile, err)
}
}
// use AST of config package to get configuration options and include in docs
opts := parseOpts(configPkgDir)
docs.Populate(opts)
// render templated documentation
var buf bytes.Buffer
if err = docsTmpl.Execute(&buf, docs); err != nil {
log.Fatalf("Failed to render docs: %v", err)
}
// either check if docs are up-to-date or write docs
if check {
checkDocs(buf.Bytes())
} else if err = ioutil.WriteFile(outputFile, buf.Bytes(), 0644); err != nil {
log.Fatal(err)
}
}
func checkDocs(rendered []byte) {
if !bytes.Equal(rendered, original) {
log.Fatalf("Documentation file '%s' needs update. To regenerate run '%s'.", outputFile, genCmd)
}
}
func getGopath() string {
gopath := os.Getenv("GOPATH")
if gopath == "" {
gopath = build.Default.GOPATH
}
return gopath
}