-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
192 lines (158 loc) · 3.94 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"os"
"strings"
"fmt"
"log"
"go/parser"
"go/token"
"go/ast"
"flag"
)
type envVar struct {
n string
d *ast.CommentGroup
}
func main() {
flag.Parse()
infile := flag.Arg(0)
outfile := flag.Arg(1)
if len(outfile) < 4 || outfile[len(outfile)-3:] != ".go" {
fmt.Fprintf(os.Stderr, "Please specify a valid outfile (second arg)")
return
}
vars, pkg := getEnvConstantsFromFile(infile)
createLookupFile(vars, pkg, outfile)
createLookupTestFile(vars, pkg, outfile)
}
func createLookupTestFile(vars []envVar, pkg string, filename string) {
testFilename := filename[:len(filename)-3]+"_test.go"
out, err := os.OpenFile(testFilename, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
defer out.Close()
fmt.Fprintln(out,"package",pkg)
fmt.Fprintln(out,"import \"testing\"")
fmt.Fprintln(out,"")
fmt.Fprintln(out, "// DO NOT EDIT")
fmt.Fprintf(out, "// This file is auto generated by: %s\n", strings.Join(os.Args, " "))
fmt.Fprintln(out,"")
fmt.Fprintln(out,"")
fmt.Fprintln(out, `
func OnPanic(t *testing.T, env string) {
if err := recover(); err != nil {
t.Errorf("Missing Env: %v", err)
}
}`)
for _, v := range vars {
fmt.Fprintln(out, createLookupTestFunction(v))
fmt.Fprintln(out, createMustLookupTestFunction(v))
}
}
func createLookupFile(vars []envVar, pkg string, filename string) {
out, err := os.OpenFile(filename, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
defer out.Close()
fmt.Fprintln(out,"package",pkg)
fmt.Fprintln(out,"import \"os\"")
fmt.Fprintln(out,"import \"fmt\"")
fmt.Fprintln(out,"")
fmt.Fprintln(out, "// DO NOT EDIT")
fmt.Fprintf(out, "// This file is auto generated by: %s\n", strings.Join(os.Args, " "))
fmt.Fprintln(out,"")
fmt.Fprintln(out,"")
for _, v := range vars {
fmt.Fprintln(out, createLookupFunction(v))
fmt.Fprintln(out, createMustLookupFunction(v))
}
}
func createCommentBlock(c *ast.CommentGroup) string {
if c == nil {
return ""
}
var sb strings.Builder
for _, comment := range c.List {
sb.WriteString(comment.Text)
sb.WriteRune('\n')
}
return sb.String()
}
func createLookupTestFunction(v envVar) string {
v.d.Text()
funcName := "Get"+v.n[3:] // replace evn with Get
s := `%sfunc Test%s(t *testing.T) {
if _, ok := %s(); !ok {
t.Errorf("Missing Env: %%s", %s)
}
}
`
return fmt.Sprintf(s, createCommentBlock(v.d), funcName, funcName ,v.n)
}
func createMustLookupTestFunction(v envVar) string {
v.d.Text()
funcName := "Get"+v.n[3:] // replace evn with Get
s := `%sfunc TestMust%s(t *testing.T) {
defer OnPanic(t, %s)
_ = Must%s()
}
`
return fmt.Sprintf(s, createCommentBlock(v.d), funcName, v.n, funcName)
}
func createLookupFunction(v envVar) string {
v.d.Text()
funcName := "Get"+v.n[3:] // replace evn with Get
s := `%sfunc %s() (value string, ok bool) {
value, ok = os.LookupEnv(%s)
return
}
`
return fmt.Sprintf(s, createCommentBlock(v.d), funcName, v.n)
}
func createMustLookupFunction(v envVar) string {
v.d.Text()
funcName := "Get"+v.n[3:] // replace evn with Get
s := `%sfunc Must%s() (value string) {
var ok bool
value, ok = os.LookupEnv(%s)
if !ok {
panic(fmt.Errorf("expected environment variable %%s to be set", %s))
}
return
}
`
return fmt.Sprintf(s, createCommentBlock(v.d), funcName, v.n, v.n)
}
func getEnvConstantsFromFile(filepath string) ([]envVar, string) {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, filepath, nil, parser.ParseComments)
if err != nil {
log.Fatal(err)
}
var envs []envVar
var pkg string
pkg = f.Name.String()
for _, decl := range f.Decls {
switch t := decl.(type){
case *ast.GenDecl:
if t.Tok == token.CONST {
for _, spec := range t.Specs {
switch st := spec.(type) {
case *ast.ValueSpec:
name := st.Names[0].String()
if name[:3] == "env" {
x := envVar{
n: name,
d: st.Doc,
}
envs = append(envs, x)
}
}
}
}
}
}
return envs, pkg
}