-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
104 lines (91 loc) · 2.03 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
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"log"
"os"
"github.com/kristiehoward/go2flow/handlers"
"github.com/urfave/cli"
)
const (
appName = "Go2Flow"
appUsage = `Convert Golang types to Flow types`
)
var (
flags = []cli.Flag{
cli.StringFlag{
Name: "file, f",
Usage: ".go file to consume",
},
cli.StringFlag{
Name: "dir, d",
Usage: "directory containing .go file to consume",
},
}
)
// TypeDefInspector handles ast nodes if they are type definitions
func TypeDefInspector(node ast.Node) bool {
// Check if this node is a type definition
ts, ok := node.(*ast.TypeSpec)
if ok {
handlers.HandleTypeDef(*ts)
}
return true
}
func handleFile(file string) error {
fset := token.NewFileSet()
// Parse the src file's information into the astNode, including the comments
astNode, err := parser.ParseFile(fset, file, nil, parser.ParseComments)
if err != nil {
return err
}
// Inspect the AST using the inspector that handles only type definitions
ast.Inspect(astNode, TypeDefInspector)
return nil
}
func run(c *cli.Context) error {
file := c.String("file")
dir := c.String("dir")
// TODO Maxime 11/5/2017
// Check if the file passed in the CLI has the .go extension
if file == "" && dir == "" {
fmt.Println("Please specify a .go file to consume")
return nil
}
// Handle directory
if dir != "" {
files, err := ioutil.ReadDir(dir)
if err != nil {
return err
}
for _, f := range files {
err := handleFile(dir + "/" + f.Name())
if err != nil {
return err
}
}
return nil
}
// Handle file
return handleFile(file)
}
// TODO Kristie 10/24/17
// - Dockerize development
// - Put the output through Prettier (use a container)
// - Optionally keep the comments by the struct defs?
// - Handle definitions not in the struct tags (talk to Maxime)
func main() {
app := cli.NewApp()
app.Name = appName
app.Usage = appUsage
app.Version = "0.0.1"
app.Flags = flags
app.Action = run
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
os.Exit(0)
}