spec

package
v0.34.0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: MIT, MIT Imports: 10 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 spec parses and validates a `preview.yml` into a resolved Stack.

Design notes that matter:

  • `${VAR}` interpolation happens ONCE, here, against a known env. Nothing downstream re-interpolates, so a value containing `${...}` can never be re-expanded by accident.
  • `stack` resolves to the single identity string every axis namespaces off. It is exported to hooks as $STACK so a hook never has to reconstruct it.
  • Axis order is significant: provisioned in declaration order, destroyed in REVERSE.

The TS module kept a module-global `warnings` array; here they are a field on the result, which is what a goroutine-per-request server needs anyway.

Presence, not nullness, is what every optional key is tested on: the TS checked `=== undefined`, and a key written with no value (`compose:` and nothing after it, `kind: ~`) is PRESENT and null — "`compose` must be a mapping", "kind: expected a string, got object" — never the default. Hence `raw.Get(k)` with only `ok` inspected, everywhere but `version`, whose `?? 1` does read a null as the default.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EscapeHostRegexp

func EscapeHostRegexp(host string) string

EscapeHostRegexp escapes a hostname for embedding in a Go regexp. regexp.QuoteMeta escapes the same fourteen bytes as the JS class `[.*+?^${}()|[\]\\]` (verified against the Go source), so the wake-router line and every generated HostRegexp stay byte-identical.

func Interpolate

func Interpolate(input string, vars map[string]string, where string, host *HostValues) (string, error)

Interpolate substitutes `${VAR}` from vars and `${vars.X}`/`${secrets.X}` from host. An unknown or EMPTY variable is an error rather than "": silently expanding `${PR}` to "" yields a stack named `pr-` that collides across every PR. Missing names are reported in first-seen order.

func IsSpecError

func IsSpecError(err error) bool

IsSpecError reports whether err is a *Error.

func ParseDuration

func ParseDuration(v string) (int64, bool)

ParseDuration is `90s`, `30m`, `2h`, `3d`, `1h30m` → ms; 0 and false for anything else. Integers only; a bare number is refused because "sleep after 30" cannot be read without guessing the unit.

func ResolvePreviewDomain

func ResolvePreviewDomain(vars map[string]string, declared []string, warn func(string)) string

ResolvePreviewDomain picks the domain generated hostnames anchor to: a DECLARED PREVIEW_DOMAIN, then a declared DOMAIN, then the ambient ones — a declaration beats an ambient value, because a stray DOMAIN in a shell would otherwise anchor every rule on the wrong domain. Both set and disagreeing is a warning, not an error.

The chain is `??`, not `||`: the first candidate that is PRESENT wins even when it is "" (a declared `PREVIEW_DOMAIN: ""` yields no domain rather than falling through to DOMAIN), and "" is then returned as "none". A map key present with "" is how Go spells that distinction.

func SubdomainEnv

func SubdomainEnv(routes []SubdomainRoute) map[string]string

SubdomainEnv is the env handed to compose so a label can interpolate `${PSTACK_WILD_<P>}`.

func SubdomainVarName

func SubdomainVarName(profile string) string

SubdomainVarName is `backend` → `PSTACK_WILD_BACKEND`. Dashes become underscores, which makes `admin-api` and `admin_api` collide — the resolver checks for that.

func WildcardRule

func WildcardRule(host string, depth Depth) string

WildcardRule is the Traefik rule for one wildcard host. Anchored at both ends; the bare host is deliberately NOT matched — that is the exact-host router's job.

Types

type Axis

type Axis struct {
	Name       string
	Up         string // Provision. Must be idempotent — `up` is re-run on every redeploy.
	Down       string // Destroy. Best-effort: a non-zero exit is reported but does not abort teardown.
	AssertGone string // Exit 0 ⇒ the resource is GONE. The leak gate.
	AssertLive string // Exit 0 ⇒ the resource EXISTS. Run after `up` to fail fast.
}

Axis is one provisioned thing.

func (Axis) Hooks

func (a Axis) Hooks() []string

Hooks lists which of the four hooks an axis declares, in the fixed order the API reports.

type ComposeSpec

type ComposeSpec struct {
	File         string
	Orchestrator Orchestrator
	Profiles     []string // never nil
	Overlays     []string // never nil
	Subdomains   []SubdomainRoute
}

