cliframe

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 9 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 cliframe is the frame shared by the mash and mashctl command lines: the global options, accepted anywhere in the arguments, and the error report.

The global options are --endpoint ADDR, --creds PATH, --server URL (repeatable), --ca PATH, --config-dir PATH, --detail (the name is DetailFlag), --json and --no-color. Each takes the form --flag value or --flag=value, before or after the verb. Extract strips them from the arguments and Global.Resolve applies the precedence flag, then environment, then config.json.

Exit codes are only 0 (success), 1 (failure) and 2 (usage error). Report maps every error onto one of them.

Index

Constants

View Source
const (
	WrapWidth  = 80
	WrapIndent = "       "
)

WrapWidth is the width of a wrapped error line, and WrapIndent the continuation: it sits under the text after "error: ".

View Source
const DetailFlag = "--detail"

DetailFlag is the global option that gives the fuller answer, on every command of both tools: in text, 'label: value' lines after the usual line; with --json, a fuller object that contains the usual one; an MCP tool takes the same word, without the dashes, as a boolean param (DetailParam). It is the one place the name is written.

Variables

View Source
var DetailParam = strings.TrimPrefix(DetailFlag, "--")

DetailParam is the MCP param that asks for the fuller answer: DetailFlag without its dashes.

View Source
var IsTerminal = func(w io.Writer) bool {
	f, ok := w.(*os.File)
	return ok && term.IsTerminal(int(f.Fd()))
}

IsTerminal says whether w is a terminal. WriteJSON asks it to choose between the indented and the compact form; a test replaces it to force either.

Functions

func Block added in v0.5.10

func Block(rows ...Row) string

Block renders rows as "label: value" lines, the labels padded to the longest label in the block (colon included) plus one space, so every value starts in one column and none overflows it. Lines end with no newline.

func JSONBytes added in v0.5.10

func JSONBytes(w io.Writer, v any) ([]byte, error)

JSONBytes is v as --json prints it, without the final newline: indented by two spaces when w is a terminal and one compact line when w is a pipe or a file (the rule "--json prints one object"). < > & are kept readable.

func Report

func Report(stdout, stderr io.Writer, err error, jsonOut bool) int

Report writes err to the user and returns the process exit code. A nil err writes nothing and returns 0. An *Error (found with errors.As) keeps its code when that is 1 or 2, otherwise the code is 1; any other error is a failure with code 1 and its text as the message.

In text mode stderr gets "error: MSG" and, when there is a hint, "hint: HINT"; stdout gets nothing. In JSON mode stdout gets the object {"error":"MSG","hint":"HINT"} with the hint left out when empty, indented when stdout is a terminal and one compact line otherwise (WriteJSON); stderr gets nothing.

func WriteJSON added in v0.5.10

func WriteJSON(w io.Writer, v any) error

WriteJSON prints v and a newline to w as JSONBytes says. It is the one place both tools write a --json object, a success object and an error object alike.

Types

type Error

type Error struct {
	Msg  string // what went wrong, without a leading "error:"
	Hint string // an optional next step
	Code int    // 1 failure, 2 usage
	Wrap bool   // the text rendering wraps a long message at WrapWidth columns (WrapAt when set); the JSON message stays one line
	// WrapAt is the width of a wrapped line for this error; 0 means WrapWidth.
	WrapAt int
}

Error is a command failure that carries what the user sees and the process exit code. Code is 1 for a failure and 2 for a usage error.

func Failf

func Failf(format string, a ...any) *Error

Failf returns a failure (exit code 1).

func Usagef

func Usagef(format string, a ...any) *Error

Usagef returns a usage error (exit code 2).

func (*Error) Error

func (e *Error) Error() string

Error returns the message.

func (*Error) WithHint

func (e *Error) WithHint(h string) *Error

WithHint returns a copy of e with the hint set.

func (*Error) WithWrap

func (e *Error) WithWrap() *Error

