Route executions through neutral snapshots
This commit is contained in:
34
parser.go
34
parser.go
@@ -4,46 +4,52 @@ import (
|
||||
"sort"
|
||||
|
||||
internalcobra "gitea.maximumdirect.net/eric/CommandKit/internal/cobra"
|
||||
commandmeta "gitea.maximumdirect.net/eric/CommandKit/internal/commandmeta"
|
||||
)
|
||||
|
||||
func (a *App) parse(args []string, options internalcobra.ParseOptions) internalcobra.Result {
|
||||
func (a *App) parse(args []string, options commandmeta.ParseOptions) commandmeta.Result {
|
||||
if a == nil {
|
||||
return internalcobra.Result{Failure: &internalcobra.Failure{Message: "cannot parse commands on a nil app"}}
|
||||
return commandmeta.Result{Failure: &commandmeta.Failure{Message: "cannot parse commands on a nil app"}}
|
||||
}
|
||||
return internalcobra.Parse(a.bridgeTree(), args, options)
|
||||
return internalcobra.Parse(a.routeSnapshot().metadata, args, options)
|
||||
}
|
||||
|
||||
func (a *App) bridgeTree() *internalcobra.Command {
|
||||
func (a *App) routeSnapshot() *routingSnapshot {
|
||||
if a == nil {
|
||||
return nil
|
||||
}
|
||||
if a.commandTree == nil {
|
||||
a.commandTree = a.buildBridgeTree()
|
||||
if a.snapshot == nil {
|
||||
a.snapshot = a.buildRoutingSnapshot()
|
||||
}
|
||||
return a.commandTree
|
||||
return a.snapshot
|
||||
}
|
||||
|
||||
func (a *App) buildBridgeTree() *internalcobra.Command {
|
||||
root := &internalcobra.Command{Name: a.identity.Name}
|
||||
func (a *App) buildRoutingSnapshot() *routingSnapshot {
|
||||
root := &commandmeta.Command{Name: a.identity.Name}
|
||||
snapshot := &routingSnapshot{metadata: root, runners: make(map[commandmeta.Key]commandPayload)}
|
||||
names := make([]string, 0, len(a.commands))
|
||||
for name := range a.commands {
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
var key commandmeta.Key
|
||||
for _, name := range names {
|
||||
root.Children = append(root.Children, bridgeCommand(a.commands[name].command))
|
||||
root.Children = append(root.Children, bridgeCommand(a.commands[name].command, snapshot, &key))
|
||||
}
|
||||
return root
|
||||
return snapshot
|
||||
}
|
||||
|
||||
func bridgeCommand(command Command) *internalcobra.Command {
|
||||
func bridgeCommand(command Command, snapshot *routingSnapshot, next *commandmeta.Key) *commandmeta.Command {
|
||||
record := command.record
|
||||
declaration := &internalcobra.Command{
|
||||
*next = *next + 1
|
||||
declaration := &commandmeta.Command{
|
||||
Key: *next,
|
||||
Name: record.name,
|
||||
Summary: record.summary,
|
||||
Description: record.description,
|
||||
}
|
||||
if record.payload != nil {
|
||||
snapshot.runners[declaration.Key] = record.payload
|
||||
declaration.Runnable = record.payload.hasRun()
|
||||
declaration.Flags = record.payload.bridgeFlags()
|
||||
declaration.Arguments = record.payload.bridgeArguments()
|
||||
@@ -54,7 +60,7 @@ func bridgeCommand(command Command) *internalcobra.Command {
|
||||
if child.record == nil || child.record.absent {
|
||||
continue
|
||||
}
|
||||
declaration.Children = append(declaration.Children, bridgeCommand(child))
|
||||
declaration.Children = append(declaration.Children, bridgeCommand(child, snapshot, next))
|
||||
}
|
||||
return declaration
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user