forked from goadesign/goa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
48 lines (40 loc) · 1.39 KB
/
command.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
package gengen
import (
"fmt"
"path/filepath"
"github.com/goadesign/goa/goagen/codegen"
"github.com/goadesign/goa/goagen/meta"
)
var (
// GenPkgPath contains the path to the third party generator Go package.
GenPkgPath string
// GenPkgName contains the name of the third party generator Go package.
GenPkgName string
)
// Command is the goa generic generator command line data structure.
// It implements meta.Command.
type Command struct {
*codegen.BaseCommand
}
// NewCommand instantiates a new command.
func NewCommand() *Command {
base := codegen.NewBaseCommand("gen", "Invoke third party generator")
return &Command{BaseCommand: base}
}
// RegisterFlags registers the command line flags with the given registry.
func (c *Command) RegisterFlags(r codegen.FlagRegistry) {
r.Flags().StringVar(&GenPkgPath, "pkg-path", "", "Go package path to generator package. The package must implement the Generate global function.")
r.Flags().StringVar(&GenPkgName, "pkg-name", "", "Go package name of generator package. Defaults to name of inner most directory in package path.")
}
// Run simply calls the meta generator.
func (c *Command) Run() ([]string, error) {
if GenPkgName == "" {
GenPkgName = filepath.ToSlash(filepath.Base(GenPkgPath))
}
gen := meta.NewGenerator(
fmt.Sprintf("%s.Generate", GenPkgName),
[]*codegen.ImportSpec{codegen.SimpleImport(GenPkgPath)},
nil,
)
return gen.Generate()
}