env

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: MIT Imports: 21 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package env runs code in long-lived shell processes and stacks them into named environments.

Index

Constants

View Source
const TopLevel = ""

TopLevel is the name of the env blocks run in when they set no env=.

Variables

View Source
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.

Functions

func Environ

func Environ(vars map[string]string) []string

Environ turns vars into sorted KEY=value pairs.

func Filter

func Filter(vars map[string]string, share []string) map[string]string

Filter keeps the variables named in share. Nil share keeps all of them. "..." (or "*") in share keeps all, and a name prefixed with "-" is dropped even if "..." or the plain name also appears, so [..., -USER] keeps everything but USER.

func Vars

func Vars(environ []string) map[string]string

Vars turns KEY=value pairs into a map.

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 Start

func Start(cfg Config) (*Shell, error)

Start launches a shell.

func (*Shell) Close

func (s *Shell) Close() error

Close ends the shell and removes its temp files.

func (*Shell) Exported

func (s *Shell) Exported(ctx context.Context) (map[string]string, error)

Exported returns the shell's exported variables.

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.

func (*Shell) RunTTY

func (s *Shell) RunTTY(context.Context, string, *os.File, io.Writer) (int, bool, error)

RunTTY 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.

func NewStack

func NewStack(cfg Config) *Stack

NewStack returns a stack whose top-level env starts with cfg.

func (*Stack) Close

func (s *Stack) Close()

Close ends every shell in the stack.

func (*Stack) Get

func (s *Stack) Get(ctx context.Context, name string, share []string) (*Env, error)

Get returns the env called name, creating it and any missing parents. share is used only when the env is created: it lists the parent's exported variables the new env receives. Nil means all of them.

Jump to

Keyboard shortcuts

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