/
new.go
204 lines (177 loc) · 4.71 KB
/
new.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
package new
import (
"fmt"
"os"
"path"
"strings"
"github.com/asim/go-micro/cmd/gomu/cmd"
"github.com/asim/go-micro/cmd/gomu/generator"
tmpl "github.com/asim/go-micro/cmd/gomu/generator/template"
"github.com/urfave/cli/v2"
)
var flags []cli.Flag = []cli.Flag{
&cli.BoolFlag{
Name: "jaeger",
Usage: "Generate Jaeger tracer files",
},
&cli.BoolFlag{
Name: "kubernetes",
Usage: "Generate Kubernetes resource files",
},
&cli.BoolFlag{
Name: "skaffold",
Usage: "Generate Skaffold files",
},
}
// NewCommand returns a new new cli command.
func init() {
cmd.Register(&cli.Command{
Name: "new",
Usage: "Create a project template",
Subcommands: []*cli.Command{
{
Name: "client",
Usage: "Create a client template, e.g. " + cmd.App().Name + " new client [github.com/auditemarlow/]helloworld",
Action: Client,
Flags: flags,
},
{
Name: "function",
Usage: "Create a function template, e.g. " + cmd.App().Name + " new function [github.com/auditemarlow/]helloworld",
Action: Function,
Flags: flags,
},
{
Name: "service",
Usage: "Create a service template, e.g. " + cmd.App().Name + " new service [github.com/auditemarlow/]helloworld",
Action: Service,
Flags: flags,
},
},
})
}
func Client(ctx *cli.Context) error {
return createProject(ctx, "client")
}
// Function creates a new function project template. Exits on error.
func Function(ctx *cli.Context) error {
return createProject(ctx, "function")
}
// Service creates a new service project template. Exits on error.
func Service(ctx *cli.Context) error {
return createProject(ctx, "service")
}
func createProject(ctx *cli.Context, pt string) error {
arg := ctx.Args().First()
if len(arg) == 0 {
return cli.ShowSubcommandHelp(ctx)
}
client := pt == "client"
name, vendor := getNameAndVendor(arg)
dir := name
if client {
dir += "-client"
}
if path.IsAbs(dir) {
fmt.Println("must provide a relative path as service name")
return nil
}
if _, err := os.Stat(dir); !os.IsNotExist(err) {
return fmt.Errorf("%s already exists", dir)
}
fmt.Printf("creating %s %s\n", pt, name)
g := generator.New(
generator.Service(name),
generator.Vendor(vendor),
generator.Directory(dir),
generator.Client(client),
generator.Jaeger(ctx.Bool("jaeger")),
generator.Skaffold(ctx.Bool("skaffold")),
)
files := []generator.File{
{".dockerignore", tmpl.DockerIgnore},
{".gitignore", tmpl.GitIgnore},
{"Dockerfile", tmpl.Dockerfile},
{"Makefile", tmpl.Makefile},
{"go.mod", tmpl.Module},
}
switch pt {
case "client":
files = append(files, []generator.File{
{"main.go", tmpl.MainCLT},
}...)
case "function":
files = append(files, []generator.File{
{"handler/" + name + ".go", tmpl.HandlerFNC},
{"main.go", tmpl.MainFNC},
{"proto/" + name + ".proto", tmpl.ProtoFNC},
}...)
case "service":
files = append(files, []generator.File{
{"handler/" + name + ".go", tmpl.HandlerSRV},
{"main.go", tmpl.MainSRV},
{"proto/" + name + ".proto", tmpl.ProtoSRV},
}...)
default:
return fmt.Errorf("%s project type not supported", pt)
}
if ctx.Bool("kubernetes") || ctx.Bool("skaffold") {
files = append(files, []generator.File{
{"plugins.go", tmpl.Plugins},
{"resources/clusterrole.yaml", tmpl.KubernetesClusterRole},
{"resources/configmap.yaml", tmpl.KubernetesEnv},
{"resources/deployment.yaml", tmpl.KubernetesDeployment},
{"resources/rolebinding.yaml", tmpl.KubernetesRoleBinding},
}...)
}
if ctx.Bool("skaffold") {
files = append(files, []generator.File{
{"skaffold.yaml", tmpl.SkaffoldCFG},
}...)
}
if err := g.Generate(files); err != nil {
return err
}
var comments []string
if client {
comments = clientComments(name, dir)
} else {
comments = protoComments(name, dir)
}
for _, comment := range comments {
fmt.Println(comment)
}
return nil
}
func clientComments(name, dir string) []string {
return []string{
"\ninstall dependencies:",
"\ncd " + dir,
"make update tidy",
}
}
func protoComments(name, dir string) []string {
return []string{
"\ndownload protoc zip packages (protoc-$VERSION-$PLATFORM.zip) and install:",
"\nvisit https://github.com/protocolbuffers/protobuf/releases/latest",
"\ndownload protobuf for go-micro:",
"\ngo get -u google.golang.org/protobuf/proto",
"go install github.com/golang/protobuf/protoc-gen-go@latest",
"go install github.com/asim/go-micro/cmd/protoc-gen-micro/v4@latest",
"\ncompile the proto file " + name + ".proto and install dependencies:",
"\ncd " + dir,
"make proto update tidy",
}
}
func getNameAndVendor(s string) (string, string) {
var n string
var v string
if i := strings.LastIndex(s, "/"); i == -1 {
n = s
v = ""
} else {
n = s[i+1:]
v = s[:i+1]
}
return n, v
}