-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
64 lines (54 loc) · 1.29 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
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/iolivernguyen/ggen/ggen"
"github.com/iolivernguyen/ggen/plugins/sample"
)
var flClean = flag.Bool("clean", false, "clean generated files without generating new files")
var flPlugin = flag.String("plugin", "", "comma separated list of plugins for generating (default to all plugins)")
var flNamespace = flag.String("namespace", "", "github.com/myproject")
var flVerbose = flag.Int("verbose", 0, "enable verbosity (0: info, 4: debug, 8: more debug)")
func usage() {
const text = `
Usage: ggen [OPTION] PATTERN ...
Options:
`
fmt.Print(text[1:])
flag.PrintDefaults()
}
func main() {
Start(
sample.New(), // sample plugin
)
}
func Start(plugins ...ggen.Plugin) {
flag.Parse()
patterns := flag.Args()
if len(patterns) == 0 {
usage()
os.Exit(2)
}
cfg := ggen.Config{
LogLevel: -ggen.LogLevel(*flVerbose),
CleanOnly: *flClean,
Namespace: *flNamespace,
GoimportsArgs: []string{}, // example: -local github.com/foo
}
cfg.RegisterPlugin(plugins...)
if *flPlugin != "" {
pluginNames := strings.Split(*flPlugin, ",")
for _, name := range pluginNames {
cfg.EnablePlugin(name)
}
}
must(ggen.Start(cfg, patterns...))
}
func must(err error) {
if err != nil {
fmt.Printf("%+v\n", err)
os.Exit(1)
}
}