command

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2026 License: MIT Imports: 10 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckURLDryRun

func CheckURLDryRun(d *Deps, cmd *cobra.Command) error

CheckURLDryRun rejects --url combined with --dry-run through the standard error envelope (CR-besluit 5/6, C02): a dry-run never performs 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 — GetBool then just reports false and this is a no-op.

func Commands

func Commands() []*cobra.Command

Commands materialises all registered resource commands.

func Handled

func Handled(code int) error

Handled wraps an exit code as an already-rendered error.

func ParseID

func ParseID(s string) (int64, error)

ParseID parses a positional <id> argument.

func PrintURL

func PrintURL(d *Deps, cmd *cobra.Command, path string) bool

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

func RenderError(p *render.Printer, e *client.APIError, invocation string) error

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

func Resolve(cmd *cobra.Command) (*config.Config, auth.Resolved, error)

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

func URLFlag(cmd *cobra.Command)

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 is a conflict — a dry-run performs no write, so there is no resource id to build a URL from. CheckURLDryRun rejects it 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.

func UnknownSub

func UnknownSub(cmd *cobra.Command, args []string) error

UnknownSub is the RunE for parent commands (no own action): show help when called bare, or reject an unknown subcommand with did-you-mean suggestions.

Types

type Deps

type Deps struct {
	Client  *client.Client
	Printer *render.Printer
	BaseURL string
}

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.

func Build

func Build(cmd *cobra.Command) (*Deps, error)

Build constructs the printer + API client. Output mode: --output flag > config.Output > table. Credentials via Resolve (profile/env/keychain).

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

type Registrar func() *cobra.Command

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.

func (Spec) Command

func (s Spec) Command() *cobra.Command

Command builds the resource's cobra command tree.

Jump to

Keyboard shortcuts

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