runner

package
v5.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 29, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package runner provides helpers for executing Cobra commands while capturing output.

This package implements a command runner that executes Cobra commands with real-time console output while simultaneously capturing stdout and stderr for programmatic inspection. This is useful for:

  • Testing commands while preserving normal console behavior
  • Providing detailed error messages that include command output
  • Building command orchestration tools that need output visibility

The primary type is CobraCommandRunner, which implements the CommandRunner interface and can be used with any Cobra command. Output is displayed to the console exactly as it would be when running the command directly, while also being captured in a CommandResult for later use.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CobraCommandRunner

type CobraCommandRunner struct {
	// contains filtered or unexported fields
}

CobraCommandRunner executes any Cobra command with console output. This runner displays command output to stdout/stderr in real-time while also capturing it for the result.

func NewCobraCommandRunner

func NewCobraCommandRunner(stdout, stderr io.Writer) *CobraCommandRunner

NewCobraCommandRunner creates a command runner that works with any Cobra command. It displays output to stdout/stderr in real-time (like running the binary directly) while also capturing output for programmatic use in the CommandResult.

If stdout or stderr are nil, they default to os.Stdout and os.Stderr respectively.

func (*CobraCommandRunner) Run

func (r *CobraCommandRunner) Run(
	ctx context.Context,
	cmd *cobra.Command,
	args []string,
) (CommandResult, error)

Run executes a Cobra command and displays output in real-time to the console. The command's output streams are configured to write to both capture buffers and the configured stdout/stderr writers, providing the same behavior as running the binary directly while also making the output available programmatically.

The command is executed with the provided context and arguments. Usage and error messages are silenced since this runner handles error reporting.

Returns the captured output and any error from command execution.

type CommandResult

type CommandResult struct {
	Stdout string
	Stderr string
}

CommandResult captures the stdout and stderr collected during a Cobra command execution. Both fields contain the complete output from the command, including any output produced before an error occurred.

type CommandRunner

type CommandRunner interface {
	Run(ctx context.Context, cmd *cobra.Command, args []string) (CommandResult, error)
}

CommandRunner executes Cobra commands while capturing their output. Implementations should display output to stdout/stderr in real-time while also capturing it for programmatic access via CommandResult.

type MockCommandRunner

type MockCommandRunner struct {
	mock.Mock
}

MockCommandRunner is an autogenerated mock type for the CommandRunner type

func NewMockCommandRunner

func NewMockCommandRunner(t interface {
	mock.TestingT
	Cleanup(func())
}) *MockCommandRunner

NewMockCommandRunner creates a new instance of MockCommandRunner. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. The first argument is typically a *testing.T value.

func (*MockCommandRunner) EXPECT

func (*MockCommandRunner) Run

func (_mock *MockCommandRunner) Run(ctx context.Context, cmd *cobra.Command, args []string) (CommandResult, error)

Run provides a mock function for the type MockCommandRunner

type MockCommandRunner_Expecter

type MockCommandRunner_Expecter struct {
	// contains filtered or unexported fields
}

func (*MockCommandRunner_Expecter) Run

func (_e *MockCommandRunner_Expecter) Run(ctx interface{}, cmd interface{}, args interface{}) *MockCommandRunner_Run_Call

Run is a helper method to define mock.On call

  • ctx context.Context
  • cmd *cobra.Command
  • args []string

type MockCommandRunner_Run_Call

type MockCommandRunner_Run_Call struct {
	*mock.Call
}

MockCommandRunner_Run_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Run'

func (*MockCommandRunner_Run_Call) Return

func (*MockCommandRunner_Run_Call) Run

func (*MockCommandRunner_Run_Call) RunAndReturn

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL