cli

package
v3.49.1 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: MIT Imports: 46 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 cli adapts typed daemon contracts and local workflows into Canary commands, machine-readable output, and terminal rendering. Broker-connected for broker state, policy decisions, and gated broker writes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DetectWriteOrigin

func DetectWriteOrigin(stdin io.Reader) string

DetectWriteOrigin classifies this process for broker-write authorization. Any agent marker or a non-TTY stdin classifies as agent; nothing can force a human classification. The daemon treats unknown origins as agent for audit and any origin-specific policy, while broker-write readiness still comes from trading mode, pins, preview tokens, freeze state, and broker checks.

func HelpInvocation

func HelpInvocation(cmd string, rest []string) bool

HelpInvocation reports whether rest asks cmd for help in a form its handler answers without the daemon: a help flag anywhere, `help` alone, or `help ACTION` where the usage offers it (`canary policy help override`). cmd/canary checks it before dialling, so asking for help never starts the daemon.

func IsKnown

func IsKnown(name string) bool

IsKnown reports whether name is a registered subcommand. Used by cmd/canary to skip the daemon autospawn for typos and unknown commands — otherwise `canary nonsense` would spawn the daemon just to fail with "unknown subcommand", which is wasteful and confusing if it tips a dormant install into a long startup.

func PolicyLocalSubcommand

func PolicyLocalSubcommand(args []string) bool

PolicyLocalSubcommand reports whether args name a policy subcommand that runs without the daemon: default, ensure, and check --offline.

func PrintUsage

func PrintUsage(w io.Writer)

PrintUsage writes the top-level help text. Commands are listed under their catalog group, in registry order inside each one, so `status` stays the first line. The listing shows the catalog's short form and the full summary stays in `canary <subcommand> --help` — a flat list of 36 long summaries wraps into a wall on an 80-column terminal.

func PushDeliveryLine

func PushDeliveryLine(p rpc.PushDeliveryProof, now time.Time) (string, bool)

PushDeliveryLine renders the one-line phone push verdict shared by `canary status` and `canary app status`: how long alert pushes have been silent, whether a device ever witnessed a push, and what blocks delivery. ok is true only when a paired device has reported a push displayed or opened and nothing currently blocks delivery. Push-service acceptance is never a witness.

func Run

func Run(ctx context.Context, env *Env, cmd string, args []string) int

Run dispatches the subcommand named by cmd. Returns the process exit code. flag package stops at the first non-flag token, but users naturally write

func RunPolicyLocal

func RunPolicyLocal(ctx context.Context, env *Env, args []string) int

RunPolicyLocal runs the policy subcommands that need no daemon: default (print a template), ensure (write missing files, review/apply conversions) and check --offline (the file-only plausibility read). The install path runs `canary policy ensure` before any daemon exists.

func RunRestart

func RunRestart(ctx context.Context, args []string, stdout, stderr io.Writer) int

RunRestart is the top-level `canary restart` entrypoint. It intentionally does not take an Env: restart is local process management and must run before the normal autospawn+dial path in cmd/canary/main.go.

func RunStop

func RunStop(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) int

RunStop is the top-level `canary stop` entrypoint. Like RunRestart it takes no Env: stopping is local process management and must run before the autospawn path in cmd/canary/main.go, which would otherwise start the very daemon this command exists to stop.

func RunUpdate

func RunUpdate(ctx context.Context, args []string, version string, stdin io.Reader, stdout, stderr io.Writer) int

RunUpdate is the entrypoint cmd/canary/main.go dispatches to. It does not match the CommandFunc signature because update has no Env (no daemon connection) — `update` is registered in cli.commands with Fn=nil and the binary's main.go calls this function directly, the same pattern `setup` uses.

args are the raw CLI args after `canary update`. version is the installed binary's version string (cmd/canary stamps it at build). stdin / stdout / stderr are the process I/O streams.

Returns the process exit code.

func ShouldColor

func ShouldColor(w io.Writer) bool

