forked from goadesign/goa
/
main.go
252 lines (225 loc) · 7.16 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/goadesign/goa/goagen/codegen"
"github.com/goadesign/goa/goagen/meta"
"github.com/goadesign/goa/goagen/utils"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
// These are packages required by the generated code but not by goagen.
// We list them here so that `go get` picks them up.
_ "gopkg.in/yaml.v2"
)
func main() {
var (
files []string
err error
terminatedByUser bool
)
// rootCmd is the base command used when goagen is called with no argument.
rootCmd := &cobra.Command{
Use: "goagen",
Short: "goa code generation tool",
Long: `The goagen tool generates artifacts from a goa service design package.
Each command supported by the tool produces a specific type of artifacts. For example
the "app" command generates the code that supports the service controllers.
The "bootstrap" command runs the "app", "main", "client" and "swagger" commands generating the
controllers supporting code and main skeleton code (if not already present) as well as a client
package and tool and the Swagger specification for the API.
`}
var (
cwd, designPkg string
debug bool
)
cwd, err = os.Getwd()
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
rootCmd.PersistentFlags().StringVarP(&cwd, "out", "o", cwd, "output directory")
rootCmd.PersistentFlags().StringVarP(&designPkg, "design", "d", "", "design package import path")
rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "enable debug mode, does not cleanup temporary files.")
// appCmd implements the "app" command.
var (
pkg string
notest bool
)
appCmd := &cobra.Command{
Use: "app",
Short: "Generate application code",
Run: func(c *cobra.Command, _ []string) { files, err = run("genapp", c) },
}
appCmd.Flags().StringVar(&pkg, "pkg", "app", "Name of generated Go package containing controllers supporting code (contexts, media types, user types etc.)")
appCmd.Flags().BoolVar(¬est, "notest", false, "Prevent generation of test helpers")
rootCmd.AddCommand(appCmd)
// mainCmd implements the "main" command.
var (
force bool
)
mainCmd := &cobra.Command{
Use: "main",
Short: "Generate application scaffolding",
Run: func(c *cobra.Command, _ []string) { files, err = run("genmain", c) },
}
mainCmd.Flags().BoolVar(&force, "force", false, "overwrite existing files")
rootCmd.AddCommand(mainCmd)
// clientCmd implements the "client" command.
var (
toolDir, tool string
notool bool
)
clientCmd := &cobra.Command{
Use: "client",
Short: "Generate client package and tool",
Run: func(c *cobra.Command, _ []string) { files, err = run("genclient", c) },
}
clientCmd.Flags().StringVar(&pkg, "pkg", "client", "Name of generated client Go package")
clientCmd.Flags().StringVar(&toolDir, "tooldir", "tool", "Name of generated tool directory")
clientCmd.Flags().StringVar(&tool, "tool", "[API-name]-cli", "Name of generated tool")
clientCmd.Flags().BoolVar(¬ool, "notool", false, "Prevent generation of cli tool")
rootCmd.AddCommand(clientCmd)
// swaggerCmd implements the "swagger" command.
swaggerCmd := &cobra.Command{
Use: "swagger",
Short: "Generate Swagger",
Run: func(c *cobra.Command, _ []string) { files, err = run("genswagger", c) },
}
rootCmd.AddCommand(swaggerCmd)
// jsCmd implements the "js" command.
var (
timeout = time.Duration(20) * time.Second
scheme, host string
noexample bool
)
jsCmd := &cobra.Command{
Use: "js",
Short: "Generate JavaScript client",
Run: func(c *cobra.Command, _ []string) { files, err = run("genjs", c) },
}
jsCmd.Flags().DurationVar(&timeout, "timeout", timeout, `the duration before the request times out.`)
jsCmd.Flags().StringVar(&scheme, "scheme", "", `the URL scheme used to make requests to the API, defaults to the scheme defined in the API design if any.`)
jsCmd.Flags().StringVar(&host, "host", "", `the API hostname, defaults to the hostname defined in the API design if any`)
jsCmd.Flags().BoolVar(&noexample, "noexample", false, `Skip generation of example HTML and controller`)
rootCmd.AddCommand(jsCmd)
// schemaCmd implements the "schema" command.
schemaCmd := &cobra.Command{
Use: "schema",
Short: "Generate JSON Schema",
Run: func(c *cobra.Command, _ []string) { files, err = run("genschema", c) },
}
rootCmd.AddCommand(schemaCmd)
// genCmd implements the "gen" command.
var (
pkgPath string
)
genCmd := &cobra.Command{
Use: "gen",
Short: "Run third-party generator",
Run: func(c *cobra.Command, _ []string) { files, err = runGen(c) },
}
genCmd.Flags().StringVar(&pkgPath, "pkg-path", "", "Package import path of generator. The package must implement the Generate global function.")
rootCmd.AddCommand(genCmd)
// boostrapCmd implements the "bootstrap" command.
bootCmd := &cobra.Command{
Use: "bootstrap",
Short: `Equivalent to running the "app", "main", "client" and "swagger" commands.`,
Run: func(c *cobra.Command, a []string) {
appCmd.Run(c, a)
if err != nil {
return
}
prev := files
mainCmd.Run(c, a)
if err != nil {
return
}
prev = append(prev, files...)
clientCmd.Run(c, a)
if err != nil {
return
}
prev = append(prev, files...)
swaggerCmd.Run(c, a)
files = append(prev, files...)
},
}
rootCmd.AddCommand(bootCmd)
// Now proceed with code generation
cleanup := func() {
for _, f := range files {
os.RemoveAll(f)
}
}
go utils.Catch(nil, func() {
terminatedByUser = true
})
rootCmd.Execute()
if terminatedByUser {
cleanup()
return
}
if err != nil {
cleanup()
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
rels := make([]string, len(files))
cd, _ := os.Getwd()
for i, f := range files {
r, err := filepath.Rel(cd, f)
if err == nil {
rels[i] = r
} else {
rels[i] = f
}
}
fmt.Println(strings.Join(rels, "\n"))
}
func run(pkg string, c *cobra.Command) ([]string, error) {
pkgPath := fmt.Sprintf("github.com/goadesign/goa/goagen/gen_%s", pkg[3:])
pkgSrcPath, err := codegen.PackageSourcePath(pkgPath)
if err != nil {
return nil, fmt.Errorf("invalid plugin package import path: %s", err)
}
pkgName, err := codegen.PackageName(pkgSrcPath)
if err != nil {
return nil, fmt.Errorf("invalid package import path: %s", err)
}
return generate(pkgName, pkgPath, c)
}
func runGen(c *cobra.Command) ([]string, error) {
pkgPath := c.Flag("pkg-path").Value.String()
pkgSrcPath, err := codegen.PackageSourcePath(pkgPath)
if err != nil {
return nil, fmt.Errorf("invalid plugin package import path: %s", err)
}
pkgName, err := codegen.PackageName(pkgSrcPath)
if err != nil {
return nil, fmt.Errorf("invalid plugin package import path: %s", err)
}
return generate(pkgName, pkgPath, c)
}
func generate(pkgName, pkgPath string, c *cobra.Command) ([]string, error) {
m := make(map[string]string)
c.Flags().Visit(func(f *pflag.Flag) {
if f.Name != "pkg-path" {
m[f.Name] = f.Value.String()
}
})
if _, ok := m["out"]; !ok {
m["out"] = c.Flag("out").DefValue
}
gen, err := meta.NewGenerator(
pkgName+".Generate",
[]*codegen.ImportSpec{codegen.SimpleImport(pkgPath)},
m,
)
if err != nil {
return nil, err
}
return gen.Generate()
}