-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
360 lines (298 loc) · 7.35 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package main
//go:generate ./tpl.sh
//go:generate ./gen.sh models
import (
"database/sql"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"sort"
"strings"
"github.com/alexflint/go-arg"
"github.com/knq/dburl"
"github.com/knq/xo/internal"
"github.com/knq/xo/models"
_ "github.com/knq/xo/loaders"
_ "github.com/knq/xoutil"
)
func main() {
// circumvent all logic to just determine if xo was built with oracle
// support
if len(os.Args) == 2 && os.Args[1] == "--has-oracle-support" {
var out int
if _, ok := internal.SchemaLoaders["ora"]; ok {
out = 1
}
fmt.Fprintf(os.Stdout, "%d", out)
return
}
var err error
// get defaults
internal.Args = internal.NewDefaultArgs()
args := internal.Args
// parse args
arg.MustParse(args)
// process args
err = processArgs(args)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
// open database
err = openDB(args)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
defer args.DB.Close()
// load schema name
if args.Schema == "" {
args.Schema, err = args.Loader.SchemaName(args)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
// load defs into type map
if args.QueryMode {
err = args.Loader.ParseQuery(args)
} else {
err = args.Loader.LoadSchema(args)
}
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
// add xo
err = args.ExecuteTemplate(internal.XOTemplate, "xo_db", "", args)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
// output
err = writeTypes(args)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
// processArgs processs cli args.
func processArgs(args *internal.ArgType) error {
var err error
// get working directory
cwd, err := os.Getwd()
if err != nil {
return err
}
// determine out path
if args.Out == "" {
args.Path = cwd
} else {
// determine what to do with Out
fi, err := os.Stat(args.Out)
if err == nil && fi.IsDir() {
// out is directory
args.Path = args.Out
} else if err == nil && !fi.IsDir() {
// file exists (will truncate later)
args.Path = path.Dir(args.Out)
args.Filename = path.Base(args.Out)
// error if not split was set, but destination is not a directory
if !args.SingleFile {
return errors.New("output path is not directory")
}
} else if _, ok := err.(*os.PathError); ok {
// path error (ie, file doesn't exist yet)
args.Path = path.Dir(args.Out)
args.Filename = path.Base(args.Out)
// error if split was set, but dest doesn't exist
if !args.SingleFile {
return errors.New("output path must be a directory and already exist when not writing to a single file")
}
} else {
return err
}
}
// check user template path
if args.TemplatePath != "" {
fi, err := os.Stat(args.TemplatePath)
if err == nil && !fi.IsDir() {
return errors.New("template path is not directory")
} else if err != nil {
return errors.New("template path must exist")
}
}
// fix path
if args.Path == "." {
args.Path = cwd
}
// determine package name
if args.Package == "" {
args.Package = path.Base(args.Path)
}
// determine filename if not previously set
if args.Filename == "" {
args.Filename = args.Package + args.Suffix
}
// if query mode toggled, but no query, read Stdin.
if args.QueryMode && args.Query == "" {
buf, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return err
}
args.Query = string(buf)
}
// query mode parsing
if args.Query != "" {
args.QueryMode = true
}
// check that query type was specified
if args.QueryMode && args.QueryType == "" {
return errors.New("query type must be supplied for query parsing mode")
}
// query trim
if args.QueryMode && args.QueryTrim {
args.Query = strings.TrimSpace(args.Query)
}
// escape all
if args.EscapeAll {
args.EscapeSchemaName = true
args.EscapeTableNames = true
args.EscapeColumnNames = true
}
// if verbose
if args.Verbose {
models.XOLog = func(s string, p ...interface{}) {
fmt.Printf("SQL:\n%s\nPARAMS:\n%v\n\n", s, p)
}
}
return nil
}
// openDB attempts to open a database connection.
func openDB(args *internal.ArgType) error {
var err error
// parse dsn
u, err := dburl.Parse(args.DSN)
if err != nil {
return err
}
// save driver type
args.LoaderType = u.Driver
// grab loader
var ok bool
args.Loader, ok = internal.SchemaLoaders[u.Driver]
if !ok {
return errors.New("unsupported database type")
}
// open database connection
args.DB, err = sql.Open(u.Driver, u.DSN)
if err != nil {
return err
}
return nil
}
// files is a map of filenames to open file handles.
var files = map[string]*os.File{}
// getFile builds the filepath from the TBuf information, and retrieves the
// file from files. If the built filename is not already defined, then it calls
// the os.OpenFile with the correct parameters depending on the state of args.
func getFile(args *internal.ArgType, t *internal.TBuf) (*os.File, error) {
var f *os.File
var err error
// determine filename
var filename = strings.ToLower(t.Name) + args.Suffix
if args.SingleFile {
filename = args.Filename
}
filename = path.Join(args.Path, filename)
// lookup file
f, ok := files[filename]
if ok {
return f, nil
}
// default open mode
mode := os.O_RDWR | os.O_CREATE | os.O_TRUNC
// stat file to determine if file already exists
fi, err := os.Stat(filename)
if err == nil && fi.IsDir() {
return nil, errors.New("filename cannot be directory")
} else if _, ok = err.(*os.PathError); !ok && args.Append && t.TemplateType != internal.XOTemplate {
// file exists so append if append is set and not XO type
mode = os.O_APPEND | os.O_WRONLY
}
// skip
if t.TemplateType == internal.XOTemplate && fi != nil {
return nil, nil
}
// open file
f, err = os.OpenFile(filename, mode, 0666)
if err != nil {
return nil, err
}
// file didn't originally exist, so add package header
if fi == nil || !args.Append {
// add build tags
if args.Tags != "" {
f.WriteString(`// +build ` + args.Tags + "\n\n")
}
// execute
err = args.TemplateSet().Execute(f, "xo_package.go.tpl", args)
if err != nil {
return nil, err
}
}
// store file
files[filename] = f
return f, nil
}
// writeTypes writes the generated definitions.
func writeTypes(args *internal.ArgType) error {
var err error
out := internal.TBufSlice(args.Generated)
// sort segments
sort.Sort(out)
// loop, writing in order
for _, t := range out {
var f *os.File
// skip when in append and type is XO
if args.Append && t.TemplateType == internal.XOTemplate {
continue
}
// check if generated template is only whitespace/empty
bufStr := strings.TrimSpace(t.Buf.String())
if len(bufStr) == 0 {
continue
}
// get file and filename
f, err = getFile(args, &t)
if err != nil {
return err
}
// should only be nil when type == xo
if f == nil {
continue
}
// write segment
if !args.Append || (t.TemplateType != internal.TypeTemplate && t.TemplateType != internal.QueryTypeTemplate) {
_, err = t.Buf.WriteTo(f)
if err != nil {
return err
}
}
}
// build goimports parameters, closing files
params := []string{"-w"}
for k, f := range files {
params = append(params, k)
// close
err = f.Close()
if err != nil {
return err
}
}
// process written files with goimports
return exec.Command("goimports", params...).Run()
}