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

41
app.go Normal file
View File

@@ -0,0 +1,41 @@
package commandkit
import "fmt"
// App owns one command registry and its application identity.
type App struct {
identity Identity
commands map[string]*commandNode
}
type commandNode struct {
command Command
reserved bool
}
// New creates an application with the supplied identity and initial commands.
// The identity name must not be empty. The root help and version names are
// reserved for generated commands.
func New(identity Identity, commands ...Command) (*App, error) {
if identity.Name == "" {
return nil, fmt.Errorf("application identity name must not be empty")
}
app := &App{
identity: identity,
commands: map[string]*commandNode{
"help": {
command: Command{name: "help", summary: "Show help for commands."},
reserved: true,
},
"version": {
command: Command{name: "version", summary: "Show application version."},
reserved: true,
},
},
}
if err := app.Register(commands...); err != nil {
return nil, err
}
return app, nil
}