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
- Variables
- func Legacy055(env func(string) string, home string) (string, bool)
- func RefuseLegacy055(stdout, stderr io.Writer, env func(string) string, home string, jsonOut bool) (code int, refused bool)
- func Report(stdout, stderr io.Writer, err error, jsonOut bool) int
- type Error
- type Global
- type Resolved
- type Setting
- type Spec
Constants ¶
const ( WrapWidth = 80 WrapIndent = " " )
WrapWidth is the width of a wrapped error line, and WrapIndent the continuation: it sits under the text after "error: ".
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 ¶
var DetailParam = strings.TrimPrefix(DetailFlag, "--")
DetailParam is the MCP param that asks for the fuller answer: DetailFlag without its dashes.
var ReleaseBase = "https://github.com/bogworks/mash/releases/latest/download"
ReleaseBase is where a release's files are published: the address the 0.5.5 migration hint is written from. The project's own releases are the default; a release build may set it with -ldflags "-X github.com/bogworks/mash/internal/cliframe.ReleaseBase=<base>" (a fork sets its own), and scripts/install.sh takes a fork's from MASH_REPO, which a binary does not see.
Functions ¶
func Legacy055 ¶
Legacy055 is the folder (as the tools name it, the home directory written as ~) when the configuration folder's config.json holds a mash 0.5.5 setting: the key url or tls_ca. Only the names of the keys are looked at, never their values. A folder that does not exist, a config.json that is not there, or one that is not a JSON object is not a 0.5.5 folder (the loader reports what is wrong with those).
func RefuseLegacy055 ¶
func RefuseLegacy055(stdout, stderr io.Writer, env func(string) string, home string, jsonOut bool) (code int, refused bool)
RefuseLegacy055 stops the command when the configuration folder was left by mash 0.5.5: one framed error, exit 1, nothing read from the old keys and nothing changed. refused is false when the folder is not a 0.5.5 folder.
func Report ¶
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 one line, {"error":"MSG","hint":"HINT"} with the hint left out when empty; stderr gets nothing.
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 (*Error) WithWrap ¶
WithWrap returns a copy of e that the text rendering wraps at WrapWidth columns.
func (*Error) WithWrapAt ¶
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 ¶
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 ¶
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 ¶
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 Setting ¶
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.