/
main.go
337 lines (293 loc) · 6.62 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"go/ast"
"go/parser"
"go/printer"
"go/token"
"go/types"
"io/ioutil"
"os"
"os/exec"
"os/signal"
"runtime"
"strings"
"sync"
"github.com/rogpeppe/go-internal/imports"
)
//go:generate gobin -m -run myitcv.io/cmd/helpflagtopkgdoc
type tagsFlag struct {
vals []string
}
func (e *tagsFlag) String() string {
return fmt.Sprintf("%v", e.vals)
}
func (e *tagsFlag) Set(v string) error {
e.vals = append(e.vals, v)
return nil
}
func main() {
os.Exit(main1())
}
func main1() int {
switch err := mainerr(); err {
case nil:
return 0
case flag.ErrHelp:
return 2
default:
fmt.Fprintln(os.Stderr, err)
return 1
}
}
func mainerr() (retErr error) {
fs := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
fs.Usage = func() {
mainUsage(os.Stderr)
}
var tagsVals tagsFlag
fs.Var(&tagsVals, "tags", "tags for build list")
if err := fs.Parse(os.Args[1:]); err != nil {
return err
}
if len(fs.Args()) != 1 {
return fmt.Errorf("expected a single arg; the variable to marshal")
}
varName := fs.Arg(0)
goos := os.Getenv("GOOS")
if goos == "" {
goos = runtime.GOOS
}
goarch := os.Getenv("GOARCH")
if goarch == "" {
goarch = runtime.GOARCH
}
tags := map[string]bool{
goos: true,
goarch: true,
}
for _, v := range tagsVals.vals {
for _, vv := range strings.Fields(v) {
tags[vv] = true
}
}
fset := token.NewFileSet()
matchFile := func(fi os.FileInfo) bool {
return imports.MatchFile(fi.Name(), tags)
}
pkgs, err := parser.ParseDir(fset, ".", matchFile, 0)
if err != nil {
return fmt.Errorf("failed to parse current directory: %v", err)
}
pkgName := os.Getenv("GOPACKAGE")
pkg := pkgs[pkgName]
if pkg == nil {
return fmt.Errorf("failed to find package for package name %v", pkgName)
}
type match struct {
file *ast.File
expr ast.Expr
}
var matches []match
typeDecls := make(map[string]*ast.TypeSpec)
for _, f := range pkg.Files {
var comments bytes.Buffer
for _, cg := range f.Comments {
if cg == f.Doc || cg.Pos() > f.Package {
break
}
for _, cm := range cg.List {
comments.WriteString(cm.Text + "\n")
}
comments.WriteString("\n")
}
if !imports.ShouldBuild(comments.Bytes(), tags) {
continue
}
for _, gd := range f.Decls {
switch gd := gd.(type) {
case *ast.GenDecl:
for _, s := range gd.Specs {
switch gd.Tok {
case token.VAR:
vs := s.(*ast.ValueSpec)
if len(vs.Values) == 0 {
// no value; nothing to do
continue
}
for i, name := range vs.Names {
expr := vs.Values[i]
if varName == name.Name {
matches = append(matches, match{
file: f,
expr: expr,
})
}
}
case token.TYPE:
ts := s.(*ast.TypeSpec)
typeDecls[ts.Name.Name] = ts
}
}
}
}
}
switch len(matches) {
case 0:
return fmt.Errorf("failed to find declaration of %v", varName)
case 1:
default:
var dups []string
for _, m := range matches {
dups = append(dups, fmt.Sprintf("found declaration of %v at %v", varName, fset.Position(m.expr.Pos())))
}
return fmt.Errorf("%v", strings.Join(dups, "\n"))
}
theMatch := matches[0]
imports := make(map[*ast.ImportSpec]bool)
usedTypes := make(map[*ast.TypeSpec]bool)
work := []ast.Node{theMatch.expr}
visitType := func(node ast.Node) bool {
switch node := node.(type) {
case *ast.SelectorExpr:
if x, ok := node.X.(*ast.Ident); ok {
for _, imp := range pkg.Files[fset.File(node.Pos()).Name()].Imports {
if imp.Name != nil {
if x.Name == imp.Name.Name {
imports[imp] = true
}
} else {
cleanPath := strings.Trim(imp.Path.Value, "\"")
parts := strings.Split(cleanPath, "/")
if x.Name == parts[len(parts)-1] {
imports[imp] = true
}
}
}
}
// we have handled the qualified identifier; do no inspect its Idents
return false
case *ast.Ident:
typ := typeDecls[node.Name]
if typ != nil {
usedTypes[typ] = true
} else if types.Universe.Lookup(node.Name) == nil {
panic(fmt.Errorf("failed to find type declaration for %v", node.Name))
}
work = append(work, node)
}
return true
}
visitVal := func(node ast.Node) bool {
switch node := node.(type) {
case *ast.CompositeLit:
if node.Type != nil {
ast.Inspect(node.Type, visitType)
}
case *ast.BasicLit:
ast.Inspect(node, visitType)
}
return true
}
err = func() (err error) {
defer func() {
if r := recover(); r != nil {
err = r.(error)
}
}()
for len(work) > 0 {
w := work[0]
work = work[1:]
ast.Inspect(w, visitVal)
}
return
}()
if err != nil {
return fmt.Errorf("failed to walk AST: %v", err)
}
var tempFile string
var lock sync.Mutex
ctrlc := make(chan os.Signal)
signal.Notify(ctrlc, os.Interrupt)
go func() {
<-ctrlc
lock.Lock()
if tempFile != "" {
os.Remove(tempFile)
}
os.Exit(1)
}()
lock.Lock()
tf, err := ioutil.TempFile(".", "vartojson.*.go")
if err != nil {
lock.Unlock()
return fmt.Errorf("failed to create temp file: %v", err)
}
tempFile = tf.Name()
defer func() {
lock.Lock()
defer lock.Unlock()
os.Remove(tempFile)
}()
lock.Unlock()
var buf bytes.Buffer
p := func(format string, args ...interface{}) {
fmt.Fprintf(&buf, format, args...)
}
pp := func(node interface{}) string {
var sb strings.Builder
printer.Fprint(&sb, fset, node)
return sb.String()
}
p(`
package main
import (
"fmt"
"encoding/json"
)
`[1:])
for i := range imports {
p("import %v\n", pp(i))
}
for t := range usedTypes {
p("type %v\n", pp(t))
}
p(`
func main() {
v := %v
byts, err := json.MarshalIndent(&v, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(byts))
}
`[1:], pp(theMatch.expr))
if err := ioutil.WriteFile(tempFile, buf.Bytes(), 0666); err != nil {
return fmt.Errorf("failed to write temp main")
}
var stdout, stderr bytes.Buffer
cmd := exec.Command("go", "run", tempFile)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to %v: %v\n%s", strings.Join(cmd.Args, " "), err, stderr.Bytes())
}
// reparse into an interface{} and write out again... so that we get consistently formatted JSON
// output
var i interface{}
if err := json.Unmarshal(stdout.Bytes(), &i); err != nil {
return fmt.Errorf("failed to Unmarshal JSON: %v", err)
}
toWrite, err := json.MarshalIndent(i, "", " ")
if err != nil {
return fmt.Errorf("failed to re-Marshal JSON: %v", err)
}
fn := "gen_" + varName + "_vartojson.json"
if err := ioutil.WriteFile(fn, append(toWrite, '\n'), 0666); err != nil {
return fmt.Errorf("failed to write %v: %v", fn, err)
}
return nil
}