Documentation
¶
Overview ¶
Package command is the shared CLI layer: dependency bootstrap (client + printer), the single error-render path, exit-code-carrying handled errors, and a resource registry + verb-builders so adding a resource is a few lines.
Index ¶
- func CheckURLDryRun(d *Deps, cmd *cobra.Command) error
- func Commands() []*cobra.Command
- func Handled(code int) error
- func ParseID(s string) (int64, error)
- func PrintURL(d *Deps, cmd *cobra.Command, path string) bool
- func Register(r Registrar)
- func RenderError(p *render.Printer, e *client.APIError, invocation string) error
- func Resolve(cmd *cobra.Command) (*config.Config, auth.Resolved, error)
- func URLFlag(cmd *cobra.Command)
- func UnknownSub(cmd *cobra.Command, args []string) error
- type Deps
- type HandledError
- type Registrar
- type Spec
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckURLDryRun ¶
CheckURLDryRun rejects --url combined with --dry-run or --preview through the standard error envelope (CR-besluit 5/6, C02): those modes never perform the write, so there is no resource id to build a URL from. Safe to call unconditionally right after Build(), even on commands that never register --dry-run or --preview — GetBool then just reports false.
func PrintURL ¶
PrintURL implements the --url output contract. When --url was requested it prints d.BaseURL+path (the resolved frontend URL, e.g. weburl.Page(id)) to stdout and reports true, so the caller's RunE can return nil immediately with no further output. It reports false, printing nothing, when --url was not set — the caller then falls through to its normal Printer output. d.BaseURL is already guaranteed non-empty here: Build() fails with exit 78 before a Deps is ever returned when no environment is configured (same precedent as `normatik base-url`, auth.go:405-409).
func Register ¶
func Register(r Registrar)
Register adds a resource command builder to the registry.
func RenderError ¶
RenderError is the single error-render route for every command: structured ProblemDetail + hint + synthesized next-command, a malformed-response class, or a transport failure. It returns a Handled error carrying the exit code. invocation is the failed command path (e.g. "normatik pages list"), used to synthesize a runnable "Try:" suggestion — not the base-URL.
func Resolve ¶
Resolve loads config and resolves the environment (base-URL + key) for the given command, honouring the --profile flag, env vars and stored profiles.
func URLFlag ¶
URLFlag registers --url on cmd: instead of the command's normal table/JSON output, print only the resolved frontend URL for the touched resource on stdout — the shape an agent pipes into `open $(...)` or embeds straight into a report (CR-besluit 1). Only commands with a mappable frontend route (see internal/weburl) call this; commands without one simply never register the flag, so cobra's own "unknown flag" rejects it self-documentingly (CR-besluit 2).
Edge-semantics shared by every --url command (CR-besluit 5, C02) live in CheckURLDryRun and PrintURL below — call both from RunE:
- --url wins over -o json: PrintURL always prints the plain URL, never wrapped in JSON, because it bypasses the Printer's JSON/table branch entirely.
- --url survives --quiet: PrintURL never consults Printer.Quiet.
- --url + --dry-run (or --preview) is a conflict — those modes perform no write, so there is no resource id to build a URL from. CheckURLDryRun rejects them via the standard error envelope.
- Writes: perform the write first, then build the URL from the response's id and call PrintURL — never resolve --url before the write happened.
Types ¶
type Deps ¶
Deps holds everything a command needs: an API client, an output printer, and the resolved base-URL (CanonicalSiteURL — no trailing slash, no legacy /api suffix) that --url commands concatenate a weburl path onto.
type HandledError ¶
type HandledError struct{ Code int }
HandledError signals that the error was already rendered; Main only maps it to an exit code (no double printing).
func (*HandledError) Error ¶
func (e *HandledError) Error() string
type Registrar ¶
Registrar builds a resource's top-level command. Resource files register one via Register() (typically in init()), and root iterates Commands(); root.go stays constant regardless of how many resources exist.
type Spec ¶
type Spec struct {
Noun string
Short string
List func(d *Deps, page, size int, sort []string) ([]byte, *client.APIError)
ListFields []string
Get func(d *Deps, id int64, expand []string) ([]byte, *client.APIError)
GetFields []string
Extra []*cobra.Command // hand-written subcommands for non-standard ops
}
Spec declares a standard resource. Only List and Get are generic verb-builders today (they cover the bulk of the read-surface); search/create/update/delete and all non-standard ops (revisions, transitions, restrictions, ...) are added as hand-written subcommands via Extra, still using Build()+RenderError so they share the same deps/error/render flow. F3 may promote a verb to a builder if the pattern repeats often enough to be worth it.