WithWrap returns a copy of e that the text rendering wraps at WrapWidth columns.

func (*Error) WithWrapAt

func (e *Error) WithWrapAt(n int) *Error

WithWrapAt returns a copy of e that the text rendering wraps at n columns instead of WrapWidth.

type Global

type Global struct {
	Endpoint, Creds, CA, ConfigDir string
	Servers                        []string
	Detail, JSON, NoColor          bool
	Extra                          map[string]string // by Setting.Flag, values given by flag
	Set                            map[string]bool   // flags ("--endpoint", ...) that were given; DetailFlag is the one that asks for the fuller answer
}

Global holds the global options found on the command line.

func Extract

func Extract(spec Spec, args []string) (Global, []string, error)

Extract removes the global options from args wherever they appear and returns them with the remaining arguments in their original order. A bare "--" ends the scan: it and everything after it are returned untouched. A value-taking flag without a value, or whose value looks like a flag, is a usage error (*Error, code 2). The last occurrence wins for single-valued flags; --server accumulates and takes a comma list.

func ExtractOwning

func ExtractOwning(spec Spec, args []string, own map[string][]string) (Global, []string, error)

ExtractOwning is Extract, except that a verb listed in own keeps the global flags that the verb itself parses: own maps a verb to those flags (for example "--json", or "--server" for mashctl adopt). Such a flag after the verb is left in the arguments exactly as written, in its place, with its value, and does not count as a global option; the verb sees its arguments as it did before the global options existed. The verb is the first argument that remains once the global options are removed. A "--json" written before the verb is a global option as usual, and is put after the verb for a verb that keeps it. For any other verb it is Extract.

func (Global) Resolve

func (g Global) Resolve(spec Spec, env func(string) string, file map[string]any) Resolved

Resolve applies the precedence flag, then environment, then config.json to every value-taking setting. env looks up an environment variable (nil means none are set); file is the decoded config.json (nil means none). An empty environment value or an empty config.json value counts as unset. NoColor is also true when NO_COLOR is non-empty.

func (Global) Shadow

func (g Global) Shadow(spec Spec, env func(string) string, lookup config.Lookup) (func(string) string, config.Lookup)

Shadow returns env and lookup wrapped so that each global flag given on the command line answers for the environment variable it overrides: --endpoint for MASH_ENDPOINT, --creds for spec.CredsEnv, --server (the values joined with commas) for MASH_SERVER, --ca for MASH_CA, --config-dir for MASH_CONFIG_DIR, and each extra setting's flag for its Env. A flag beats the environment variable. The wrapped lookup also answers config.FlagSourcePrefix+VAR with the flag's name, so config.Load can record "flag:--name" as the source.

type Resolved

type Resolved struct {
	Endpoint, Creds, CA, ConfigDir string
	Servers                        []string
	Detail, JSON, NoColor          bool
	Extra                          map[string]string // by Setting.Flag
	// Source says where each setting came from, keyed by flag ("--endpoint", "--server", an extra's flag): "flag", the
	// environment variable name, "config.json", or "unset".
	Source map[string]string
}

Resolved is the effective value of each setting after applying flag, environment and config.json.

type Row added in v0.5.10

type Row struct{ Label, Value string }

Row is one line of a label block: a label without its colon, and its value.

type Setting

type Setting struct {
	Flag string // like "--system-creds"
	Env  string
	Key  string
}

Setting is one extra value-taking setting: its flag, environment variable and config.json key.

type Spec

type Spec struct {
	Tool     string    // "mash" or "mashctl"
	CredsEnv string    // environment variable for --creds (mash MASH_CREDS, mashctl MASH_ADMIN_CREDS)
	CredsKey string    // config.json key for --creds (mash "creds", mashctl "admin_creds")
	Extra    []Setting // extra value-taking settings of this tool
}

Spec is what one tool adds to the shared frame.

Jump to

Keyboard shortcuts

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