MCPcopy Create free account
hub / github.com/anchordotdev/cli / NewCmd

Function NewCmd

cmd.go:253–372  ·  view source on GitHub ↗
(parent *cobra.Command, name string, fn func(*cobra.Command))

Source from the content-addressed store, hash-verified

251}
252
253func NewCmd[T UIer](parent *cobra.Command, name string, fn func(*cobra.Command)) *cobra.Command {
254 var def, parentDef *CmdDef
255 if parent != nil {
256 var ok bool
257 if parentDef, ok = cmdDefByCommands[parent]; !ok {
258 panic("unregistered parent command")
259 }
260 for _, sub := range parentDef.SubDefs {
261 if sub.Name == name {
262 def = &sub
263 break
264 }
265 }
266 if def == nil {
267 panic("missing subcommand definition")
268 }
269 } else {
270 def = &rootDef
271 }
272
273 constructor := func() *cobra.Command {
274 cmd := &cobra.Command{
275 Aliases: def.Aliases,
276 Args: def.Args,
277 Long: def.Long,
278 Short: def.Short,
279 Use: def.Use,
280 SilenceUsage: true,
281 }
282
283 cmd.SetErrPrefix(ui.Danger("Error!"))
284
285 ctx := ContextWithConfig(context.Background(), defaultConfig())
286 cmd.SetContext(ctx)
287
288 fn(cmd)
289
290 cmd.RunE = func(cmd *cobra.Command, args []string) (returnedError error) {
291 ctx := cmd.Context()
292
293 cfg := new(Config)
294 if err := cfg.Load(ctx); err != nil {
295 return err
296 }
297
298 if cfg.Test.SkipRunE {
299 return nil
300 }
301
302 ctx = ContextWithConfig(cmd.Context(), cfg)
303 cmd.SetContext(ctx)
304
305 var t T
306
307 switch any(t).(type) {
308 case ShowHelp:
309 cmd.HelpFunc()(cmd, args)
310 return nil

Callers 3

auth.goFile · 0.92
service.goFile · 0.92
org.goFile · 0.92

Calls 15

NewDriverTUIFunction · 0.92
CapturePanicFunction · 0.92
ContextWithConfigFunction · 0.85
defaultConfigFunction · 0.85
ContextWithCalledAsFunction · 0.85
isReportableFunction · 0.85
LoadMethod · 0.80
ActivateMethod · 0.80
wrapMethod · 0.80
ReportErrorFunction · 0.70
QuitMethod · 0.65
WaitMethod · 0.65

Tested by

no test coverage detected