-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
103 lines (89 loc) · 2.37 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
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
)
const namePostfix = "fields"
var (
tName = flag.String("tag", "field",
"tag name whose value will be used as the name of the field")
sNames = flag.String("struct", "",
"comma-separated list of struct names for which to generate fields; must be set")
cNames = flag.String("custom_name", "",
"comma-separated list of names, default is the struct names for which to generate fields")
output = flag.String("output", "",
"output file name; default file creates for each struct, with name: <struct_name>_fields.go")
genji = flag.Bool("genji", false,
"determining the generation of helpers to work with genji, casting and etc")
)
// usage is a replacement usage function for the flags package.
func usage() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\tgen-struct-fields -struct=StructName [flags]\n")
fmt.Fprintf(os.Stderr, "For more information, see:\n")
fmt.Fprintf(os.Stderr, "\thttps://github.com/abramlab/gen-struct-fields\n")
fmt.Fprintf(os.Stderr, "Flags:\n")
flag.PrintDefaults()
}
type parsedFlags struct {
tagName string
neededStructs map[string]*options
output string
genji bool
}
type options struct {
customName string
}
func parseFlags() *parsedFlags {
flag.Usage = usage
flag.Parse()
if *sNames == "" {
flag.Usage()
os.Exit(2)
}
structNames := strings.Split(*sNames, ",")
customNames := make([]string, len(structNames))
if *cNames != "" {
cns := strings.Split(*cNames, ",")
for i, name := range cns {
customNames[i] = name
}
}
neededStructs := make(map[string]*options)
for i := 0; i < len(structNames); i++ {
customName := structNames[i]
if customNames[i] != "" {
customName = customNames[i]
}
neededStructs[structNames[i]] = &options{
customName: customName,
}
}
return &parsedFlags{
tagName: *tName,
neededStructs: neededStructs,
output: *output,
genji: *genji,
}
}
func main() {
flags := parseFlags()
gen := &generator{
tagName: flags.tagName,
genTpls: []*Template{
basicTemplates,
},
}
if flags.genji {
gen.genTpls = append(gen.genTpls, genjiTemplates)
}
if err := gen.parse(flags.neededStructs); err != nil {
log.Fatalf("parsing files failed: %v", err)
}
if err := gen.generate(flags.output); err != nil {
log.Fatalf("generating files failed: %v", err)
}
}