Documentation
¶
Overview ¶
`pstack api …` — every HTTP route as a command, generated from `packages/pstack/api/openapi.yaml`.
── WHY THIS IS THE ONE COBRA SUBTREE IN A HAND-ROLLED CLI ───────────────────────────────────────
The rest of `pstack` is parsed by the positional walker in args.go, deliberately: flags may appear anywhere, `--ui` peeks ahead, and `cli-goldens.test.ts` pins the usage text byte for byte. Cobra cannot reproduce any of that, so it never owns the root. It owns exactly the `api` subtree, which has none of those constraints because nobody has typed it before.
The split is enforced by INTERCEPTION, not by cooperation: `run` hands the whole tail to this function before `ParseArgs` sees a single token. Otherwise `pstack api deployments up --id pr-1` would be parsed as the verb `api` with three unknown arguments, and the walker would refuse it with a message about a spec file.
── THE COMMANDS ARE NOT WRITTEN HERE, AND THAT IS THE POINT ─────────────────────────────────────
`zz_generated.go` is produced by oascmd-gen from the OpenAPI document and carries a lock file, so a route that changes shape either regenerates cleanly or is reported as a BREAKING change to a command somebody may have scripted. Hand-writing sixty-nine subcommands would reproduce every path, method, parameter and enum a second time and let them drift — which is the same argument docs/README.md makes about route tables in prose.
── ADDRESSING AND CREDENTIALS ARE THE ONES THAT ALREADY EXISTED ─────────────────────────────────
`PSTACK_API_URL` and `PSTACK_TOKEN`, resolved by the same `apiBase` that `pull config` and `push config` use — including its refusals: no default URL (a guess talks to the wrong host) and no plain http off a private address. Viper would be a third configuration mechanism beside the `Tuning` env reader and that pair, for values that are already one lookup each.
The token is checked HERE rather than inside apiBase's config-specific sentence, because most of these routes are not root-only — a personal token at the right role is the normal credential, and telling somebody they need the root token to list deployments would be false.
Package cli is argv, dispatch and exit codes. Logic belongs in the modules, not here.
The parser is hand-rolled and positional: flags may appear ANYWHERE, including after the command word, `--ui` peeks ahead (a switch for build-image, a value for init), and unrecognised positional words accumulate into `cmd` and `sub`. Go's `flag` stops at the first non-flag argument and cobra would own the usage text — both would change observable behaviour that the CLI goldens pin.
Index ¶
Constants ¶
const ( ExitOK = 0 ExitFailed = 1 ExitLeaked = 2 ExitUsage = 3 )
Exit codes. The distinct code for leaks lets CI treat "torn down but leaked" differently from "teardown errored", which are different problems with different owners.
Variables ¶
var Commands = append(append([]string{}, SpecCommands...), "init", "serve", "build-image", "cloud-init", "dockerfile", "upgrade", "ui", "swarm", "pull", "push", "healthcheck", "api")
Commands in usage order.
var SpecCommands = []string{"up", "down", "verify", "status", "validate"}
SpecCommands read a spec; Commands is every command that exists. An unknown command must fail as unknown, never by hunting for a spec file — that bug shipped once.
Functions ¶
func IsSpecCommand ¶
IsSpecCommand reports whether name reads a spec.
Types ¶
type Exit ¶
type Exit struct {
Code int
// Msg is printed to stderr prefixed with `pstack: ` when non-empty.
Msg string
}
Exit is how a command ends: a code and an optional stderr line (`pstack: <msg>`).
func Healthcheck ¶
Healthcheck is the container's HEALTHCHECK: one GET against this process's own /api/health, exit 0 or 1. `init` blocks on this verdict, so it must be boring.
func Serve ¶
func Serve(o ServeOptions) *Exit
Serve is `pstack serve`. Blocks until SIGINT/SIGTERM, then stops the server.
func UnknownCommand ¶
UnknownCommand is the refusal for a command this build does not have.
type IO ¶
type IO struct {
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
Env func(string) (string, bool)
// Environ is the whole environment as a map (what hooks inherit); nil means os.Environ().
Environ map[string]string
// UIHTML and ShareHTML are the embedded pages `serve` hosts.
UIHTML string
// OpenAPISpec is the embedded OpenAPI document `serve` publishes.
OpenAPISpec []byte
}
IO is the process's streams and environment, so a test can drive Run without a process.
type Parsed ¶
type Parsed struct {
Cmd string
Sub string
File string
DryRun bool
Level exec.Level
Overrides map[string]string
OverrideKeys []string
NoVerify bool
Force bool
Domain string
AcmeEmail string
DNSProvider string
Challenge string // http01 | dns01
Orchestrator string // swarm | compose
Distro string
Format string
Tag string
UI string // basic | advanced
UIImage bool
UIDist string
SSHKey string
Password string
// AdminUser / AdminPassword / APIToken are cloud-init's credential flags. Deliberately WITHOUT
// environment defaults, unlike --password (PSTACK_DASHBOARD_PASSWORD) beside them: an operator's
// shell has PSTACK_TOKEN set precisely because it is talking to a host that already exists, and a
// default would silently clone that host's bearer token into a new host's user-data. The pair
// follows the token because they are the same decision — the credentials of one specific host.
AdminUser string
AdminPassword string
APIToken string
ConfigRepo string
// Config / ConfigURL are cloud-init's two ways to carry a `pull config` export onto a new host:
// the sealed file itself, or a URL the host fetches at boot. Mutually exclusive, and both need
// PSTACK_CONFIG_KEY — see cloudInit().
//
// The PASSPHRASE is deliberately not here and has no flag anywhere: argv is world-readable
// through `ps`, so it comes from PSTACK_CONFIG_KEY or a no-echo prompt. Keeping it out of this
// struct also keeps it out of the `t.Errorf("got %+v", p)` that every parser test does.
Config string
ConfigURL string
Out string
// In is `-i` — the file `push config` reads. The counterpart of Out.
In string
Yes bool
To string
Resume bool
// Typed records which value flags were spelled on the command line, for the two commands that
// must only act on an EXPLICIT --ui / --orchestrator (upgrade, ui).
Typed map[string]bool
// Help and Version short-circuit dispatch.
Help bool
Version bool
}
Parsed is argv, resolved against the environment defaults.
type ServeOptions ¶
type ServeOptions struct {
UIHTML string
// OpenAPISpec is the embedded OpenAPI document, served at /api/openapi.{yaml,json}.
OpenAPISpec []byte
Stdout io.Writer
Stderr io.Writer
// Env is the process environment (os.LookupEnv-shaped); nil means the real one.
Env func(string) (string, bool)
}
ServeOptions are what the binary hands `serve`: the two embedded pages and the streams.