forked from goadesign/goa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
355 lines (318 loc) · 9.91 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package main
import (
"encoding/json"
"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/goadesign/goa/version"
"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 (
designPkg string
debug bool
)
rootCmd.PersistentFlags().StringP("out", "o", ".", "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.")
// versionCmd implements the "version" command
versionCmd := &cobra.Command{
Use: "version",
Short: "Print the version number of goagen",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("goagen " + version.String() + "\nThe goa generation tool.")
},
}
rootCmd.AddCommand(versionCmd)
// 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...)
},
}
bootCmd.Flags().AddFlagSet(appCmd.Flags())
bootCmd.Flags().AddFlagSet(mainCmd.Flags())
bootCmd.Flags().AddFlagSet(clientCmd.Flags())
bootCmd.Flags().AddFlagSet(swaggerCmd.Flags())
rootCmd.AddCommand(bootCmd)
// cmdsCmd implements the commands command
// It lists all the commands and flags in JSON to enable shell integrations.
cmdsCmd := &cobra.Command{
Use: "commands",
Short: "Lists all commands and flags in JSON",
Run: func(c *cobra.Command, _ []string) { runCommands(rootCmd) },
}
rootCmd.AddCommand(cmdsCmd)
// 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
}
// turn "out" into an absolute path
var err error
m["out"], err = filepath.Abs(m["out"])
if err != nil {
return nil, err
}
gen, err := meta.NewGenerator(
pkgName+".Generate",
[]*codegen.ImportSpec{codegen.SimpleImport(pkgPath)},
m,
)
if err != nil {
return nil, err
}
return gen.Generate()
}
type (
rootCommand struct {
Name string `json:"name"`
Commands []*command `json:"commands"`
Flags []*flag `json:"flags"`
}
flag struct {
Long string `json:"long,omitempty"`
Short string `json:"short,omitempty"`
Description string `json:"description,omitempty"`
Argument string `json:"argument,omitempty"`
Required bool `json:"required"`
}
command struct {
Name string `json:"name"`
Flags []*flag `json:"flags,omitempty"`
}
)
func runCommands(root *cobra.Command) {
var (
gblFlags []*flag
cmds []*command
)
root.Flags().VisitAll(func(fl *pflag.Flag) {
gblFlags = append(gblFlags, flagJSON(fl))
})
cmds = make([]*command, len(root.Commands())-2)
j := 0
for _, cm := range root.Commands() {
if cm.Name() == "help" || cm.Name() == "commands" {
continue
}
cmds[j] = cmdJSON(cm, gblFlags)
j++
}
rc := rootCommand{os.Args[0], cmds, gblFlags}
b, _ := json.MarshalIndent(rc, "", " ")
fmt.Println(string(b))
}
// Lots of assumptions in here, it's OK for what we are doing
// Remember to update as goagen commands and flags evolve
//
// The flag argument values use variable names that cary semantic:
// $DIR for file system directories, $DESIGN_PKG for import path to Go goa design Go packages, $PKG
// for import path to any Go package.
func flagJSON(fl *pflag.Flag) *flag {
f := &flag{Long: fl.Name, Short: fl.Shorthand, Description: fl.Usage}
f.Required = fl.Name == "pkg-path" || fl.Name == "design"
switch fl.Name {
case "out":
f.Argument = "$DIR"
case "design":
f.Argument = "$DESIGN_PKG"
case "pkg-path":
f.Argument = "$PKG"
}
return f
}
func cmdJSON(cm *cobra.Command, flags []*flag) *command {
res := make([]*flag, len(flags))
for i, fl := range flags {
f := *fl
res[i] = &f
}
cm.Flags().VisitAll(func(fl *pflag.Flag) {
res = append(res, flagJSON(fl))
})
return &command{cm.Name(), res}
}