Add instance command registry and composition
This commit is contained in:
27
registry.go
Normal file
27
registry.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package commandkit
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Register adds a batch of root commands to the application. The batch is
|
||||
// validated before the registry is changed.
|
||||
func (a *App) Register(commands ...Command) error {
|
||||
if a == nil {
|
||||
return fmt.Errorf("cannot register commands on a nil app")
|
||||
}
|
||||
|
||||
updated := make(map[string]*commandNode, len(a.commands)+len(commands))
|
||||
for name, node := range a.commands {
|
||||
updated[name] = node
|
||||
}
|
||||
for _, command := range commands {
|
||||
if err := validateCommandDeclaration(command); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, exists := updated[command.name]; exists {
|
||||
return fmt.Errorf("command path %q is already registered", command.name)
|
||||
}
|
||||
updated[command.name] = &commandNode{command: cloneCommand(command)}
|
||||
}
|
||||
a.commands = updated
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user