ShouldColor reports whether ANSI color escapes should be emitted to w. Policy, in order:

  1. CANARY_COLOR=always → on (overrides TTY check)
  2. CANARY_COLOR=never → off
  3. NO_COLOR set (any) → off (https://no-color.org)
  4. w is an interactive terminal → on
  5. otherwise → off (pipes, file redirects, bytes.Buffer in tests)

Computed once per process and cached on Env.Color so colored renderers don't re-syscall on every value.

Types

type Command

type Command struct {
	Name    string
	Summary string
	Usage   string // optional one-line usage example shown in `canary X --help`
	Fn      CommandFunc
}

Command bundles a subcommand's name, one-line summary, optional usage

func Commands

func Commands() []Command

Commands returns the registered subcommand entries in declaration order.

type CommandFunc

type CommandFunc func(ctx context.Context, env *Env, args []string) int

CommandFunc is the signature implemented by every subcommand handler.

type CommandSpec

type CommandSpec struct {
	Name    string
	Summary string
	// Brief is the listing line for commands whose Summary is too long for
	// a terminal row. `canary <command> --help` and the generated CLI
	// reference always render Summary, so the detail is moved, not lost.
	Brief       string
	Usage       string
	Flags       []FlagSpec
	Subcommands []SubcommandSpec
	Guard       GuardClass
	Group       HelpGroup
}

CommandSpec is the user-facing command catalog. Name/Summary/Usage are copied from Commands() at runtime so the help table and catalog cannot silently drift.

func Catalog

func Catalog() []CommandSpec

Catalog returns the registered commands with shared metadata for the help listing, generated reference, guard descriptions, and flag-value handling.

type DaemonConn

type DaemonConn interface {
	Call(context.Context, string, any, any) error
	Stream(context.Context, string, any, func(json.RawMessage) error) error
}

DaemonConn is the CLI's typed daemon-call surface. *dial.Conn implements

type Env

type Env struct {
	// HelpOnly parses offline; it must never execute a normal command.
	HelpOnly bool
	Stdout   io.Writer
	Stderr   io.Writer
	// Stdin is the interactive input used for live-write confirmation
	Stdin io.Reader
	Conn  DaemonConn
	// Origin is this process's broker-write origin classification
	// Empty classifies as agent at the daemon (fail closed).
	Origin string
	// Version is the running CLI version stamped by cmd/canary. Empty in
	// renderer tests and local-only helper paths that do not need parity
	// checks against the daemon.
	Version string
	// Color is true when ANSI color escapes should be emitted on Stdout.
	Color bool
}

Env is the per-invocation context shared by every subcommand.

type FlagSpec

type FlagSpec struct {
	Name       string
	TakesValue bool
	Values     []string
	Summary    string
}

FlagSpec is the shared flag metadata used by command-line flag hoisting and generated reference documentation. Values is intentionally small and enum-like.

type GuardClass

type GuardClass string

GuardClass describes whether a command is read-only, changes local state, or needs a human confirmation. It is metadata only; existing CLI gates still enforce the real safety policy.

const (
	GuardReadOnly GuardClass = "read-only"
	GuardLocal    GuardClass = "local"
	GuardConfirm  GuardClass = "confirm"
)

Guard classifications used by the command catalog.

type HelpGroup

type HelpGroup string

HelpGroup buckets commands in the top-level help listing. It is a reading aid for the command registry and carries no policy; guard classes remain the only statement about what a command may do.

const (
	GroupDesk    HelpGroup = "desk"
	GroupMarkets HelpGroup = "markets"
	GroupSystem  HelpGroup = "system"
)

Help groups used by the command catalog.

type HelpGroupSpec

type HelpGroupSpec struct {
	Group   HelpGroup
	Title   string
	Tagline string
}

HelpGroupSpec is one heading in the top-level help listing.

func HelpGroups

func HelpGroups() []HelpGroupSpec

HelpGroups returns the listing groups in render order. Commands keep registry order inside their group, so `status` stays the first line of the first group.

type SubcommandSpec

type SubcommandSpec struct {
	Name  string
	Guard GuardClass
}

SubcommandSpec captures nested command words that are useful for completion and guard classification. The existing handlers remain authoritative for parsing and validation.

Jump to

Keyboard shortcuts

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