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 Job
- type Shell
- func (s *Shell) Background(ctx context.Context, script, pidVar string) (*Job, error)
- func (s *Shell) Close() error
- func (s *Shell) Exited() bool
- 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(context.Context, string, int, int, io.Writer, ...) (int, bool, error)
- func (s *Shell) RunTTY(context.Context, string, *os.File, io.Writer) (int, bool, error)
- func (s *Shell) RunTerminal(context.Context, string, Terminal, io.Writer) (int, bool, error)
- type Stack
- type Terminal
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 ErrKilled = errors.New("killed")
ErrKilled is a job's error when Kill ended it, or its shell ended first.
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 Job ¶ added in v1.2.0
type Job struct {
// PID is the background job's process ID, as $! gave it.
PID int
// contains filtered or unexported fields
}
Job is a script running in the background of its shell, started by Background. Its output goes to files, so it never mixes with the output of what the shell runs next.
func (*Job) Done ¶ added in v1.2.0
Done reports whether the job has ended, and its exit status. A job that was killed, or whose shell ended, has status -1 and ErrKilled.
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) Background ¶ added in v1.2.0
Background starts script as a background job of the shell, the way `script &` would: in a subshell, so it sees the shell's variables, functions and working directory but what it sets stays its own. Its stdin is /dev/null and its stdout and stderr are kept apart from the shell's. pidVar, if not empty, is a shell variable set to the job's process ID, so later scripts can wait for it.
A script with a syntax error doesn't start: the job is already done, with status 2 and the error as its stderr.
func (*Shell) Exited ¶ added in v1.1.0
Exited reports whether the shell has ended, by a block calling exit or by being killed.
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(context.Context, string, int, int, io.Writer, func(context.Context, io.Writer) error) (int, bool, error)
RunScripted is not supported here.
type Stack ¶
type Stack struct {
// contains filtered or unexported fields
}
Stack creates envs on first use and keeps them for reuse.
type Terminal ¶ added in v1.2.0
type Terminal struct {
// Keys carries what is typed, as the bytes a terminal would send.
Keys <-chan []byte
// Out shows what the script writes to its terminal.
Out io.Writer
// Size is the terminal's size in cells. Nil, or a zero size, means
// 80 by 24.
Size func() (cols, rows int)
// Resized, if not nil, says when Size changes.
Resized <-chan struct{}
}
Terminal is a terminal mdsh draws itself, such as a pane in a window, for RunTerminal. It has no file of its own: keys arrive on a channel and what the script shows goes to a writer.