ComposeSpec is the `compose:` section.

type Depth

type Depth string

Depth is how many labels the wildcard spans.

const (
	DepthOne Depth = "one"
	DepthAny Depth = "any"
)

type Error

type Error struct{ Msg string }

Error is a spec that will not parse, or will not resolve. Always the caller's problem: the API maps it to 400 with the prefix `spec: `, the CLI to exit 3.

func (*Error) Error

func (e *Error) Error() string

type HostValues

type HostValues struct {
	Vars    map[string]string
	Secrets map[string]string
}

HostValues are the control plane's `${vars.*}` / `${secrets.*}`. Nil under the bare CLI.

type Kind

type Kind string

Kind is what a deployment IS: `shared` (a host singleton previews borrow — no axes, guarded down) or `isolated` (one tenant, has axes, leaves nothing behind). Explicit, never inferred.

const (
	Shared   Kind = "shared"
	Isolated Kind = "isolated"
)

type Orchestrator

type Orchestrator string

Orchestrator is how a compose project is driven.

const (
	Compose Orchestrator = "compose"
	Swarm   Orchestrator = "swarm"
)

type Requirement

type Requirement struct {
	Name   string
	Assert string
	Hint   string // "" when absent
}

Requirement is a precondition checked by name before `up`.

type SleepPolicy

type SleepPolicy struct {
	IdleMs  int64
	AfterMs int64
}

SleepPolicy is the `sleep:` section; zero means absent.

type Stack

type Stack struct {
	Version int
	Kind    Kind
	// Stack is the resolved identity, e.g. `pr-123`. Exported to every hook as $STACK.
	Stack    string
	Compose  *ComposeSpec
	Sleep    *SleepPolicy
	Requires []Requirement
	Axes     []Axis
	// Env is the fully-resolved env handed to compose and every hook.
	//
	// DANGER — seeded from the WHOLE ambient environment before the spec's own `env:` is layered
	// on, so it holds every secret this process holds, PSTACK_TOKEN included. Never serialise it,
	// never log it, never return it from a response. Use DeclaredEnv to show a spec's variables.
	Env map[string]string
	// DeclaredEnv is the variable names the SPEC ITSELF declared under `env:`, in order.
	DeclaredEnv []string
	// Warnings are the non-fatal observations `pstack validate` prints.
	Warnings []string
}

Stack is a fully resolved spec.

func Load

func Load(path string, baseEnv map[string]string, host *HostValues) (*Stack, error)

Load parses a spec file from disk.

func Parse

func Parse(source string, baseEnv map[string]string, host *HostValues) (*Stack, error)

Parse parses and fully resolves a spec. baseEnv is the environment to interpolate against (usually the process env plus CLI overrides); host is nil under the bare CLI.

type SubdomainConfig

type SubdomainConfig struct {
	Profile string
	Depth   Depth
	// Host is the explicit host when the spec gave one (a pointer: `host: ""` was given, and the TS
	// `??` kept it), nil to derive `<profile>-<stack>.<domain>`.
	Host *string
}

SubdomainConfig is one `compose.subdomains` entry before the host is resolved.

func ParseSubdomains

func ParseSubdomains(raw any, where string, interp func(v, at string) (string, error)) ([]SubdomainConfig, error)

ParseSubdomains accepts two shapes: a list of profile names (depth one, host derived) or a mapping of profile → depth | { depth?, host? }. Call it only when the key is present: an absent `compose.subdomains` is no subdomains, but a present null is the "must be a list…" error below.

type SubdomainRoute

type SubdomainRoute struct {
	Profile string `json:"profile"`
	Host    string `json:"host"`
	Depth   Depth  `json:"depth"`
	VarName string `json:"varName"`
	Rule    string `json:"rule"`
}

SubdomainRoute is one resolved wildcard.

func ResolveSubdomains

func ResolveSubdomains(configs []SubdomainConfig, stack string, profiles []string, domain string) ([]SubdomainRoute, error)

ResolveSubdomains turns configs into routes. A missing domain is a hard error: a rule anchored on an empty suffix matches nothing, and a router that silently never fires is worse than a refusal.

Jump to

Keyboard shortcuts

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