MCPcopy Create free account
hub / github.com/goadesign/goa / BuildSubcommandData

Function BuildSubcommandData

codegen/cli/cli.go:186–246  ·  view source on GitHub ↗

BuildSubcommandData builds the data needed by CLI code generators to render the CLI parsing of the service sub-command.

(data *service.Data, m *service.MethodData, buildFunction *BuildFunctionData, flags []*FlagData)

Source from the content-addressed store, hash-verified

184// BuildSubcommandData builds the data needed by CLI code generators to render
185// the CLI parsing of the service sub-command.
186func BuildSubcommandData(data *service.Data, m *service.MethodData, buildFunction *BuildFunctionData, flags []*FlagData) *SubcommandData {
187 en := m.Name
188 name := codegen.KebabCase(en)
189 fullName := goifyTerms(data.Name, en)
190 description := m.Description
191 if description == "" {
192 description = fmt.Sprintf("Make request to the %q endpoint", m.Name)
193 }
194
195 var conversion string
196 if m.Payload != "" && buildFunction == nil && len(flags) > 0 {
197 // No build function, just convert the arg to the body type
198 var convPre, convSuff string
199 target := "data"
200 if flagType(m.Payload) == "JSON" {
201 target = "val"
202 convPre = fmt.Sprintf("var val %s\n", m.Payload)
203 convSuff = "\ndata = val"
204 }
205 conv, _, check := conversionCode(
206 "*"+flags[0].FullName+"Flag",
207 target,
208 m.Payload,
209 false,
210 )
211 conversion = convPre + conv + convSuff
212 if check {
213 conversion = "var err error\n" + conversion
214 conversion += "\nif err != nil {\n"
215 if flagType(m.Payload) == "JSON" {
216 conversion += fmt.Sprintf(`return nil, nil, fmt.Errorf("invalid JSON for %s, \nerror: %%s, \nexample of valid JSON:\n%%s", err, %q)`,
217 flags[0].FullName+"Flag", flags[0].Example)
218 } else {
219 conversion += fmt.Sprintf(`return nil, nil, fmt.Errorf("invalid value for %s, must be %s")`,
220 flags[0].FullName+"Flag", flags[0].Type)
221 }
222 conversion += "\n}"
223 }
224 }
225
226 var interceptors *InterceptorData
227 if len(m.ClientInterceptors) > 0 {
228 interceptors = &InterceptorData{
229 VarName: codegen.Goify(data.Name, false) + "Inter",
230 PkgName: data.PkgName,
231 }
232 }
233 sub := &SubcommandData{
234 Name: name,
235 FullName: fullName,
236 Description: description,
237 Flags: flags,
238 MethodVarName: m.VarName,
239 BuildFunction: buildFunction,
240 Conversion: conversion,
241 Interceptors: interceptors,
242 }
243 generateExample(sub, data.Name)

Callers 2

buildSubcommandDataFunction · 0.92
ClientCLIFilesFunction · 0.92

Calls 6

KebabCaseFunction · 0.92
GoifyFunction · 0.92
goifyTermsFunction · 0.85
flagTypeFunction · 0.85
conversionCodeFunction · 0.85
generateExampleFunction · 0.85

Tested by

no test coverage detected