/
cmd.go
79 lines (67 loc) · 1.5 KB
/
cmd.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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"gopkg.in/src-d/go-kallax.v1/generator"
"gopkg.in/urfave/cli.v1"
)
func main() {
app := cli.NewApp()
app.Name = "kallax"
app.Version = "1.1.3"
app.Usage = "generate kallax models"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "input",
Value: ".",
Usage: "Input package directory",
},
cli.StringFlag{
Name: "output",
Value: "kallax.go",
Usage: "Output file name",
},
cli.StringSliceFlag{
Name: "exclude, e",
Usage: "List of excluded files from the package when generating the code for your models. Use this to exclude files in your package that uses the generated code. You can use this flag as many times as you want.",
},
}
app.Action = generateModels
app.Commands = []cli.Command{
{
Name: "gen",
Usage: "Generate kallax models",
Action: app.Action,
Flags: app.Flags,
},
}
app.Run(os.Args)
}
func generateModels(c *cli.Context) error {
input := c.String("input")
output := c.String("output")
excluded := c.StringSlice("exclude")
if !isDirectory(input) {
return fmt.Errorf("kallax: Input path should be a directory %s", input)
}
p := generator.NewProcessor(input, excluded)
pkg, err := p.Do()
if err != nil {
return err
}
gen := generator.NewGenerator(filepath.Join(input, output))
err = gen.Generate(pkg)
if err != nil {
return err
}
return nil
}
func isDirectory(name string) bool {
info, err := os.Stat(name)
if err != nil {
log.Fatal(err)
}
return info.IsDir()
}