runner

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2025 License: Apache-2.0 Imports: 11 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnsureLineTermination added in v0.2.0

func EnsureLineTermination(cmd string) string

Types

type Command

type Command struct {
	Stdin  string
	Index  int
	Stdout []string
	Error  []string
}

Command represents a command

func NewCommand

func NewCommand(command string, output string, err error) *Command

NewCommand creates a new command

func (*Command) Err

func (c *Command) Err() error

Err returns command error

func (*Command) Output

func (c *Command) Output() string

Output returns command output

type History

type History struct {
	Commands []*Command
}

History represents command history

func NewHistory

func NewHistory() *History

NewHistory creates a command history

type Listener

type Listener func(stdout string, hasMore bool)

Listener represent command listener (it will send stdout fragments as thier being available on stdout)

type Option

type Option func(*Options)

Option represents runner option

func AsPipeline added in v0.2.0

func AsPipeline() Option

func WithEnvironment

func WithEnvironment(env map[string]string) Option

WithEnvironment creates with environment option

func WithFlashIntervalMs

func WithFlashIntervalMs(ts int) Option

WithFlashIntervalMs creates with flash time option

func WithHistory

func WithHistory(history *History) Option

WithHistory creates with history option

func WithListener

func WithListener(listener Listener) Option

WithListener creates with listener option

func WithPath

func WithPath(aPath string) Option

WithPath creates with path option

func WithRunner

func WithRunner(runner Runner) Option

WithRunner creates with runner option

func WithShell

func WithShell(shell string) Option

WithShell creates with shell option

func WithShellPrompt

func WithShellPrompt(shellPrompt string) Option

WithShellPrompt creates with shell prompt option

func WithSystemPaths

func WithSystemPaths(paths []string) Option

WithSystemPaths creates with listener option

func WithTerminators

func WithTerminators(terminators []string) Option

WithTerminators creates with terminators option

func WithTimeout

func WithTimeout(timeoutMs int) Option

WithTimeout creates with timeout option

type Options

type Options struct {
	Runner      Runner
	Shell       string
	Term        string
	Cols        int
	Rows        int
	Path        string
	Env         map[string]string
	SystemPaths []string

	History *History
	// contains filtered or unexported fields
}

Options represents runner options

func NewOptions

func NewOptions(opts []Option) *Options

NewOptions creates a new options

func (*Options) Apply

func (o *Options) Apply(options []Option) *Options

Apply applies options

func (*Options) AsPipeline added in v0.2.0

func (o *Options) AsPipeline() bool

func (*Options) Environ

func (o *Options) Environ() []string

Environ returns environment variables

type Pipeline

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

Pipeline represents a command pipeline

func NewPipeline

func NewPipeline(ctx context.Context, in io.WriteCloser, stdout io.Reader, stderr io.Reader, options *Options) (*Pipeline, error)

NewPipeline creates a new pipeline

func (*Pipeline) Close

func (p *Pipeline) Close() (err error)

Close closes pipeline

func (*Pipeline) Drain

func (p *Pipeline) Drain(ctx context.Context, opts ...Option)

Drain reads any outstanding output

func (*Pipeline) Err

func (p *Pipeline) Err() error

Err returns error

func (*Pipeline) FormatCmd

func (p *Pipeline) FormatCmd(cmd string) string

FormatCmd formats a command that is sent to an interactive shell via stdin.

Key guarantees:

  • Always append a "status:" marker so the runner can detect completion and capture the exit code.
  • Shield the shell's stdin from the user command by grouping and redirecting that group's stdin to /dev/null. This prevents commands that read from stdin from consuming the subsequently appended status marker. Explicit stdin redirections inside the user command still take precedence.
  • Attempt to enable 'pipefail' (if supported) inside the group so pipelines report a non-zero status when any segment fails. On shells without pipefail, this attempt is silenced and the status falls back to that of the last command in the pipeline (standard POSIX behavior).

Final layout:

{ set -o pipefail 2>/dev/null; <user_command>; } </dev/null
status=$?; echo 'status:'$status

Using a group redirection avoids brittle parsing (quotes, pipes, heredocs) and reliably shields the shell stdin across a wide range of inputs.

func (*Pipeline) Listen added in v0.2.0

func (p *Pipeline) Listen(ctx context.Context, opts ...Option) error

Listen listens for output

func (*Pipeline) Read

func (p *Pipeline) Read(ctx context.Context, opts ...Option) (output string, has bool, code int, err error)

Read reads output

func (*Pipeline) Running

func (p *Pipeline) Running() bool

Running returns true if pipeline is running

type Runner

type Runner interface {
	//Run runs supplied command
	Run(ctx context.Context, command string, options ...Option) (string, int, error)
	//Sends data to stdin
	Send(ctx context.Context, data []byte) (int, error)
	//PID returns process id
	PID() int
	//Close closes runner
	Close() error
}

Runner represents a command runner

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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