shellenv

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 16 Imported by: 0 modules, 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 shellenv reads the environment the user's login shell sets up, as editors do, so the tools the app runs see the PATH, the ssh agent, and the settings the user's terminal has. An app started from the Finder or the Dock gets only launchd's few variables and its /usr/bin:/bin:/usr/sbin:/sbin.

A read runs the shell named by $SHELL (or /bin/zsh when that is unset or not listed in /etc/shells) as an interactive login shell, in its own process group, with stdin from /dev/null and a time limit that kills the whole group: [$SHELL -i -l -c "printf marker; /usr/bin/env -0; printf marker"]. The marker is random each time, so whatever the shell's startup files print before or after it is ignored, and the values are split on NUL, so a value holding a newline survives.

It never logs, stores, or returns in an error a variable's value or its name: names can carry private project names. The log gets how long a read took, how many variables it found, and why one failed.

Index

Constants

View Source
const (
	// DefaultTimeout bounds one read.
	DefaultTimeout = 5 * time.Second
)

Variables

View Source
var (
	// ErrNoMarkers is returned when the shell's output doesn't hold the environment
	// between two markers: the shell failed early, or printed nothing.
	ErrNoMarkers = errors.New("the shell's output had no environment between its markers")

	// ErrNoVariables is returned when the shell's environment was empty.
	ErrNoVariables = errors.New("the shell reported no variables")
)

Functions

func Await

func Await(ctx context.Context, ready <-chan struct{}) bool

Await waits until ready, a Reader's Ready, is closed, or until ctx ends, and reports whether ready was closed: what runs before a Read has ended, such as a run settled as the app starts, waits for it with this, so a quit never waits on a read that never comes.

func Lookup

func Lookup(env []string, key string) string

Lookup returns key's value in env, the last one when it repeats, or "".

func LookupOK

func LookupOK(env []string, key string) (string, bool)

LookupOK returns key's value in env, the last one when it repeats, and whether env sets key at all, even to "". An entry's key is its text up to its first "=", and its value the rest: a key holding "=" is never set (a value may hold one), and an entry without "=" sets nothing.

func RunShell

func RunShell(ctx context.Context, argv, env []string) ([]byte, error)

RunShell runs argv in its own process group (execpath.Start) with env, and returns what it wrote to stdout and stderr: the first 4 MiB, with the rest read and dropped. Once the shell exits it waits a moment more for a program the shell started to let go of the output, then stops reading.

func With

func With(env []string, key, value string) []string

With returns a copy of env with key set to value, last, in place of every entry that named it before (Without). env itself is never changed.

func Without

func Without(env []string, keys ...string) []string

Without returns a copy of env, in its order, without every entry whose key is one of keys: its text up to its first "=", or its whole text when it has none, so a bare KEY goes too. env itself is never changed, and the copy is never nil.

Types

type Config

type Config struct {
	// Start runs the shell. It defaults to RunShell.
	Start Starter

	// Environ returns the app's own environment: what the shell starts from, and what
	// runs get when the shell's can't be read. It defaults to os.Environ.
	Environ func() []string

	// Home is the user's home folder, for the well-known tool folders a fallback PATH
	// puts first (execpath.FallbackPath).
	Home string

	// Shells is the file of valid login shells. It defaults to /etc/shells.
	Shells string

	// Timeout bounds one read. It defaults to DefaultTimeout.
	Timeout time.Duration

	// Logger gets one record per read. Without it nothing is logged.
	Logger *slog.Logger
}

Config holds a Reader's settings. Every field is optional.

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader reads the login shell's environment and keeps the latest one it read. It is safe for concurrent use.

func New

func New(cfg Config) *Reader

New returns a Reader over cfg that hasn't read yet.

func (*Reader) Env

func (r *Reader) Env() ([]string, bool)

Env returns the environment the latest successful read found, and true; until a read has succeeded, it returns the app's own environment with execpath.FallbackPath as PATH, and false. The slice is the caller's to change.

func (*Reader) Path

func (r *Reader) Path() string

Path returns PATH from Env: the shell's, or the fallback.

func (*Reader) Read

func (r *Reader) Read(ctx context.Context) error

Read reads the login shell's environment, keeps it, and returns nil; or it returns why it couldn't, keeping what an earlier read found. Ready closes once the first read ends, either way.

func (*Reader) Ready

func (r *Reader) Ready() <-chan struct{}

Ready is closed once the first Read has ended, whether it read the environment or not. Nothing reads before the window's first paint, and a startup measurement never reads, so a caller waits on it together with its own context, never on it alone (Await).

func (*Reader) Shell

func (r *Reader) Shell() string

Shell returns the user's login shell: $SHELL from the app's environment when it is a listed login shell (in /etc/shells), else /bin/zsh. The read runs it, and so does a terminal the app opens in a folder.

type Starter

type Starter func(ctx context.Context, argv, env []string) ([]byte, error)

Starter runs a shell's argv with env, stdin from /dev/null, and returns what it wrote, stopping it when ctx ends. RunShell is the real one; tests hand in their own, so no test runs the developer's own shell.

Jump to

Keyboard shortcuts

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