-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.go
540 lines (454 loc) · 14.5 KB
/
generate.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
package main
import (
"os"
"flag"
"strings"
"path/filepath"
"github.com/fatih/color"
"text/template"
)
func main() {
color.New(color.FgBlue, color.Bold).Println("Welcome to Gonfigen, the Gonfig generator")
tn := flag.String("type", "", "Targetted type")
pckg := flag.String("package", "", "Targetted package")
pr := flag.String("root", "..", "Project root")
flag.Parse()
typeNames := *tn
packg := *pckg
projectRoot := *pr
if typeNames == "" {
panic("No type set")
}
if packg == "" {
color.Blue("No package set, guessing from current directory.\n")
gopath := os.Getenv("GOPATH")
if (gopath == "") {
panic("Can not guess package with no $GOPATH set!")
}
currDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
panic(err)
}
gopathParts := strings.Split(gopath, string(os.PathListSeparator))
for _, p := range(gopathParts) {
srcDir := p + string(os.PathSeparator) + "src" + string(os.PathSeparator)
if strings.HasPrefix(currDir, srcDir) {
packg = strings.TrimPrefix(currDir, srcDir)
break
}
}
if (packg == "") {
panic("Can not guess package from available $GOPATH!")
}
}
color.New(color.FgBlue).Printf("Working with %s package and %s type.\n", packg, typeNames)
currDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
panic(err)
}
cmdDir := currDir + string(os.PathSeparator) + projectRoot + string(os.PathSeparator) + "cmd" + string(os.PathSeparator) + "gonfig" + string(os.PathSeparator)
os.MkdirAll(cmdDir, 0777)
f, err := os.Create(cmdDir + string(os.PathSeparator) + "gonfig.go")
if err != nil {
panic(err)
}
defer f.Close()
packageParts := strings.Split(packg, "/")
typeNames = packageParts[len(packageParts)-1] + "." + typeNames
asd := struct{
Package string
Type string
}{
Package: packg,
Type: typeNames,
}
var tpl = template.Must(template.New("").Parse(tplData))
if err = tpl.Execute(f, asd); err != nil {
panic(err)
}
pth, err := filepath.Abs(f.Name())
if err != nil {
panic(err)
}
color.New(color.FgGreen, color.Bold).Printf("Gonfig CLI generated to %s\n", pth)
f2, err := os.Create(currDir + string(os.PathSeparator) + projectRoot + string(os.PathSeparator) + "gonfig_loaders.go")
if err != nil {
panic(err)
}
defer f2.Close()
var tpl2 = template.Must(template.New("").Parse(GonfigLoadersTplData))
if err = tpl2.Execute(f2, asd); err != nil {
panic(err)
}
pth2, err := filepath.Abs(f2.Name())
if err != nil {
panic(err)
}
color.New(color.FgGreen, color.Bold).Printf("Gonfig loaders generated to %s\n", pth2)
}
const tplData = `package main
import (
"reflect"
"bufio"
"os"
"github.com/fatih/color"
"github.com/BurntSushi/toml"
"flag"
"strings"
"strconv"
"fmt"
"{{.Package}}"
)
func main() {
destination := *flag.String("destination", "gonfig.toml", "Path generated toml file")
template := *flag.String("template", "", "Path to template toml file")
nonInteractive := flag.Bool("non-interactive", false, "Dump the template/defaults")
flag.Parse()
regeneratingDestination := false
if template == "" {
template = "gonfig.dist.toml"
if _, err := os.Stat(destination); err == nil {
template = destination
regeneratingDestination = true
}
}
color.New(color.FgBlue, color.Bold).Println("Welcome to Gonfig!")
var n = {{.Type}}{}
color.New(color.FgBlue).Printf("Working with %s configuration object.\n", reflect.TypeOf(n))
if regeneratingDestination {
color.New(color.FgBlue).Printf("Destination (%s) already exists, loading values from it.\n", template)
} else {
color.New(color.FgBlue).Printf("Reading template data from %s\n", template)
}
toml.DecodeFile(template, &n)
if *nonInteractive {
color.Blue("Non-interactive mode: skipping user input.")
} else {
color.Blue("Now let's setup the configuration values:")
setStructValue("", &n)
color.Green("Thank you, that will be all. Generating config...")
}
// open output file
fo, err := os.Create(destination)
if err != nil {
panic(err)
}
// close fo on exit and check for its returned error
defer func() {
if err := fo.Close(); err != nil {
panic(err)
}
}()
toml.NewEncoder(fo).Encode(n);
color.New(color.FgGreen, color.Bold).Printf("Config successfully generated to '%s'. Exiting.", destination)
}
func setStructValue(name string, s interface{}) {
numField := reflect.TypeOf(s).Elem().NumField()
rv := reflect.ValueOf(s).Elem()
for i := 0; i < numField; i++ {
// if struct, recursion?
field := rv.Field(i)
n := rv.Type().Field(i).Name
if name != "" {
n = fmt.Sprintf(" %s", n)
}
setFieldValue(n, field, 0)
}
}
func setFieldValue(name string, field reflect.Value, indent int) (error) {
//todo: move this out at some point
reader := bufio.NewReader(os.Stdin)
p := printer{indent}
FieldInputLoop:
for ; ; {
switch field.Kind() {
case reflect.String:
s := color.New(color.FgYellow, color.Bold).Sprint(name) + color.New(color.FgYellow).Sprintf(" (%s)", field) + color.New(color.FgYellow, color.Bold).Sprint(": ")
p.Print(s)
line, tooLong, err := reader.ReadLine()
if tooLong {
p.Print(color.RedString("Input too long"))
} else if err != nil {
p.Print(color.RedString("Unable to parse! %s", err))
} else {
if len(line) > 0 {
field.SetString(string(line))
}
break FieldInputLoop
}
case reflect.Bool:
p.Print(color.New(color.FgYellow, color.Bold).Sprint(name))
if field.Bool() {
p.Print(color.New(color.FgYellow).Sprintf(" (Y/n): ", ))
} else {
p.Print(color.New(color.FgYellow).Sprintf(" (y/N): ", ))
}
line, tooLong, err := reader.ReadLine()
if tooLong {
p.Print(color.RedString("Input too long."))
} else if err != nil {
p.Print(color.RedString("Unable to parse. %s", err))
} else if l := strings.ToLower(string(line)); l != "y" && l != "n" && l != "" {
p.Print(color.RedString("Invalid value, only Y/y/N/n allowed."))
} else {
if len(line) > 0 {
field.SetBool(strings.ToLower(string(line)) == "y")
}
break FieldInputLoop
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
p.Print(color.New(color.FgYellow, color.Bold).Sprint(name))
p.Print(color.New(color.FgYellow).Sprintf(" (%v)", field.Int()))
p.Print(color.New(color.FgYellow, color.Bold).Sprint(": "))
line, tooLong, err := reader.ReadLine()
if tooLong {
p.Print(color.RedString("Input too long."))
} else if err != nil {
p.Print(color.RedString("Unable to parse. %s", err))
} else {
if string(line) == "" {
break FieldInputLoop
}
bitSize := field.Type().Bits()
val, err := strconv.ParseInt(string(line), 10, bitSize)
if err != nil {
message := "Error parsing integer."
if e, ok := err.(*strconv.NumError); ok {
switch e.Err {
case strconv.ErrSyntax:
message += " Wrong syntax."
case strconv.ErrRange:
message += fmt.Sprintf(" Value out of range, destination type is %s.", field.Kind())
}
}
p.Print(color.RedString(message))
} else {
field.Set(reflect.ValueOf(val).Convert(field.Type()))
break FieldInputLoop
}
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
p.Print(color.New(color.FgYellow, color.Bold).Sprint(name))
p.Print(color.New(color.FgYellow).Sprintf(" (%v)", field.Uint()))
p.Print(color.New(color.FgYellow, color.Bold).Sprint(": "))
line, tooLong, err := reader.ReadLine()
if tooLong {
p.Print(color.RedString("Input too long."))
} else if err != nil {
p.Print(color.RedString("Unable to parse. %s", err))
} else {
if string(line) == "" {
break FieldInputLoop
}
bitSize := field.Type().Bits()
val, err := strconv.ParseUint(string(line), 10, bitSize)
if err != nil {
message := "Error parsing unsigned integer."
if e, ok := err.(*strconv.NumError); ok {
switch e.Err {
case strconv.ErrSyntax:
message += " Wrong syntax."
case strconv.ErrRange:
message += fmt.Sprintf(" Value out of range, destination type is %s.", field.Kind())
}
}
p.Print(color.RedString(message))
} else {
field.Set(reflect.ValueOf(val).Convert(field.Type()))
break FieldInputLoop
}
}
case reflect.Float32, reflect.Float64:
p.Print(color.New(color.FgYellow, color.Bold).Sprint(name))
p.Print(color.New(color.FgYellow).Sprintf(" (%v)", field.Float()))
p.Print(color.New(color.FgYellow, color.Bold).Sprint(": "))
line, tooLong, err := reader.ReadLine()
if tooLong {
p.Print(color.RedString("Input too long."))
} else if err != nil {
p.Print(color.RedString("Unable to parse. %s", err))
} else {
if string(line) == "" {
break FieldInputLoop
}
bitSize := field.Type().Bits()
val, err := strconv.ParseFloat(string(line), bitSize)
if err != nil {
message := "Error parsing float."
if e, ok := err.(*strconv.NumError); ok {
switch e.Err {
case strconv.ErrSyntax:
message += " Wrong syntax."
case strconv.ErrRange:
message += fmt.Sprintf(" Value out of range, destination type is %s.", field.Kind())
}
}
p.Print(color.RedString(message))
} else {
field.Set(reflect.ValueOf(val).Convert(field.Type()))
break FieldInputLoop
}
}
case reflect.Array, reflect.Slice:
SliceLoop:
for ; ; {
//load/show defaults
//remove/add items
p.Print(color.New(color.FgYellow, color.Bold).Sprintln(name + ":"))
if field.Len() > 0 {
p.Print(color.New(color.FgYellow).Sprintf(" Existing values (index : value):\n"))
for i := 0; i < field.Len(); i++ {
p.Print(color.New(color.FgYellow).Sprintf(" %v: %v\n", i, field.Index(i)))
}
p.Print(color.New(color.FgYellow).Sprintf(" (A)dd/(R)emove/(P)urge/(N)ext (N): "))
} else {
p.Print(color.New(color.FgYellow).Sprintf(" (A)dd/(P)urge/(N)ext (N): "))
}
line, tooLong, err := reader.ReadLine()
if tooLong {
p.Print(color.RedString(" Input too long."))
} else if err != nil {
p.Print(color.RedString(" Unable to parse. %s", err))
} else {
if string(line) == "" {
line = []byte("n")
}
switch strings.ToLower(string(line)) {
case "n": break SliceLoop
case "p":
field.Set(reflect.New(field.Type()).Elem())
case "r":
p.Print(color.New(color.FgYellow).Sprintf(" Index to remove: "))
line, tooLong, err := reader.ReadLine()
if tooLong {
p.Print(color.RedString(" Input too long."))
} else if err != nil {
p.Print(color.RedString(" Unable to parse. %s", err))
} else {
val, err := strconv.ParseInt(string(line), 10, strconv.IntSize)
if err != nil {
message := " Error parsing integer."
if e, ok := err.(*strconv.NumError); ok {
switch e.Err {
case strconv.ErrSyntax:
message += " Wrong syntax."
case strconv.ErrRange:
message += " Value out of range, destination type is int64."
}
}
p.Print(color.RedString(message))
} else {
if int(val) >= field.Len() {
p.Print(color.RedString(" Index out of bounds. Index must be between 0 and %v", field.Len()))
} else {
field.Set(reflect.AppendSlice(field.Slice(0, int(val)), field.Slice(int(val) + 1, field.Len())))
}
//break FieldInputLoop
}
}
case "a":
x := reflect.New(field.Type().Elem()).Elem()
setFieldValue(fmt.Sprintf("%s.%v", name, field.Len()), x, indent + 1)
field.Set(reflect.Append(field, x))
default:
p.Print(color.RedString(" Unknown operation. Only A/a/R/r/P/p/N/n are allowed."))
}
p.Print(color.YellowString(" ---------------\n"))
}
}
break FieldInputLoop
case reflect.Map:
MapLoop:
for ; ; {
//load/show defaults
//remove/add items
p.Print(color.New(color.FgYellow, color.Bold).Sprintln(name + ":"))
if field.Len() > 0 {
p.Print(color.New(color.FgYellow).Sprintf(" Existing values (index : value):\n"))
keys := field.MapKeys()
for i := 0; i < len(keys); i++ {
p.Print(color.New(color.FgYellow).Sprintf(" %v : %v\n", keys[i], field.MapIndex(keys[i])))
}
p.Print(color.New(color.FgYellow).Sprintf(" (A)dd/(R)emove/(P)urge/(N)ext (N): "))
} else {
p.Print(color.New(color.FgYellow).Sprintf(" (A)dd/(P)urge/(N)ext (N): "))
}
line, tooLong, err := reader.ReadLine()
if tooLong {
p.Print(color.RedString(" Input too long."))
} else if err != nil {
p.Print(color.RedString(" Unable to parse. %s", err))
} else {
if string(line) == "" {
line = []byte("n")
}
switch strings.ToLower(string(line)) {
case "n": break MapLoop
case "p":
field.Set(reflect.New(field.Type()).Elem())
case "r":
x := reflect.New(field.Type().Key()).Elem()
setFieldValue(fmt.Sprint(" Index to remove:"), x, indent + 1)
if field.MapIndex(x) == (reflect.Value{}) {
p.Print(color.RedString(" Key not found."))
} else {
field.SetMapIndex(x, reflect.Value{})
}
case "a":
k := reflect.New(field.Type().Key()).Elem()
setFieldValue(fmt.Sprint(" New index:"), k, indent + 1)
v := reflect.New(field.Type().Elem()).Elem()
setFieldValue(fmt.Sprint(" New value:"), v, indent + 1)
if field.Len() == 0 {
field.Set(reflect.MakeMap(reflect.MapOf(field.Type().Key(), field.Type().Elem())))
}
field.SetMapIndex(k, v)
default:
p.Print(color.RedString(" Unknown operation. Only A/a/R/r/P/p/N/n are allowed."))
}
p.Print(color.YellowString("---------------\n"))
}
}
break FieldInputLoop
case reflect.Struct:
p.Print(color.New(color.FgYellow, color.Bold).Sprint(name + ":\n"))
numField := field.NumField()
for i := 0; i < numField; i++ {
f := field.Field(i)
n := field.Type().Field(i).Name
if name != "" {
n = fmt.Sprintf("%s", n)
}
setFieldValue(n, f, indent + 1)
}
break FieldInputLoop
default: panic(field.Kind())
}
}
return nil
}
type printer struct {
indent int
}
func (p printer) Print(s string) {
fmt.Print(strings.Repeat(" ", p.indent * 2) + s)
}
func (p printer) Println(s string) {
fmt.Println(strings.Repeat(" ", p.indent * 2) + s)
}`
const GonfigLoadersTplData = `package main
import (
"path/filepath"
"os"
"github.com/BurntSushi/toml"
"{{.Package}}"
)
func LoadConfig() (n {{.Type}}, err error) {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
return
}
_, err = toml.DecodeFile(dir + string(os.PathSeparator) + "gonfig.toml", &n)
return
}
`