MCPcopy
hub / github.com/larksuite/cli / buildMethodCommand

Function buildMethodCommand

cmd/service/service.go:251–363  ·  view source on GitHub ↗

buildMethodCommand assembles the cobra command for a service method from its static spec: the standard flags, the conditional --data/--file/--yes flags, the generated typed param flags (via paramFlagBinder), and the risk/identity policy annotations.

(ctx context.Context, f *cmdutil.Factory, spec methodCommandSpec, runF func(*ServiceMethodOptions) error, reserved *pflag.FlagSet)

Source from the content-addressed store, hash-verified

249// the generated typed param flags (via paramFlagBinder), and the risk/identity
250// policy annotations.
251func buildMethodCommand(ctx context.Context, f *cmdutil.Factory, spec methodCommandSpec, runF func(*ServiceMethodOptions) error, reserved *pflag.FlagSet) *cobra.Command {
252 m := spec.method
253 opts := &ServiceMethodOptions{
254 Factory: f,
255 ServicePath: spec.servicePath,
256 Method: m,
257 SchemaPath: spec.schemaPath,
258 FileFields: spec.fileFields,
259 }
260 var asStr string
261
262 cmd := &cobra.Command{
263 Use: m.Name,
264 Short: m.Description,
265 // Long is assembled below, once the binder knows which params got no
266 // typed flag.
267 RunE: func(cmd *cobra.Command, args []string) error {
268 opts.Cmd = cmd
269 opts.Ctx = cmd.Context()
270 opts.As = core.Identity(asStr)
271 if runF != nil {
272 return runF(opts)
273 }
274 return serviceMethodRun(opts)
275 },
276 }
277 cmdmeta.SetSource(cmd, cmdmeta.SourceService, true)
278
279 cmd.Flags().StringVar(&opts.Params, "params", "", "Raw URL/query params JSON. Supports - and @file.")
280 if spec.acceptsBody {
281 dataUsage := "JSON request body. Supports - and @file."
282 if !spec.declaresBody {
283 // POST/etc. with no documented body fields: --data is a raw escape
284 // hatch, not a declared body — say so rather than imply structure.
285 dataUsage = "Raw JSON request body (no documented fields; see schema). Supports - and @file."
286 }
287 cmd.Flags().StringVar(&opts.Data, "data", "", dataUsage)
288 }
289 cmdutil.AddAPIIdentityFlag(ctx, cmd, f, &asStr)
290 cmd.Flags().StringVarP(&opts.Output, "output", "o", "", "output file path for binary responses")
291 cmd.Flags().BoolVar(&opts.PageAll, "page-all", false, "automatically paginate through all pages")
292 cmd.Flags().IntVar(&opts.PageLimit, "page-limit", 10, "max pages to fetch with --page-all (0 = unlimited)")
293 cmd.Flags().IntVar(&opts.PageDelay, "page-delay", 200, "delay in ms between pages")
294 // Keep the pagination flags registered (a harmless no-op if passed) but hide
295 // them from help on non-paginating commands, so help doesn't imply a
296 // get/write can paginate.
297 if !spec.paginates {
298 for _, name := range []string{"page-all", "page-limit", "page-delay"} {
299 _ = cmd.Flags().MarkHidden(name)
300 }
301 }
302 cmd.Flags().StringVar(&opts.Format, "format", "json", "output format: json|ndjson|table|csv")
303 cmd.Flags().Bool("json", false, "shorthand for --format json")
304 cmd.Flags().StringVarP(&opts.JqExpr, "jq", "q", "", "jq expression to filter JSON output")
305 cmd.Flags().BoolVar(&opts.DryRun, "dry-run", false, "print request without executing")
306 if spec.risk == cmdutil.RiskHighRiskWrite {
307 cmd.Flags().Bool("yes", false, "confirm high-risk operation")
308 }

Callers 2

Calls 15

SetSourceFunction · 0.92
AddAPIIdentityFlagFunction · 0.92
RegisterFlagCompletionFunction · 0.92
SetTipsFunction · 0.92
SetRiskFunction · 0.92
SetSupportedIdentitiesFunction · 0.92
serviceMethodRunFunction · 0.85
newParamFlagBinderFunction · 0.85
methodLongFunction · 0.85
setMethodHelpDataFunction · 0.85
annotateFunction · 0.85
tagFlagGroupFunction · 0.85

Tested by

no test coverage detected