-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
181 lines (154 loc) · 4.04 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
package main
import (
"flag"
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/iancoleman/strcase"
"golang.org/x/tools/go/packages"
)
const (
cliName = "goor"
cliVersion = "0.1"
)
// flags
var (
typeName = flag.String("type", "", "[required] a struct type name.")
output = flag.String("output", "", `[option] output file name (default "srcdir/<type>_constructor_gen.go").`)
pointer = flag.Bool("pointer", true, `[option] set the return value to a pointer when creating the constructor.`)
getter = flag.Bool("getter", false, "[option] when you create a constructor, you also create a getter.")
setter = flag.Bool("setter", false, "[option] when you create a constructor, you also create a setter.")
version = flag.Bool("version", false, "outputs the current version.")
)
// Usage is a replacement usage function for the flags package.
func Usage() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", cliName)
fmt.Fprintf(os.Stderr, "\t%s [flags] -type=[struct type name]\n", cliName)
fmt.Fprintf(os.Stderr, "For more information, see:\n")
fmt.Fprintf(os.Stderr, "\thttps://github.com/ttakuya50/goor\n")
fmt.Fprintf(os.Stderr, "Flags:\n")
flag.PrintDefaults()
}
func main() {
log.SetPrefix(fmt.Sprintf("%s:", cliName))
flag.Usage = Usage
flag.Parse()
if *version {
fmt.Fprintf(os.Stdout, "%s\n", cliVersion)
os.Exit(0)
}
if len(*typeName) == 0 {
flag.Usage()
os.Exit(2)
}
args := flag.Args()
if len(args) <= 0 {
// Default: process whole package in current directory.
args = []string{"."}
}
pkg := parsePackage(args)
astFiles, err := parseFiles(pkg.GoFiles)
if err != nil {
log.Fatalf("[error]parse files err: %v", err)
}
structType, err := searchStructType(astFiles)
if err != nil {
log.Fatalf("[error]: %v", err)
}
fileds, err := searchFiled(structType)
if err != nil {
log.Fatalf("[error]search filed err: %v", err)
}
src, err := createConstructor(pkg, fileds)
if err != nil {
log.Fatalf("[error]writing output: %s", err)
}
if err := ioutil.WriteFile(createFileName(args), src, 0644); err != nil {
log.Fatalf("[error] write file:%s", err)
}
}
// searchStructType search struct type.
func searchStructType(astFiles []*ast.File) (*ast.StructType, error) {
for _, astFile := range astFiles {
for _, decl := range astFile.Decls {
genDecl, ok := decl.(*ast.GenDecl)
if !ok {
continue
}
for _, spec := range genDecl.Specs {
typeSpec, ok := spec.(*ast.TypeSpec)
if !ok {
continue
}
if *typeName != typeSpec.Name.Obj.Name {
continue
}
structType, ok := typeSpec.Type.(*ast.StructType)
if !ok {
continue
}
return structType, nil
}
}
}
return nil, fmt.Errorf("struct and type names do not match")
}
// createFileName create a file name.
func createFileName(args []string) string {
if *output != "" {
return *output
}
var dir string
if len(args) == 1 && isDirectory(args[0]) {
dir = args[0]
} else {
dir = filepath.Dir(args[0])
}
return fmt.Sprintf("%s/%s_constructor_gen.go", dir, strcase.ToSnake(*typeName))
}
// isDirectory reports whether the named file is a directory.
func isDirectory(name string) bool {
info, err := os.Stat(name)
if err != nil {
log.Fatal(err)
}
return info.IsDir()
}
// parsePackage parse package.
func parsePackage(patterns []string) *packages.Package {
cfg := &packages.Config{
Mode: packages.NeedTypes |
packages.NeedSyntax |
packages.NeedTypesInfo |
packages.NeedName |
packages.NeedFiles |
packages.NeedImports,
Tests: false,
}
pkgs, err := packages.Load(cfg, patterns...)
if err != nil {
log.Fatal(err)
}
if len(pkgs) != 1 {
log.Fatalf("[error]: %d packages found", len(pkgs))
}
return pkgs[0]
}
// parseFiles parse files.
func parseFiles(files []string) ([]*ast.File, error) {
fset := token.NewFileSet()
astFiles := make([]*ast.File, 0, len(files))
for _, file := range files {
f, err := parser.ParseFile(fset, file, nil, parser.ParseComments)
if err != nil {
return nil, err
}
astFiles = append(astFiles, f)
}
return astFiles, nil
}