runner

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2025 License: Apache-2.0 Imports: 10 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 command FormatCmd formats a command that is sent to an interactive shell via stdin.

The command has a "status:" marker appended so that the runner can detect when the command has finished and capture its exit code.

A tricky corner-case arises when the executed command attempts to read from STDIN (for example, utilities such as `rg`, `grep`, `cat` when no file arguments are supplied). Since the same STDIN is also used to feed further commands to the shell, such programs can accidentally consume the subsequently appended marker making the runner believe the command never finished. To prevent the command from draining the shell’s STDIN we redirect its standard input to `/dev/null`. In practice this is equivalent to what most shells do for non-interactive execution and is safe for the majority of use-cases where gosh is employed. If a caller really needs to feed data to the command via STDIN they should use pipeline mode (Options.AsPipeline) or the Runner.Send API which bypasses this redirection.

The final layout therefore looks like:

<user_command> < /dev/null
echo 'status:'$?

Using a newline before the echo avoids any dependency on command chaining operators while keeping the construction simple.

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