-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
65 lines (56 loc) · 1.21 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
package main
import (
"os"
"fmt"
"go/parser"
"go/token"
"go/ast"
"go/printer"
)
func printUsageAndExit() {
fmt.Println("Usage: go-ast-trace <trace-type> <input-files>")
fmt.Println("Trace Types:")
fmt.Println(" locks Prints trace before and after locks of mutexes and channels")
os.Exit(0)
}
func printErrorAndExit(err error) {
fmt.Println("ERROR: ", err.Error())
os.Exit(1)
}
func main() {
args := os.Args[1:]
if len(args) < 2 {
printUsageAndExit()
}
traceType := args[0]
inputFilenames := args[1:]
for _, inputFilename := range inputFilenames {
fset, node := parseFile(inputFilename)
switch traceType {
case "locks":
traceLocks(node)
default:
printUsageAndExit()
}
writeFile(inputFilename, fset, node)
}
}
func parseFile(filename string) (*token.FileSet, *ast.File) {
fset := token.NewFileSet()
node, err := parser.ParseFile(fset, filename, nil, 0)
if err != nil {
printErrorAndExit(err)
}
ast.Print(fset, node)
return fset, node
}
func writeFile(filename string, fset *token.FileSet, node *ast.File) {
f, err := os.Create(filename)
if err != nil {
printErrorAndExit(err)
}
defer f.Close()
if err := printer.Fprint(f, fset, node); err != nil {
printErrorAndExit(err)
}
}