-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.go
78 lines (70 loc) · 1.49 KB
/
action.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
package checker
import (
"fmt"
"log"
"strings"
"github.com/urfave/cli"
)
var Usage = "generate struct checker function"
var Description = "make a Struct.Validate() function"
var Flags = []cli.Flag{
//jsonfield -t UpdateParam -n Validate -g tagname -o checkers.go
cli.StringFlag{
Name: "type,t",
Usage: "the `TYPE` to map field",
},
cli.StringFlag{
Name: "name,n",
Usage: "the `FUNCTION` to generate, default is Validate",
},
cli.StringFlag{
Name: "tag,g",
Usage: "tag name to define the checker rule, defualt is checker",
},
cli.StringSliceFlag{
Name: "import,i",
Usage: "the requried imports",
},
cli.StringFlag{
Name: "output,o",
Usage: "the `FILE` to output",
},
}
func Action(c *cli.Context) error {
t := c.String("type")
if t == "" {
return fmt.Errorf("type should be given")
}
tag := c.String("tag")
if tag == "" {
tag = "checker"
}
name := c.String("name")
if name == "" {
name = "Validate"
}
output := c.String("output")
config := &Config{
Type: t,
Output: output,
TagName: tag,
Name: name,
}
imports := c.StringSlice("import")
configImports := map[string]string{}
for _, impt := range imports {
kv := strings.Split(impt, ":")
if len(kv) == 1 {
path := kv[0]
names := strings.Split(path, "/")
name := names[len(names)-1]
configImports[name] = path
} else {
configImports[kv[0]] = kv[1]
}
}
config.Imports = configImports
g := NewGenerator()
log.Printf("start generate chekcer:")
return g.Generate(config)
}