AddModuleFunc add the module after Invoking its constructor. Clean up functions and errors are handled automatically.
(constructor interface{})
| 392 | // AddModuleFunc add the module after Invoking its constructor. Clean up |
| 393 | // functions and errors are handled automatically. |
| 394 | func (c *C) AddModuleFunc(constructor interface{}) { |
| 395 | c.provide(constructor) |
| 396 | ftype := reflect.TypeOf(constructor) |
| 397 | targetTypes := make([]reflect.Type, 0) |
| 398 | for i := 0; i < ftype.NumOut(); i++ { |
| 399 | if isErr(ftype.Out(i)) { |
| 400 | continue |
| 401 | } |
| 402 | if isCleanup(ftype.Out(i)) { |
| 403 | continue |
| 404 | } |
| 405 | outT := ftype.Out(i) |
| 406 | targetTypes = append(targetTypes, outT) |
| 407 | } |
| 408 | |
| 409 | fnType := reflect.FuncOf(targetTypes, nil, false /* variadic */) |
| 410 | fn := reflect.MakeFunc(fnType, func(args []reflect.Value) []reflect.Value { |
| 411 | for _, arg := range args { |
| 412 | c.AddModule(arg.Interface()) |
| 413 | } |
| 414 | return nil |
| 415 | }) |
| 416 | |
| 417 | err := c.di.Invoke(fn.Interface()) |
| 418 | if err != nil { |
| 419 | panic(err) |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | // ApplyRootCommand iterates through every CommandProvider registered in the container, |
| 424 | // and introduce the root *cobra.Command to everyone. |