Compile typed command runners

This commit is contained in:
2026-10-11 01:21:52 +00:00
parent 4e0611bc13
commit 74f6bdeade
5 changed files with 69 additions and 42 deletions

View File

@@ -33,23 +33,11 @@ func (a *App) Execute(ctx context.Context, args []string, streams Streams) Outco
}
return current.present(current.renderHelp(parsed.Path), parsed.Path)
}
buildInput, err := command.record.payload.prepare(parsed.FlagValues, parsed.Operands, true)
run, 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.record.payload.checkInput(input); err != nil {
return current.present(classifyInputError(err), parsed.Path)
}
if len(parsed.Path) == 1 && parsed.Path[0] == "help" {
help, ok := input.(*helpInput)
if !ok {
return current.present(Failure(errors.New("built-in help input has an unexpected type")), parsed.Path)
}
return current.present(current.renderHelpTargets(help.Targets.Value), parsed.Path)
}
outcome := command.record.payload.run(Invocation{Context: ctx, Streams: streams}, input)
return current.present(outcome, parsed.Path)
return current.present(run(current), parsed.Path)
}
type execution struct {
@@ -61,6 +49,22 @@ type execution struct {
session *internalcobra.Session
}
type commandExecution struct {
invocation Invocation
renderHelpTargetsFunc func([]string) Outcome
}
func (current *execution) commandContext() commandExecution {
return commandExecution{
invocation: Invocation{Context: current.ctx, Streams: current.streams},
renderHelpTargetsFunc: current.renderHelpTargets,
}
}
func (current commandExecution) renderHelpTargets(targets []string) Outcome {
return current.renderHelpTargetsFunc(targets)
}
func newExecution(app *App, ctx context.Context, streams Streams) *execution {
current := &execution{
app: app,