35 lines
910 B
Go
35 lines
910 B
Go
package commandkit
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
internalcobra "gitea.maximumdirect.net/eric/CommandKit/internal/cobra"
|
|
)
|
|
|
|
// App owns one command registry and its application identity.
|
|
type App struct {
|
|
identity Identity
|
|
commands map[string]*commandNode
|
|
commandTree *internalcobra.Command
|
|
}
|
|
|
|
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
|
|
}
|