Documentation
¶
Overview ¶
Package env runs code in long-lived shell processes and stacks them into named environments.
Index ¶
- Constants
- Variables
- func Environ(vars map[string]string) []string
- func Filter(vars map[string]string, share []string) map[string]string
- func Vars(environ []string) map[string]string
- type Config
- type Env
- type Shell
- func (s *Shell) Close() error
- func (s *Shell) Exported(ctx context.Context) (map[string]string, error)
- func (s *Shell) Run(ctx context.Context, script string, stdout, stderr io.Writer) (status int, exited bool, err error)
- func (s *Shell) RunScripted(ctx context.Context, script string, cols, rows int, out io.Writer, ...) (status int, exited bool, err error)
- func (s *Shell) RunTTY(ctx context.Context, script string, tty *os.File, rec io.Writer) (status int, exited bool, err error)
- type Stack
Constants ¶
const TopLevel = ""
TopLevel is the name of the env blocks run in when they set no env=.
Variables ¶
var ErrExited = errors.New("shell exited")
ErrExited is returned by Run when the block ended the shell, for example by calling exit. The shell can't run anything else.
var ErrNoPTY = errors.New("shell has no terminal of its own")
ErrNoPTY is returned by RunTTY on a shell started without Config.TTY.
var ErrNotForeground = errors.New("mdsh is not in the terminal's foreground")
ErrNotForeground is returned by RunTTY when mdsh isn't in the terminal's foreground, for example when it runs in the background.
var ErrTapeEnded = errors.New("still running 10s after its input ended")
ErrTapeEnded is returned by RunScripted when the block is still running a while after drive has typed everything, most likely waiting for input that will never come.
Functions ¶
Types ¶
type Config ¶
type Config struct {
Path string // shell binary; defaults to /bin/sh
Env []string // KEY=value, the shell's whole environment
Dir string // working directory
Args []string // positional parameters, $1 and on
// TTY gives the shell a pseudo-terminal of its own as its
// controlling terminal, so RunTTY can run blocks on it.
TTY bool
}
Config starts a shell.
type Env ¶
type Env struct {
Name string // full name, such as "build/test"; "" for the top level
Parent *Env
Shell *Shell
}
Env is a named shell stacked on its parent.
type Shell ¶
type Shell struct {
// contains filtered or unexported fields
}
Shell is one long-lived shell process. Blocks run in it one at a time, so variables, functions and the working directory carry over.
func (*Shell) Run ¶
func (s *Shell) Run(ctx context.Context, script string, stdout, stderr io.Writer) (status int, exited bool, err error)
Run runs script in the shell and returns its exit status. The script's stdin is /dev/null. If the script ends the shell, Run returns ErrExited and the shell's exit status. If ctx ends first, the shell is killed.
func (*Shell) RunScripted ¶
func (s *Shell) RunScripted(ctx context.Context, script string, cols, rows int, out io.Writer, drive func(ctx context.Context, keys io.Writer) error) (status int, exited bool, err error)
RunScripted runs script on the shell's own pseudo-terminal, sized cols by rows, with no person at it: drive types the input, writing to keys, while the script runs, and everything the script shows goes to out. If drive fails, or the script is still running 10s after drive is done, the shell is killed and the error returned.
func (*Shell) RunTTY ¶
func (s *Shell) RunTTY(ctx context.Context, script string, tty *os.File, rec io.Writer) (status int, exited bool, err error)
RunTTY runs script like Run, but on the shell's own pseudo-terminal, with mdsh in the middle: what is typed on tty goes to the script, and what the script writes to its terminal goes to tty and to rec. Since the pseudo-terminal is the controlling terminal of the shell's session, programs that need a terminal, such as docker run -it, work, ctrl+c reaches the running command and window size changes are passed on. An interrupted command ends; the rest of the script goes on. tty must be mdsh's controlling terminal, with mdsh in its foreground group.
type Stack ¶
type Stack struct {
// contains filtered or unexported fields
}
Stack creates envs on first use and keeps them for reuse.