-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargsgen.go
207 lines (182 loc) · 4.19 KB
/
argsgen.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main
import (
"bytes"
"embed"
"fmt"
"go/ast"
"go/parser"
"go/token"
"io"
"log"
"os"
"reflect"
"strconv"
"strings"
"text/template"
"unicode"
)
//go:embed templates/*
var fs embed.FS
type flagSet struct {
VarFunc string
Field string
Description string
Default string
}
type required struct {
Field string
Zero string
}
type vars struct {
StructName string
Flags []flagSet
Positional map[int][]string
Required []required
Aliases map[string]string
}
type header struct {
Package string
Packages map[string]struct{}
}
func toTitle(input string) string {
builder := new(strings.Builder)
for i, r := range input {
if i == 0 {
builder.WriteRune(unicode.ToUpper(r))
continue
}
builder.WriteRune(r)
}
return builder.String()
}
func parse(filename, pkg string, writer io.Writer) error {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, filename, nil, 0)
if err != nil {
return err
}
tmpl, err := template.ParseFS(fs, "templates/*.tmpl")
if err != nil {
return err
}
var header header
header.Packages = make(map[string]struct{})
header.Packages["flag"] = struct{}{}
header.Packages["os"] = struct{}{}
header.Packages["fmt"] = struct{}{}
header.Package = pkg
var varsList []vars
var vs vars
ast.Inspect(f, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.TypeSpec:
vs.StructName = x.Name.Name
case *ast.StructType:
// skip anonymous structs
if vs.StructName == "" {
return true
}
vs.Flags = []flagSet{}
vs.Positional = make(map[int][]string)
vs.Required = []required{}
vs.Aliases = make(map[string]string)
for _, field := range x.Fields.List {
var varFunc string
var ident *ast.Ident
switch t := field.Type.(type) {
case *ast.Ident:
ident = t
case *ast.SelectorExpr:
pkg, ok := t.X.(*ast.Ident)
if ok && pkg.Name == "time" {
ident = t.Sel
break
}
continue
default:
continue
}
switch name := ident.Name; name {
case "uint", "uint64", "int", "int64", "float64", "string", "bool", "Duration":
varFunc = fmt.Sprintf("%sVar", toTitle(name))
default:
panic("type not supported " + name)
}
var tag string
if field.Tag != nil {
tagString, _ := strconv.Unquote(field.Tag.Value)
tag = reflect.StructTag(tagString).Get(("arg"))
options := strings.Split(tag, ",")
for _, option := range options[1:] {
switch option {
case "+", "positional":
tag = options[0]
n := len(vs.Positional)
for _, name := range field.Names {
vs.Positional[n] = append(vs.Positional[n], name.String())
}
case "!", "required":
tag = options[0]
for _, name := range field.Names {
var zero string
switch ident.Name {
case "uint", "uint64", "int", "int64", "float64", "Duration":
zero = "0"
case "string":
zero = `""`
default:
panic("type cannot be a required field: " + ident.Name)
}
vs.Required = append(vs.Required, required{
Field: name.String(),
Zero: zero,
})
header.Packages["errors"] = struct{}{}
}
}
}
}
for _, name := range field.Names {
vs.Flags = append(vs.Flags, flagSet{
VarFunc: varFunc,
Field: name.String(),
Description: tag,
Default: field.Names[0].String(),
})
}
for _, alias := range field.Names[1:] {
vs.Aliases[field.Names[0].String()] = alias.String()
}
}
varsList = append(varsList, vs)
vs = vars{}
return false
}
return true
})
if err = tmpl.ExecuteTemplate(writer, "header.tmpl", header); err != nil {
return err
}
for _, vs := range varsList {
if err = tmpl.ExecuteTemplate(writer, "parse.tmpl", vs); err != nil {
return err
}
}
return nil
}
func main() {
input := os.Getenv("GOFILE")
pkg := os.Getenv("GOPACKAGE")
writer := new(bytes.Buffer)
if err := parse(input, pkg, writer); err != nil {
log.Fatal(err)
}
output := strings.TrimSuffix(input, ".go") + "_gen.go"
file, err := os.Create(output)
if err != nil {
log.Fatal(err)
}
defer file.Close()
out := writer.String()
file.WriteString(out)
}