Files
commandkit/app.go

51 lines
1.2 KiB
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
configRegistered bool
}
type commandNode struct {
command Command
reserved bool
}
// 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: map[string]*commandNode{
"help": {
command: Command{record: &commandRecord{name: "help", summary: "Show help for commands."}},
reserved: true,
},
"version": {
command: Command{record: &commandRecord{name: "version", summary: "Show application version."}},
reserved: true,
},
},
}
if err := app.installBuiltins(); err != nil {
return nil, err
}
if err := app.Register(commands...); err != nil {
return nil, err
}
return app, nil
}