forked from goadesign/goa
/
main.go
104 lines (91 loc) · 2.42 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
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/goadesign/goa/goagen/codegen"
"github.com/goadesign/goa/goagen/gen_app"
"github.com/goadesign/goa/goagen/gen_client"
"github.com/goadesign/goa/goagen/gen_gen"
"github.com/goadesign/goa/goagen/gen_js"
"github.com/goadesign/goa/goagen/gen_main"
"github.com/goadesign/goa/goagen/gen_schema"
"github.com/goadesign/goa/goagen/gen_swagger"
"github.com/goadesign/goa/goagen/utils"
"github.com/spf13/cobra"
)
// Commands contains the list of all supported sub-commands.
var Commands = []codegen.Command{
&BootstrapCommand{},
genapp.NewCommand(),
genmain.NewCommand(),
genclient.NewCommand(),
genswagger.NewCommand(),
genjs.NewCommand(),
genschema.NewCommand(),
gengen.NewCommand(),
}
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 various 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.
`}
)
// Now proceed with code generation
cleanup := func() {
for _, f := range files {
os.RemoveAll(f)
}
}
go utils.Catch(nil, func() {
terminatedByUser = true
})
for _, command := range Commands {
run := command.Run
sub := &cobra.Command{
Use: command.Name(),
Short: command.Description(),
Run: func(cmd *cobra.Command, args []string) {
codegen.ExtraFlags = args
files, err = run()
},
}
command.RegisterFlags(sub)
codegen.RegisterFlags(sub)
RootCmd.AddCommand(sub)
}
codegen.RegisterFlags(RootCmd)
RootCmd.Execute()
if terminatedByUser {
cleanup()
return
}
if err != nil {
cleanup()
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
rels := make([]string, len(files))
cwd, err := os.Getwd()
for i, f := range files {
r, err := filepath.Rel(cwd, f)
if err == nil {
rels[i] = r
} else {
rels[i] = f
}
}
fmt.Println(strings.Join(rels, "\n"))
}