43 lines
1.2 KiB
Go
43 lines
1.2 KiB
Go
package commandkit
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
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
|
|
snapshot *routingSnapshot
|
|
}
|
|
|
|
type routingSnapshot struct {
|
|
metadata *commandmeta.Command
|
|
runners map[commandmeta.Key]commandPayload
|
|
}
|
|
|
|
type commandNode struct {
|
|
command Command
|
|
}
|
|
|
|
// New creates an application with the supplied identity and initial commands.
|
|
// 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.
|
|
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: make(map[string]*commandNode)}
|
|
all := append(app.builtinCommands(), commands...)
|
|
if err := app.installBatch(all...); err != nil {
|
|
return nil, err
|
|
}
|
|
return app, nil
|
|
}
|