Capture immutable command declarations
This commit is contained in:
25
execute.go
25
execute.go
@@ -7,7 +7,6 @@ import (
|
||||
"io"
|
||||
|
||||
internalcobra "gitea.maximumdirect.net/eric/CommandKit/internal/cobra"
|
||||
configkit "gitea.maximumdirect.net/eric/ConfigKit"
|
||||
)
|
||||
|
||||
// Execute parses and dispatches one invocation. args excludes the executable
|
||||
@@ -25,21 +24,21 @@ func (a *App) Execute(ctx context.Context, args []string, streams Streams) Outco
|
||||
return current.present(Usage(errors.New(parsed.Failure.Message)), parsed.Path)
|
||||
}
|
||||
command, ok := a.commandAt(parsed.Path)
|
||||
if !ok || command.payload == nil {
|
||||
if !ok || command.record == nil || command.record.payload == nil {
|
||||
return current.present(current.renderHelp(parsed.Path), parsed.Path)
|
||||
}
|
||||
if parsed.HelpRequested || !command.payload.hasRun() {
|
||||
if _, err := command.payload.prepare(parsed.FlagValues, parsed.Operands, false); err != nil {
|
||||
if parsed.HelpRequested || !command.record.payload.hasRun() {
|
||||
if _, err := command.record.payload.prepare(parsed.FlagValues, parsed.Operands, false); err != nil {
|
||||
return current.present(Usage(err), parsed.Path)
|
||||
}
|
||||
return current.present(current.renderHelp(parsed.Path), parsed.Path)
|
||||
}
|
||||
buildInput, err := command.payload.prepare(parsed.FlagValues, parsed.Operands, true)
|
||||
buildInput, err := command.record.payload.prepare(parsed.FlagValues, parsed.Operands, true)
|
||||
if err != nil {
|
||||
return current.present(Usage(err), parsed.Path)
|
||||
}
|
||||
input := buildInput()
|
||||
if err := command.payload.checkInput(input); err != nil {
|
||||
if err := command.record.payload.checkInput(input); err != nil {
|
||||
return current.present(Usage(err), parsed.Path)
|
||||
}
|
||||
if len(parsed.Path) == 1 && parsed.Path[0] == "help" {
|
||||
@@ -49,7 +48,7 @@ func (a *App) Execute(ctx context.Context, args []string, streams Streams) Outco
|
||||
}
|
||||
return current.present(current.renderHelpTargets(help.Targets.Value), parsed.Path)
|
||||
}
|
||||
outcome := command.payload.run(Invocation{Context: ctx, Streams: streams}, input)
|
||||
outcome := command.record.payload.run(Invocation{Context: ctx, Streams: streams}, input)
|
||||
return current.present(outcome, parsed.Path)
|
||||
}
|
||||
|
||||
@@ -144,14 +143,14 @@ func (a *App) commandAt(path []string) (Command, bool) {
|
||||
return Command{}, true
|
||||
}
|
||||
command, ok := a.commands[path[0]]
|
||||
if !ok || (command.reserved && command.command.payload == nil) {
|
||||
if !ok || (command.reserved && command.command.record.payload == nil) {
|
||||
return Command{}, false
|
||||
}
|
||||
current := command.command
|
||||
for _, name := range path[1:] {
|
||||
found := false
|
||||
for _, child := range current.children {
|
||||
if child.name == name {
|
||||
for _, child := range current.record.children {
|
||||
if child.record != nil && child.record.name == name {
|
||||
current = child
|
||||
found = true
|
||||
break
|
||||
@@ -183,10 +182,10 @@ func (a *App) presentWithUsageLine(outcome Outcome, path []string, streams Strea
|
||||
}
|
||||
diagnostics := append([]Diagnostic(nil), outcome.Diagnostics...)
|
||||
if outcome.Cause != nil && !hasErrorDiagnostic(diagnostics) {
|
||||
diagnostics = append(diagnostics, MessageDiagnostic(configkit.SeverityError, outcome.Cause.Error()))
|
||||
diagnostics = append(diagnostics, MessageDiagnostic(SeverityError, outcome.Cause.Error()))
|
||||
}
|
||||
if outcome.Kind == OutcomeUsage {
|
||||
diagnostics = append(diagnostics, MessageDiagnostic(configkit.SeverityError, usageLine(path)))
|
||||
diagnostics = append(diagnostics, MessageDiagnostic(SeverityError, usageLine(path)))
|
||||
}
|
||||
outcome.Diagnostics = diagnostics
|
||||
if len(diagnostics) == 0 {
|
||||
@@ -236,7 +235,7 @@ func (a *App) usageFallback() string {
|
||||
|
||||
func hasErrorDiagnostic(diagnostics []Diagnostic) bool {
|
||||
for _, diagnostic := range diagnostics {
|
||||
if diagnostic.Write != nil && diagnostic.Severity == configkit.SeverityError {
|
||||
if diagnostic.Write != nil && diagnostic.Severity == SeverityError {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user