-
Notifications
You must be signed in to change notification settings - Fork 0
/
polygen.go
66 lines (53 loc) · 1.18 KB
/
polygen.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
package main
import (
"errors"
"fmt"
"os"
"strings"
"github.com/jessevdk/go-flags"
"github.com/dripolles/polygen/generator"
)
func main() {
gen := newGeneratorCommand()
parser := flags.NewParser(gen, flags.Default)
_, err := parser.Parse()
if err != nil {
if _, ok := err.(*flags.Error); ok {
parser.WriteHelp(os.Stdout)
}
os.Exit(1)
}
err = gen.Execute()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
type GeneratorCommand struct {
Types generator.TypeAssignments `short:"t" long:"types" required:"true"`
Args struct {
Source string `positional-arg-name:"source" description:"source file"`
Dest string `positional-arg-name:"destination" description:"destination file"`
} `positional-args:"true"`
}
func newGeneratorCommand() *GeneratorCommand {
return &GeneratorCommand{}
}
func (g *GeneratorCommand) Dest() string {
d := g.Args.Dest
if d == "" {
return d
}
if strings.HasSuffix(d, ".go") {
return d
}
return d + ".go"
}
func (g *GeneratorCommand) Execute() error {
if g.Args.Source == "" {
return errors.New("Source file must be provided")
}
gen := generator.NewGenerator(g.Types, g.Args.Source, g.Dest())
err := gen.Generate()
return err
}