Files
commandkit/app.go

57 lines
1.8 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 {
identity Identity
commands map[string]*commandNode
routingRules []RoutingRule
aliases *alias.Resolver
flagAliases map[string]resolvedFlagAliases
discoveryFlags map[string]resolvedFlagAliases
snapshot *routingSnapshot
}
type routingSnapshot struct {
metadata *commandmeta.Command
runners map[commandmeta.Key]commandPayload
aliases *alias.Resolver
canonical map[string]*canonicalNode
2026-10-19 19:40:55 +00:00
commandAliases []commandmeta.CommandAlias
flagAliases map[string]resolvedFlagAliases
discoveryFlags map[string]resolvedFlagAliases
}
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
}