Documentation
¶
Overview ¶
Package perhost is THE single reader and writer of the per-host charly config (~/.config/charly/charly.yml, honoring CHARLY_DEPLOY_CONFIG).
The file is a SHARED document: the deploy overlay, the git-ref cache, the install ledger and the system info all live in it as top-level sections, written by DIFFERENT charly commands that can run CONCURRENTLY. Four independent copies of "take <path>.lock, read the yaml.Node, find-or-create my key, marshal, tempfile+rename" grew here, and the copies disagreed about the contract in ways that destroyed real data on 2026-11-24:
- one cleared a section by DELETING the whole file (every deploy entry with it);
- one wrote a stale in-memory value over a value another process had just changed.
Update is that contract, stated once: one lock, one read INSIDE the lock, one mutate that touches only the keys it owns, one atomic rename. A corrupt or unparseable file is never clobbered — mutate is not called at all. Callers supply ONLY the section logic; the file mechanics are here.
Index ¶
- Constants
- func Apply(path string, mutate func(root *yaml.Node) (changed bool, err error)) error
- func Get(root *yaml.Node, keys ...string) *yaml.Node
- func Lock(path string) (release func() error, err error)
- func Read(path string) (*yaml.Node, error)
- func Set(root *yaml.Node, value *yaml.Node, keys ...string) bool
- func Update(path string, mutate func(root *yaml.Node) (changed bool, err error)) error
Constants ¶
const LockWait = 10 * time.Second
LockWait bounds the wait for the shared advisory lock. A section write is milliseconds, so a legitimate queue clears well inside this; a holder that outlives it is a stuck writer or a cross-command collision, and both deserve a fast, named error rather than an unbounded hang.
Variables ¶
This section is empty.
Functions ¶
func Apply ¶ added in v0.2026328.1115
Apply runs the document mechanics of one read-modify-write WITHOUT taking the lock — steps 2-4 of Update: read the CURRENT file, mutate its root, marshal, replace atomically.
THE CALLER MUST ALREADY HOLD Lock(path). Apply exists so that a wider cycle which holds the lock across a typed read-modify-write (deploykit.MutateDeployConfig) writes through the SAME mechanics rather than a second copy of read/marshal/tempfile/rename. Acquiring the lock here as well would self-deadlock: flock is per-open-file-description, so a second acquire inside the same process waits for the first and fails at LockWait.
func Get ¶
Get returns the value node at the given key path (e.g. Get(root, "cache", "git", "bypass")), or nil when any step is absent or not a mapping.
func Lock ¶ added in v0.2026328.1115
Lock acquires the advisory lock that serializes every writer and every wider read-modify-write cycle over the per-host config (<path>.lock), waiting at most LockWait. It is THE definition of that lock: the path and the bound live here, so a caller whose critical section is WIDER than one document update — deploykit's typed overlay cycle, or a save-versus-remove decision — takes THIS lock and then calls Apply, instead of re-deriving the path and re-inventing the bound.
func Read ¶
Read parses the per-host config at path. A missing file is (nil, nil) — a fresh host; a corrupt file is an error, never an empty document (a writer must refuse, not truncate).
func Set ¶
Set stores value at the given key path, creating intermediate mappings. A nil value REMOVES the leaf key. It reports whether the document changed.
func Update ¶
Update runs ONE locked read-modify-write cycle over the per-host config:
- take <path>.lock (Lock, bounded: LockWait);
- Read the CURRENT on-disk document — never a caller's snapshot, which is how a concurrent writer's change gets lost;
- call mutate on the document's root mapping; mutate touches ONLY the keys it owns and reports whether anything changed;
- when it did, marshal and replace the file atomically (tempfile in the same directory + rename), so a reader never sees a half-written file.
A corrupt file is an error and mutate is NOT called: the bytes stay for recovery.
Types ¶
This section is empty.