-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
85 lines (71 loc) · 2.34 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
package main
//go:generate ./genval -d examples/aliases -p aliases
//go:generate ./genval -d examples/simple -p simple
//go:generate ./genval -d examples/overriding -p overriding
//go:generate ./genval -d examples/complicated -p complicated
//go:generate ./genval -d examples/empty -p empty
import (
"flag"
"fmt"
"log"
"os"
"strings"
)
const (
version = "1.5"
)
var (
supportedTags = flag.String("tags", "", "supported tags as source for field naming")
outputFile = flag.String("outputFile", "validators.go", "output file name")
dir = flag.String("d", "", "directory with files to be validated")
pkg = flag.String("p", "", "package with files to be validated")
printVersion = flag.Bool("version", false, "print current version")
needValidatableCheck = flag.Bool("needValidatableCheck", true, "check struct on Validatable before calling Validate()")
excludeRegexp = flag.String("excludeRegexp", `(client\.go|client_mock\.go)`,
"regexp file names that generator should exclude, e.g. `(client\\.go|client_mock\\.go)`")
)
func main() {
flag.Parse()
if *printVersion {
fmt.Fprintf(os.Stdout, "genval version: %s\n", version)
os.Exit(0)
}
// if directory & package aren`t set then first argument is used for both flags
d, p := *dir, *pkg
if d == "" && p == "" {
args := flag.Args()
if len(args) != 1 {
flag.PrintDefaults()
os.Exit(1)
}
d, p = args[0], args[0]
}
supportedTagsSlice := strings.Split(*supportedTags, ",")
if len(supportedTagsSlice) >= 1 {
if supportedTagsSlice[0] != "" {
supportedTagsSlice = append([]string{""}, supportedTagsSlice...)
}
}
// supportedTags[0] = "" always to generate default validator.
for i, tag := range supportedTagsSlice {
supportedTagsSlice[i] = strings.ToUpper(tag)
}
cfg := config{
supportedTags: supportedTagsSlice,
dir: d,
pkg: p,
outputFile: *outputFile,
excludeRegexpStr: *excludeRegexp,
}
generate(cfg, *needValidatableCheck)
}
func generate(cfg config, needCheck bool) {
insp := NewInspector()
if err := insp.Inspect(cfg); err != nil {
log.Fatalf("unable to inspect structs for %q: %v", cfg.dir, err)
}
g := NewGenerator(insp.Result())
if err := g.Generate(cfg, needCheck); err != nil {
log.Fatalf("unable to generate validators for %q: %v", cfg.dir, err)
}
}