Documentation
¶
Overview ¶
Package execpath finds the external programs the app runs (gh, git, claude) and runs them.
An app started from the Finder or the Dock inherits launchd's minimal PATH, which lacks Homebrew, ~/.local/bin, and other common install locations. Search.Find therefore tries a search path (the user's login shell's, when Search.Path is set, else the inherited one) and then a short list of well-known directories for the current OS. Exec runs a program as an argv, never through a shell, and Start runs a long-lived one in its own process group (Proc). Every service that finds a program takes a Finder, and every service that runs one takes a Runner, so tests can hand it a fake. ExitCode and GitMessage read a failed program's outcome.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("executable not found")
ErrNotFound is returned when a program is on neither the search path nor any of the well-known directories.
var ErrOutputLimit = errors.New("the program printed more than the limit")
ErrOutputLimit is returned by Exec.Run for a program that printed more than its Limit allows; the program is stopped, and none of its output is returned.
var ErrUnsupported = errors.New("process groups are not supported on this OS")
ErrUnsupported is returned by Start where process groups don't exist (Windows).
Functions ¶
func Executable ¶ added in v0.2.0
Executable reports whether a file with mode m is a program someone may run: a regular file with an execute bit.
func ExitCode ¶ added in v0.2.0
ExitCode returns the status a program exited with when err holds its exit (a non-zero exit status is an *exec.ExitError in Exec's error chain), or -1 when err holds none: the program could not be run, was killed by a signal, or err is nil.
func FallbackPath ¶ added in v0.2.0
FallbackPath returns the search path to use when the user's login shell's PATH can't be read: the well-known directories for home first, then inherited, without repeats. On macOS a GUI app inherits launchd's /usr/bin:/bin:/usr/sbin:/sbin, where Apple's git asks in a keychain window for credentials another git stored and so stalls a run with no terminal; the well-known directories hold the git and tools the user's terminal runs.
func FirstLine ¶ added in v0.2.0
FirstLine returns the first non-blank line of a program's output, trimmed, or "".
func GitMessage ¶ added in v0.2.0
GitMessage returns the first non-blank line of git's error output without its "fatal: " or "error: " prefix: the part of a git failure worth showing.
Types ¶
type Cmd ¶ added in v0.2.0
type Cmd struct {
// Path is the program's absolute path.
Path string
// Args are the arguments after the program.
Args []string
// Dir is the working directory; "" is the app's.
Dir string
// Env is the whole environment, as in exec.Cmd.Env; nil is the app's.
Env []string
// Output gets stdout and stderr both, in one ordered stream; nil discards them.
Output *os.File
// Stdin is what the program reads on its standard input, such as a file holding a
// prompt, so the text never appears on a command line; nil reads /dev/null.
Stdin *os.File
}
Cmd is a long-lived program to start in its own process group: a run the user started, or the login shell read. Its output goes straight into Output, never through a pipe the app reads, so a crash of the app can't break the program's output midway and a descendant holding that output can't hang Wait.
type Exec ¶ added in v0.2.0
type Exec struct {
Search Search
// Env, when non-nil, is the whole environment each program runs with, as in
// exec.Cmd.Env; nil runs it with this process's environment. It lets a caller, such
// as a test isolating git from the user's configuration, set a program's environment
// without changing the process's, which every other goroutine shares. Search looks a
// program up on its own Path, or this process's search path, never on a PATH in Env.
Env []string
// Dir, when set, is the folder each program runs in, as in exec.Cmd.Dir: a tool that
// reads the repository it runs in, such as go-pre-commit, takes it from there. "" runs
// it in this process's folder.
Dir string
// Limit, when above zero, is the most of a program's standard output Run holds, in
// bytes: a program that prints more is stopped, and Run fails with ErrOutputLimit, so
// a program whose output a repository controls, as git's listing of a tree, can't
// grow the app's memory without bound. Zero holds it all.
Limit int
// contains filtered or unexported fields
}
Exec runs programs as argv with exec.CommandContext. A name that is not an absolute path is resolved with Search first.
func (Exec) Run ¶ added in v0.2.0
func (r Exec) Run(ctx context.Context, name string, args ...string) (stdout, stderr []byte, err error)
Run runs name with args and returns what it wrote to stdout and stderr. The error is the lookup or exec error, wrapped; a non-zero exit is an *exec.ExitError in the chain, so callers can read its exit code (see ExitCode). A program that exits but leaves a descendant holding its output is waited for runWaitDelay more, then its output so far is returned and its exit status counts: such a descendant can't hang Run past the program. A program that prints more than Limit is stopped, and Run fails with ErrOutputLimit, returning none of its standard output.
type Finder ¶
Finder resolves a program name to the absolute path of an executable. Search is the app's.
type Ladder ¶ added in v0.2.0
type Ladder []Step
Ladder is how Stop ends a group: each step's signal in turn, until the program ends. It should end with SIGKILL.
func StopLadder ¶ added in v0.2.0
func StopLadder() Ladder
StopLadder is the ladder for a run the user stops: SIGINT, then SIGTERM after five seconds, then SIGKILL after ten more.
func (Ladder) Climb ¶ added in v0.2.0
Climb sends each step's signal in turn through signal, waiting the step's time between them, until done closes or alive, when given, reports the target gone. Proc.Stop climbs with it; a caller that holds no Proc, such as one stopping a program from before the app restarted, passes its own alive and signal, which check the group is still its own.
type Proc ¶ added in v0.2.0
type Proc struct {
// contains filtered or unexported fields
}
Proc is a program Start started, the leader of its own process group. A descendant that calls setsid (an ssh or gpg agent, a daemon) leaves the group, and is not stopped with it.
func Start ¶ added in v0.2.0
Start starts cmd in a new process group, with stdin from cmd.Stdin, or /dev/null, and stdout and stderr both written straight into cmd.Output. When ctx ends, the whole group is killed. When the program exits, the rest of its group gets SIGHUP, as closing a terminal sends it: what the program left running in the background ends, unless it was started to survive a hang-up (nohup) or left the group (setsid).
func (*Proc) Done ¶ added in v0.2.0
func (p *Proc) Done() <-chan struct{}
Done is closed once the program has exited and Wait's answer is ready.
func (*Proc) Killed ¶ added in v0.4.0
Killed reports whether the group was sent SIGKILL, by Signal or by Stop's ladder. A program that gets SIGKILL can't stop what it started in the background, so whatever of it left the group may still run.
func (*Proc) Stop ¶ added in v0.2.0
Stop climbs ladder until the program exits: it sends the first step's signal to the group now, and each later one once the step before has waited in vain. It returns at once; the waiting happens in the background and ends with the program. Only the first call counts.
func (*Proc) Wait ¶ added in v0.2.0
Wait waits for the program to exit and returns its exit status: its exit code, or 128 plus the signal's number when a signal ended it. err is the *exec.ExitError of a non-zero status, the context's error when Start's context ended it, or why it couldn't be waited for. Every call returns the same answer.
type Runner ¶
type Runner interface {
Run(ctx context.Context, name string, args ...string) (stdout, stderr []byte, err error)
}
Runner runs a program with arguments and returns what it wrote. Implementations run the argv directly, never through a shell; Exec is the app's.
type Search ¶ added in v0.2.0
type Search struct {
// Path, when set, is the search path Find tries first, in place of the inherited
// PATH: the user's login shell's PATH (internal/shellenv), so the app finds the same
// git, magex, and go-pre-commit as the user's terminal. FallbackPath builds one for
// when the shell's can't be read.
Path string
// Home is the user's home directory. Home-relative well-known directories are skipped
// when it is empty.
Home string
// Extra lists additional directories to search, before the well-known ones.
Extra []string
}
Search finds programs on a search path and in the well-known directories where command-line tools are installed. Its zero value searches the inherited PATH and those directories without the home-relative ones.
func (Search) Find ¶ added in v0.2.0
Find returns the absolute path of the named executable. It tries the search path first (Path, or the inherited PATH through exec.LookPath), then each extra and well-known directory in order, and returns ErrNotFound naming the program and every directory searched when none has it.