28 lines
779 B
Go
28 lines
779 B
Go
|
|
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
|
||
|
|
}
|