Documentation
¶
Index ¶
- func EnsureLineTermination(cmd string) string
- type Command
- type History
- type Listener
- type Option
- func AsPipeline() Option
- func WithEnvironment(env map[string]string) Option
- func WithFlashIntervalMs(ts int) Option
- func WithHistory(history *History) Option
- func WithListener(listener Listener) Option
- func WithPath(aPath string) Option
- func WithRunner(runner Runner) Option
- func WithShell(shell string) Option
- func WithShellPrompt(shellPrompt string) Option
- func WithSystemPaths(paths []string) Option
- func WithTerminators(terminators []string) Option
- func WithTimeout(timeoutMs int) Option
- type Options
- type Pipeline
- func (p *Pipeline) Close() (err error)
- func (p *Pipeline) Drain(ctx context.Context, opts ...Option)
- func (p *Pipeline) Err() error
- func (p *Pipeline) FormatCmd(cmd string) string
- func (p *Pipeline) Listen(ctx context.Context, opts ...Option) error
- func (p *Pipeline) Read(ctx context.Context, opts ...Option) (output string, has bool, code int, err error)
- func (p *Pipeline) Running() bool
- type Runner
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EnsureLineTermination ¶ added in v0.2.0
Types ¶
type Listener ¶
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 ¶
WithEnvironment creates with environment option
func WithFlashIntervalMs ¶
WithFlashIntervalMs creates with flash time option
func WithHistory ¶
WithHistory creates with history option
func WithListener ¶
WithListener creates with listener option
func WithShellPrompt ¶
WithShellPrompt creates with shell prompt option
func WithSystemPaths ¶
WithSystemPaths creates with listener option
func WithTerminators ¶
WithTerminators creates with terminators 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 (*Options) AsPipeline ¶ added in v0.2.0
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) FormatCmd ¶
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.
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