cli

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2026 License: MIT Imports: 36 Imported by: 0

Documentation

Overview

Package cli implements the brainjar command-line interface on top of cobra. Commands are registered against a single root command; each command resolves `--home`, loads config, and opens a LocalBackend.

Index

Constants

View Source
const (
	ExitOK           = 0
	ExitGeneric      = 1
	ExitBadUsage     = 2
	ExitNotFound     = 3
	ExitConflict     = 4
	ExitUnauthorized = 5
)

Exit codes — stable contract published to scripts and CI.

Variables

This section is empty.

Functions

func Execute

func Execute(ctx context.Context, app *App, args []string) int

Execute runs the CLI with the given args (usually os.Args[1:]) and returns the exit code. Errors are printed to app.IO.Err; success output has already been written by the command that ran.

func ExitCodeFor

func ExitCodeFor(err error) int

ExitCodeFor maps any error returned by a command to the process exit code the CLI should terminate with.

func NewRootCmd

func NewRootCmd(app *App) *cobra.Command

NewRootCmd builds the cobra tree and returns the root command.

func NewUsageError

func NewUsageError(format string, args ...any) error

NewUsageError returns a UsageError with the given message.

func PrintError

func PrintError(w io.Writer, err error, jsonOut bool)

PrintError writes the error to w. In text mode it writes a plain line (plus hint/invalid-refs indented on following lines); in JSON mode it writes a single structured envelope matching apperrors.ToHTTP.

func StdinIsTTY

func StdinIsTTY() bool

StdinIsTTY reports whether the real process stdin is a terminal. CLI commands use this to fail fast when content is expected on a pipe but the user invoked the command interactively without flags.

Types

type App

type App struct {
	IO IO

	// RemoteHTTPClient overrides the default RemoteBackend transport.
	// Set by integration tests so httptest self-signed certs validate;
	// production leaves it nil and NewRemote builds a client using the
	// system trust store.
	RemoteHTTPClient *http.Client
	// contains filtered or unexported fields
}

App holds global CLI state shared across commands.

func (*App) Backend

func (a *App) Backend() (backend.Backend, error)

Backend resolves the active context and returns the matching backend: LocalBackend when the context has no URL, RemoteBackend when it does. The caller is responsible for calling Close() when done.

func (*App) EmitJSON

func (a *App) EmitJSON(v any)

EmitJSON writes v to stdout as a single JSON document. No-op when --json is not set — call EmitText for human output instead.

func (*App) EmitText

func (a *App) EmitText(format string, args ...any)

EmitText writes a human-readable line to stdout. No-op when --json is set — agents consume EmitJSON output instead.

func (*App) JSON

func (a *App) JSON() bool

JSON reports whether machine-readable JSON output was requested.

func (*App) LocalBackend added in v0.3.1

func (a *App) LocalBackend() (*backend.LocalBackend, error)

LocalBackend opens a LocalBackend unconditionally, regardless of the active context's URL. Filesystem-bound commands (init, reset, hooks, sync, serve) call this instead of Backend() because they can only run against the embedded SQLite. Commands that can work against either backend (soul/persona/rule/brain/compose/status/versions, workspace + api-key management, pack, and mcp) use Backend().

func (*App) ReadContent

func (a *App) ReadContent(content, file string) (string, error)

ReadContent returns the content for a `create` command. Precedence: --content > --file > stdin. Errors if both flags are set, or if neither is set and stdin is a TTY.

func (*App) ResolveHome

func (a *App) ResolveHome() (string, error)

ResolveHome returns the resolved home directory, honoring the flag and environment. Safe to call from any command's RunE.

type IO

type IO struct {
	In  io.Reader
	Out io.Writer
	Err io.Writer
}

IO holds the in/out/err streams a command reads and writes. Tests substitute buffers; the root command wires stdin/stdout/stderr.

func DefaultIO

func DefaultIO() IO

DefaultIO returns an IO pointing at the process stdio.

type UsageError

type UsageError struct{ Msg string }

UsageError marks a cobra-level usage problem (unknown flag, missing positional arg, mutually-exclusive flags). It maps to ExitBadUsage.

func (*UsageError) Error

func (e *UsageError) Error() string

Jump to

Keyboard shortcuts

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