/
client_cli.go
241 lines (229 loc) Β· 6.73 KB
/
client_cli.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
package codegen
import (
"path"
"path/filepath"
"goa.design/goa/codegen"
"goa.design/goa/codegen/cli"
"goa.design/goa/expr"
)
// ClientCLIFiles returns the CLI files to generate a command-line client that
// makes gRPC requests.
func ClientCLIFiles(genpkg string, root *expr.RootExpr) []*codegen.File {
if len(root.API.GRPC.Services) == 0 {
return nil
}
var (
data []*cli.CommandData
svcs []*expr.GRPCServiceExpr
)
{
for _, svc := range root.API.GRPC.Services {
if len(svc.GRPCEndpoints) == 0 {
continue
}
sd := GRPCServices.Get(svc.Name())
command := cli.BuildCommandData(sd.Service)
for _, e := range sd.Endpoints {
flags, buildFunction := buildFlags(sd, e)
subcmd := cli.BuildSubcommandData(sd.Service.Name, e.Method, buildFunction, flags)
command.Subcommands = append(command.Subcommands, subcmd)
}
command.Example = command.Subcommands[0].Example
data = append(data, command)
svcs = append(svcs, svc)
}
}
var files []*codegen.File
{
for _, svr := range root.API.Servers {
files = append(files, endpointParser(genpkg, root, svr, data))
}
for i, svc := range svcs {
files = append(files, payloadBuilders(genpkg, svc, data[i]))
}
}
return files
}
// endpointParser returns the file that implements the command line parser that
// builds the client endpoint and payload necessary to perform a request.
func endpointParser(genpkg string, root *expr.RootExpr, svr *expr.ServerExpr, data []*cli.CommandData) *codegen.File {
pkg := codegen.SnakeCase(codegen.Goify(svr.Name, true))
fpath := filepath.Join(codegen.Gendir, "grpc", "cli", pkg, "cli.go")
title := svr.Name + " gRPC client CLI support package"
specs := []*codegen.ImportSpec{
{Path: "context"},
{Path: "flag"},
{Path: "fmt"},
{Path: "os"},
{Path: "strconv"},
{Path: "goa.design/goa", Name: "goa"},
{Path: "goa.design/goa/grpc", Name: "goagrpc"},
{Path: "google.golang.org/grpc", Name: "grpc"},
}
for _, svc := range root.API.GRPC.Services {
sd := GRPCServices.Get(svc.Name())
if sd == nil {
continue
}
svcName := codegen.SnakeCase(sd.Service.VarName)
specs = append(specs, &codegen.ImportSpec{
Path: path.Join(genpkg, "grpc", svcName, "client"),
Name: sd.Service.PkgName + "c",
})
specs = append(specs, &codegen.ImportSpec{
Path: path.Join(genpkg, "grpc", svcName, pbPkgName),
Name: svcName + pbPkgName,
})
}
sections := []*codegen.SectionTemplate{
codegen.Header(title, "cli", specs),
cli.UsageCommands(data),
cli.UsageExamples(data),
{
Name: "parse-endpoint",
Source: parseEndpointT,
Data: struct {
FlagsCode string
Commands []*cli.CommandData
}{
cli.FlagsCode(data),
data,
},
},
}
for _, cmd := range data {
sections = append(sections, cli.CommandUsage(cmd))
}
return &codegen.File{Path: fpath, SectionTemplates: sections}
}
// payloadBuilders returns the file that contains the payload constructors that
// use flag values as arguments.
func payloadBuilders(genpkg string, svc *expr.GRPCServiceExpr, data *cli.CommandData) *codegen.File {
sd := GRPCServices.Get(svc.Name())
svcName := codegen.SnakeCase(sd.Service.VarName)
fpath := filepath.Join(codegen.Gendir, "grpc", svcName, "client", "cli.go")
title := svc.Name() + " gRPC client CLI support package"
specs := []*codegen.ImportSpec{
{Path: "encoding/json"},
{Path: "fmt"},
{Path: "strconv"},
{Path: "goa.design/goa", Name: "goa"},
{Path: path.Join(genpkg, svcName), Name: sd.Service.PkgName},
{Path: path.Join(genpkg, "grpc", svcName, pbPkgName), Name: sd.PkgName},
}
sections := []*codegen.SectionTemplate{
codegen.Header(title, "client", specs),
}
for _, sub := range data.Subcommands {
if sub.BuildFunction != nil {
sections = append(sections, cli.PayloadBuilderSection(sub.BuildFunction))
}
}
return &codegen.File{Path: fpath, SectionTemplates: sections}
}
func buildFlags(svc *ServiceData, e *EndpointData) ([]*cli.FlagData, *cli.BuildFunctionData) {
var (
flags []*cli.FlagData
buildFunction *cli.BuildFunctionData
)
{
if e.Request != nil {
flags, buildFunction = makeFlags(e, e.Request.CLIArgs)
}
}
return flags, buildFunction
}
func makeFlags(e *EndpointData, args []*InitArgData) ([]*cli.FlagData, *cli.BuildFunctionData) {
var (
fdata []*cli.FieldData
flags = make([]*cli.FlagData, len(args))
params = make([]string, len(args))
pInitArgs = make([]*codegen.InitArgData, len(args))
check bool
pinit *cli.PayloadInitData
)
for i, arg := range args {
pInitArgs[i] = &codegen.InitArgData{
Name: arg.Name,
FieldName: arg.FieldName,
FieldType: arg.FieldType,
Type: arg.Type,
}
f := cli.NewFlagData(e.ServiceName, e.Method.Name, arg.Name, arg.TypeName, arg.Description, arg.Required, arg.Example, arg.DefaultValue)
flags[i] = f
params[i] = f.FullName
code, chek := cli.FieldLoadCode(f, arg.Name, arg.TypeName, arg.Validate, arg.DefaultValue, e.PayloadType)
check = check || chek
tn := arg.TypeRef
if f.Type == "JSON" {
// We need to declare the variable without
// a pointer to be able to unmarshal the JSON
// using its address.
tn = arg.TypeName
}
fdata = append(fdata, &cli.FieldData{
Name: arg.Name,
VarName: arg.Name,
TypeRef: tn,
Init: code,
})
}
if e.Method.PayloadRef == "" {
return flags, nil
}
if e.Request.ServerConvert != nil {
pinit = &cli.PayloadInitData{
Code: e.Request.ServerConvert.Init.Code,
ReturnIsStruct: e.Request.ServerConvert.Init.ReturnIsStruct,
ReturnTypePkg: e.Request.ServerConvert.Init.ReturnTypePkg,
Args: pInitArgs,
}
}
return flags, &cli.BuildFunctionData{
Name: "Build" + e.Method.VarName + "Payload",
ActualParams: params,
FormalParams: params,
ServiceName: e.ServiceName,
MethodName: e.Method.Name,
ResultType: e.PayloadRef,
Fields: fdata,
PayloadInit: pinit,
CheckErr: check,
}
}
const parseEndpointT = `// ParseEndpoint returns the endpoint and payload as specified on the command
// line.
func ParseEndpoint(cc *grpc.ClientConn, opts ...grpc.CallOption) (goa.Endpoint, interface{}, error) {
{{ .FlagsCode }}
var (
data interface{}
endpoint goa.Endpoint
err error
)
{
switch svcn {
{{- range .Commands }}
case "{{ .Name }}":
c := {{ .PkgName }}.NewClient(cc, opts...)
switch epn {
{{- $pkgName := .PkgName }}{{ range .Subcommands }}
case "{{ .Name }}":
endpoint = c.{{ .MethodVarName }}()
{{- if .BuildFunction }}
data, err = {{ $pkgName}}.{{ .BuildFunction.Name }}({{ range .BuildFunction.ActualParams }}*{{ . }}Flag, {{ end }})
{{- else if .Conversion }}
{{ .Conversion }}
{{- else }}
data = nil
{{- end }}
{{- end }}
}
{{- end }}
}
}
if err != nil {
return nil, nil, err
}
return endpoint, data, nil
}
`