Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ func (m *Menu) RunCommandLine(ctx context.Context, line string) (err error) {
return m.RunCommandArgs(ctx, args)
}

// Execute runs a processed argument vector against a menu command tree.
//
// Most callers should prefer Menu.RunCommandArgs, which resets the active menu
// before execution. Execute is useful for integrations that already prepared a
// menu and need direct access to the lower-level execution path.
func (c *Console) Execute(ctx context.Context, menu *Menu, args []string, async bool) error {
return c.execute(ctx, menu, args, async)
}

// execute - The user has entered a command input line, the arguments have been processed:
// we synchronize a few elements of the console, then pass these arguments to the command
// parser for execution and error handling.
Expand Down
33 changes: 33 additions & 0 deletions run_execute_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package console

import (
"context"
"testing"

"github.com/spf13/cobra"
)

func TestConsoleExecuteRunsPreparedMenu(t *testing.T) {
c := New("test")
menu := c.ActiveMenu()

var ran bool
root := &cobra.Command{Use: "root"}
root.AddCommand(&cobra.Command{
Use: "run",
Run: func(*cobra.Command, []string) {
ran = true
},
})
menu.Command = root

if err := c.Execute(context.Background(), menu, []string{"run"}, false); err != nil {
t.Fatal(err)
}
if !ran {
t.Fatal("Execute did not run the target command")
}
if c.isExecuting.Load() {
t.Fatal("Execute left the console marked as executing")
}
}
Loading