execpath

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: 14 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 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

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

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

func Executable(m fs.FileMode) bool

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

func ExitCode(err error) int

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

func FallbackPath(home, inherited string) string

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

func FirstLine(out []byte) string

FirstLine returns the first non-blank line of a program's output, trimmed, or "".

func GitMessage added in v0.2.0

func GitMessage(stderr []byte) string

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
}

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

type Finder

type Finder interface {
	Find(name string) (string, error)
}

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

func (l Ladder) Climb(done <-chan struct{}, alive func() bool, signal func(syscall.Signal) error)

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

func Start(ctx context.Context, cmd Cmd) (*Proc, error)

Start starts cmd in a new process group, with stdin from /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) Pgid added in v0.2.0

func (p *Proc) Pgid() int

Pgid returns its process group's ID, which is its PID.

func (*Proc) Pid added in v0.2.0

func (p *Proc) Pid() int

Pid returns the program's process ID.

func (*Proc) Signal added in v0.2.0

func (p *Proc) Signal(sig syscall.Signal) error

Signal sends sig to the program's whole process group.

func (*Proc) Stop added in v0.2.0

func (p *Proc) Stop(ladder Ladder)

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

func (p *Proc) Wait() (exitCode int, err error)

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

func (f Search) Find(name string) (string, error)

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.

type Step added in v0.2.0

type Step struct {
	Signal syscall.Signal
	Wait   time.Duration
}

Step is one rung of a Ladder: a signal for the whole group, then how long to wait for the program to end before the next rung.

Jump to

Keyboard shortcuts

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