Files
commandkit/app.go

40 lines
1008 B
Go

package commandkit
import (
"fmt"
commandmeta "gitea.maximumdirect.net/eric/CommandKit/internal/commandmeta"
)
// App owns one command registry and its application identity.
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. 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
}