Add instance command registry and composition

This commit is contained in:
2026-10-07 13:16:52 +00:00
parent f61d1ae7dd
commit f99be3257c
6 changed files with 564 additions and 5 deletions

27
registry.go Normal file
View 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
}