Documentation
¶
Overview ¶
Package workspacecmd implements `golder workspace`: the command line that starts, inspects and stops golder's own workspace around a project.
golder workspace [run] [flags] [-- <agent args>...] start it, and attach golder workspace status [flags] one JSON status document golder workspace stop [flags] shut it down
The package is the argv-and-identity half of the command and nothing more. It parses the flags, decides which project this workspace belongs to, where its config, state, log and socket live, and whether starting one here would nest it inside another multiplexer; then it hands one resolved Options value to a Backend, which owns the daemon. That split is why this file has no os.Exit, no process-wide chdir, no environment mutation, no signal subscription and no dependency outside the standard library plus internal/hostcompat and internal/golderhome: a policy that can only be tested against a real terminal is a policy that is not tested.
Three decisions are worth stating, because they are what the flags mean:
- Visible by default. `golder workspace` and `golder workspace run` start a workspace a person can see and use. --headless is the explicit "daemon only" mode: it starts the same workspace, attaches nothing, and prints one readiness JSON document so a script can reach it.
- Arguments after -- are an argv. They are handed to the golder agent this workspace hosts as tokens, never joined into a string and never re-parsed by a shell. `golder workspace -- --model opus` passes two arguments, and `golder workspace -- 'a; rm -rf b'` passes one literal one.
- The project, not the working directory, owns the files. Config, state and log live under one durable directory derived from the project's canonical path (symlinks resolved where they resolve) and a hash of it, so two checkouts of the same name never share them, and neither does the same checkout reached through a link.
Index ¶
- Constants
- func Run(ctx context.Context, args []string, stdout, stderr io.Writer, backend Backend) int
- func RunWith(ctx context.Context, args []string, stdout, stderr io.Writer, backend Backend, ...) int
- func RuntimeDir(getenv func(string) string) string
- type Appearance
- type Backend
- type Command
- type Deps
- type Options
- type Paths
- type StartResult
Constants ¶
const ( // ExitOK is success, including a help request. ExitOK = 0 // ExitFailure is a workspace that could not be started, reached or // stopped: a backend error, an unusable state root, a refused nesting, a // backend that is not wired into this build. ExitFailure = 1 // ExitUsage is argv this command cannot act on: an unknown flag, a flag // without its value, an extra argument, a session name the daemon would // refuse, a --socket path that cannot be a Unix socket. ExitUsage = 2 // ExitInterrupted is a cancellation: the context was already done before // the backend was asked, or the backend returned because it was cancelled. // It is 128+SIGINT, the code a shell reports for a Ctrl+C'd command. ExitInterrupted = 130 )
Exit codes Run returns. They are exported because the dispatcher in cmd/golder passes the code straight to os.Exit and a script may want to name them; nothing else in this package exits a process.
Variables ¶
This section is empty.
Functions ¶
func Run ¶
Run executes one `golder workspace` invocation with args — the words after `workspace` in argv — writing its output to stdout and its errors to stderr. It returns the process exit code (see the Exit constants) and never exits, changes directory or touches the environment itself.
func RunWith ¶
func RunWith(ctx context.Context, args []string, stdout, stderr io.Writer, backend Backend, deps Deps) int
RunWith is Run with the environment seams named, which is what a test needs to state a GOLDER_HOME and a multiplexer instead of inheriting one.
func RuntimeDir ¶
RuntimeDir is the short, private directory default sockets live in: $XDG_RUNTIME_DIR/golder, else $TMPDIR/golder-<uid>, else /tmp/golder-<uid>. It is exported because a backend writing a pid file or a lock beside the socket should use the same directory rather than guess at one.
A socket path is limited by the kernel to about a hundred bytes, so this is deliberately not under the state root: $GOLDER_HOME can be arbitrarily deep, and a project directory is usually deeper still. Neither is it in the runtime directory bare: a socket in a world-writable /tmp is a socket anyone can connect to, so it sits one level down, in a directory this command creates 0700 and keeps that way (see ensurePrivateDir). Under a shared temp directory the name carries the uid, so two accounts on one machine cannot contend for the same socket directory, pid file or lock; see privatepath.RuntimeRoot, which owns the rule.
Types ¶
type Appearance ¶
type Appearance struct {
// ASCIIOnly draws panes without Nerd Font glyphs.
ASCIIOnly bool
// NoAnimations disables the workspace's UI animations.
NoAnimations bool
// Theme is a colour theme name, "" for the configured one.
Theme string
}
Appearance is the small subset of the workspace's display settings the command line accepts. A zero Appearance asks for the configured defaults, which is what every field means when it is unset: there is a flag to turn icons and animations off and none to turn them back on.
type Backend ¶
type Backend interface {
// Start starts the workspace Options describes and returns once it is
// ready to use. In visible mode it also attaches or opens a window on it,
// which normally means Start returns when the person leaves.
Start(ctx context.Context, opts Options) (StartResult, error)
// Status returns the workspace's status as one JSON document. The command
// re-indents and validates it; it does not interpret it.
Status(ctx context.Context, opts Options) ([]byte, error)
// Stop stops the workspace and its daemon. Stopping a workspace that is
// not running is a backend's decision; the usual answer is success.
Stop(ctx context.Context, opts Options) error
}
Backend is the workspace implementation behind the command: the daemon, its panes and its client. It is an interface here, and only here, so that the command line can be read and tested without a terminal, a daemon or a binary on PATH.
The command owns the argv surface, the project identity, the paths and the exit codes; a backend owns everything that touches the machine. Every method receives the same fully resolved Options, and must respect Headless (start the daemon and return, attach nothing) and Shell (no initial agent).
type Command ¶
type Command string
Command is the operation one `golder workspace` invocation asks for.
const ( // CommandRun starts a workspace: visible and attached unless // Options.Headless, which starts the daemon alone. CommandRun Command = "run" // CommandStatus reports a running workspace as one JSON document. CommandStatus Command = "status" // CommandStop stops a running workspace. CommandStop Command = "stop" )
type Deps ¶
type Deps struct {
// Getenv reads the environment: GOLDER_HOME, the runtime directory and the
// multiplexer markers hostcompat parses.
Getenv func(string) string
// Getwd is the project directory when --cwd was not given.
Getwd func() (string, error)
}
Deps are the seams a test (or a driver that knows better) can replace. Every field is optional; a zero Deps reads the real environment and the real working directory.
type Options ¶
type Options struct {
// Command is the operation being asked for.
Command Command
// Paths is where this workspace keeps its files. It is embedded, so
// opts.StateDir reads as one field and opts.Paths reads as the whole set.
Paths
// Cwd is the project directory the workspace's panes start in: absolute,
// with symlinks resolved where they resolve.
Cwd string
// Session is the workspace session name: --session, or one derived from
// the project. It is also a file name component, so it is validated.
Session string
// Socket is the daemon's control socket, absolute.
Socket string
// Headless asks for the daemon alone: no client is attached, and Start
// returns as soon as the workspace is ready.
Headless bool
// Shell asks for a workspace whose first pane is a shell rather than an
// agent. AgentArgs is then empty, by construction (see parsed.validate).
Shell bool
// Nested records that --nested was given: this workspace was asked for
// inside another multiplexer, and the person knows.
Nested bool
// Host is the multiplexer this process runs in, as hostcompat names it
// ("golder-workspace", "tuios", "herdr", "tmux", "zellij", "screen"), or
// "" in a plain terminal. A backend may use it to say where the workspace
// was started from; it has already been used for the nesting decision.
Host string
// AgentArgs is the argv after "--": the golder agent's own arguments,
// exactly as typed. It is never joined into a command line.
AgentArgs []string
// Appearance is the display settings asked for on the command line.
Appearance Appearance
}
Options is one resolved `golder workspace` invocation: everything a backend needs, and nothing it has to derive for itself.
type Paths ¶
type Paths struct {
// Home is golder's state root: $GOLDER_HOME, else ~/.golder.
Home string
// Project is the project's canonical path: absolute, symlinks resolved
// where they resolve. It is what ProjectID hashes and what Cwd is.
Project string
// ProjectID is the stable identity of Project: the first 12 hex
// characters of its sha256. Two projects never share it.
ProjectID string
// ProjectDir is the project's durable directory:
// <Home>/workspace/<ProjectID>.
ProjectDir string
// ConfigPath is the workspace's config file (--config, else config.toml
// in ProjectDir). It is project-scoped: every session of one checkout reads
// the same config.
ConfigPath string
// SessionDir is this session's durable directory inside ProjectDir,
// <ProjectDir>/sessions/<session>. It holds the state directory and the log,
// and is where a backend should put anything else that belongs to one
// session instance (a pid file, a lock).
SessionDir string
// StateDir is the workspace's state directory: --state-dir when it was
// given, else state/ in SessionDir. The default is per session, so two
// sessions of one project never share a session store; --state-dir is the
// person's own decision, and two sessions given the same directory do share
// it, which is what StateDirExplicit records for a backend that has to
// arbitrate (its lock decides who owns it, not this package).
StateDir string
// StateDirExplicit records that --state-dir named StateDir, so a backend can
// tell a per-session default from a directory the person deliberately shared
// between sessions.
StateDirExplicit bool
// LogFile is the workspace daemon's log, in SessionDir: one session's
// history is one file to read, and two sessions never append to one file.
LogFile string
// SocketDir is the private directory the default socket is created in, and
// "" when --socket named a path (a custom socket is used verbatim, and its
// directory is the person's to manage).
SocketDir string
}
Paths are the durable and runtime locations one workspace uses.
type StartResult ¶
type StartResult struct {
// Session is the session the workspace is running under. Empty keeps the
// name the options asked for.
Session string
// Socket is the daemon socket the workspace listens on. Empty keeps the
// path the options named.
Socket string
}
StartResult is what a backend reports once a workspace is up. It exists so a backend can name what actually happened — a daemon that generated its own session name, a socket the runtime chose — without the caller having to re-derive it.