Files
commandkit/app.go

50 lines
1.5 KiB
Go
Raw Normal View History

package commandkit
import (
"fmt"
2026-10-19 17:27:17 +00:00
"gitea.maximumdirect.net/eric/CommandKit/internal/alias"
commandmeta "gitea.maximumdirect.net/eric/CommandKit/internal/commandmeta"
)
// App owns one command registry and its application identity. Each App has an
// independent registry; command declarations may be reused across Apps.
type App struct {
2026-10-19 17:23:32 +00:00
identity Identity
commands map[string]*commandNode
routingRules []RoutingRule
2026-10-19 17:27:17 +00:00
aliases *alias.Resolver
2026-10-19 17:23:32 +00:00
snapshot *routingSnapshot
}
type routingSnapshot struct {
metadata *commandmeta.Command
runners map[commandmeta.Key]commandPayload
}
type commandNode struct {
command Command
}
2026-10-19 17:23:32 +00:00
// New creates an application with the supplied identity and initial
// declarations.
// The identity name must not be empty. New installs its generated help and
// version commands and caller declarations as one batch; an invalid declaration
// returns an error without exposing an App. The root help and version names are
// reserved for generated commands.
2026-10-19 17:23:32 +00:00
func New(identity Identity, declarations ...Declaration) (*App, error) {
if identity.Name == "" {
return nil, fmt.Errorf("application identity name must not be empty")
}
app := &App{identity: identity, commands: make(map[string]*commandNode)}
2026-10-19 17:23:32 +00:00
builtins := app.builtinCommands()
all := make([]Declaration, 0, len(builtins)+len(declarations))
all = append(all, builtins...)
all = append(all, declarations...)
if err := app.installBatch(all...); err != nil {
return nil, err
}
return app, nil
}