util

package
v0.160.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 9 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 util contains the file-only part of Gofer binary upgrades. Worker handover and managed server lifecycle remain with their own owners.

Package util holds gofer's small, dependency-free helpers: the allocation capacity hints (CapSum/CapMul) and the environment layering used before a child process is spawned (Environ/MergeEnv/EnvWith).

It is a leaf package: it imports nothing from gofer and only the standard library, so any layer may use it without risking an import cycle (G022). Keep it that way — anything with a real domain, a dependency, or more than a handful of lines belongs in its own package, not here.

Index

Constants

View Source
const MaxHint = 1 << 20

MaxHint is the largest capacity hint any caller can receive. It is deliberately far below any int limit so that the arithmetic in CapSum/CapMul can never wrap, and high enough (1M elements) that no realistic collection in gofer is clamped: these hints size env blocks, argv blocks and log lines, not bulk payloads.

Variables

This section is empty.

Functions

func CapMul

func CapMul(n, k int) int

CapMul returns a capacity hint for a collection holding n groups of k elements each: CapMul(len(m), 2) for "two entries per map key". Non-positive factors yield 0. The result is always in [0, MaxHint].

func CapSum

func CapSum(parts ...int) int

CapSum returns a capacity hint for a collection that will hold the given element counts added together: CapSum(len(a), len(b)) for "a followed by b". Non-positive parts contribute nothing (a nil map/slice has no elements). The result is always in [0, MaxHint], so the hint can never overflow and can never be rejected by make.

func EnvWith

func EnvWith(base, overrides map[string]string) map[string]string

EnvWith returns a fresh copy of base with overrides layered on top. Unlike MergeEnv it always copies, and overrides always win: it is the seam for gofer-owned keys (job metadata, worktree identity, ...) that a caller-supplied env key must never shadow, and the result is safe for the caller to keep mutating.

func Environ

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

Environ returns os.Environ() with extra layered on top, ready for exec.Cmd.Env or a pty spec. extra wins on key collision: os/exec (and therefore both the local and the pty backend) keeps the LAST occurrence of a duplicated key, so appending extra after the inherited environment overrides it. An empty extra returns the inherited environment unchanged.

func EnvironKeyDenied

func EnvironKeyDenied(key string, deny, allow []string) bool

EnvironKeyDenied reports whether key is denied by the deny list and not re-admitted by the allow list. Exported because the job pipeline needs the same decision to report WHICH allowances actually took effect (job.env_allowed) — a second spelling of the rule would let the event disagree with the environment.

func EnvironKeysPresent

func EnvironKeysPresent(names []string) []string

EnvironKeysPresent returns the subset of names that the CURRENT process environment actually carries a value for, preserving the input order. It is the "which of these keys does the allow list really hand to a child" question the SEC-01 job.env_allowed event answers, without copying values around.

func EnvironWithout

func EnvironWithout(deny, allow []string, extra map[string]string) []string

EnvironWithout is Environ with a DENY list applied to the inherited environment (SEC-01): every os.Environ() entry whose key is named in deny is dropped before extra is layered on, unless the same key is named in allow.

The filter exists because gofer's own credentials live in the process environment — `server.token_env: GOFER_TOKEN` for a serve started from the deployment's .env, GOFER_SERVER_TOKEN / GOFER_WORKER_TOKEN for the CLI and the worker — and a job process that inherits them is handed the operator's bearer token. That is not theoretical: a v0.57 leader job without gofer MCP fell back to the inherited server token and reviewed work as the human (design §背景).

Only the INHERITED environment is filtered. `extra` is the caller's own, written-down decision (agent/role/job config, gofer metadata) and passes through verbatim: those keys are configured deliberately, not inherited by accident.

Key matching is case-insensitive, because Windows environment variable names are (and os.Environ preserves whatever case it was set with).

func MergeEnv

func MergeEnv(base, extra map[string]string) map[string]string

MergeEnv returns base with extra layered on top (extra wins on key collision) as a fresh map; neither input is modified. An empty extra returns base itself — there is nothing to add, so no copy is needed.

func ParseVersionOutput

func ParseVersionOutput(raw string) (string, error)

ParseVersionOutput removes terminal color and returns the version token printed by Gofer's --version. Worker and managed server upgrades share it.

func RealPath

func RealPath(p string) string

RealPath resolves symlinks on the existing prefix of p and re-appends the non-existent tail. When nothing resolves it falls back to the lexical clean, so pure-logical (non-existent) paths map to themselves.

func StageUpgradeFile

func StageUpgradeFile(source, target, upgradeID string, size int64, wantSHA256 string) (string, error)

Stage copies a verified candidate into the executable's directory, so the final rename stays on one volume. The destination is unique to upgradeID.

func SwitchBinary

func SwitchBinary(target, candidate string) (rollback func() error, err error)

Switch parks the old image beside target, installs a same-volume candidate, and returns a rollback closure. The old image remains until caller cleanup.

func VerifyUpgradeFile

func VerifyUpgradeFile(path string, size int64, wantSHA256 string) error

Types

This section is empty.

Jump to

Keyboard shortcuts

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