Documentation
¶
Overview ¶
Package cmdutil holds the helpers backd's command trees share: flag definitions and reads, the wrapper every command runs in, exit codes, the user's terminal and JSON output. It is built on kli and never touches the process environment or standard streams directly, so commands using it run in-process in tests.
Index ¶
- Constants
- func Act(name string, fn kli.CommandFunc) kli.CommandFunc
- func Boolean(cc *kli.CommandConfig, key, description string)
- func ConfigDir(c *kli.CommandContext) (string, error)
- func Execute(cfg *kli.Config, prog string, args []string, stderr io.Writer) int
- func Failed(err error) error
- func Flag(c *kli.CommandContext, key string) bool
- func GroupedHelp(prog string, groups []HelpGroup, summaries map[string]string, footer string) string
- func IsGlobalHelp(args []string) bool
- func IsTerminal(r any) bool
- func Num(c *kli.CommandContext, key string) int64
- func Optional(cc *kli.CommandConfig, key, description string)
- func PrintJSON(w io.Writer, v any) error
- func PrintJSONLines[T any](w io.Writer, items []T) error
- func ReadKeySource(spec string, getenv func(string) string, warn io.Writer) (string, error)
- func ReadLine(uio IO, prompt string) (string, error)
- func ReadSecret(uio IO, confirm bool, prompt string) (string, error)
- func ReadSecretValue(uio IO, prompt string) (string, error)
- func Required(cc *kli.CommandConfig, key, description string)
- func SinceParam(v string, now time.Time) (string, error)
- func Str(c *kli.CommandContext, key string) string
- func Summaries(cfg *kli.Config, prog string) map[string]string
- func UsageErr(err error) error
- type HelpGroup
- type IO
Constants ¶
const ( ExitFailure = 1 ExitUsage = kli.ExitUsage )
Exit codes: 0 for success, ExitFailure for a command that ran and failed, ExitUsage for one invoked wrongly.
Variables ¶
This section is empty.
Functions ¶
func Act ¶
func Act(name string, fn kli.CommandFunc) kli.CommandFunc
Act wraps a command named name (the whole command line, program included: "backd template realm"): stray arguments are a usage error, and failures are prefixed with the name.
func Boolean ¶
func Boolean(cc *kli.CommandConfig, key, description string)
func ConfigDir ¶
func ConfigDir(c *kli.CommandContext) (string, error)
ConfigDir is CONFIG_DIR, required by commands that read the config.
func Execute ¶
Execute runs args (without the program name) on cfg and returns the exit code: 0, ExitFailure, ExitUsage, or what the command chose. An error not yet reported is printed to stderr, prefixed with prog.
func GroupedHelp ¶
func GroupedHelp(prog string, groups []HelpGroup, summaries map[string]string, footer string) string
GroupedHelp renders the global help of prog: the groups in order, then the footer.
func IsGlobalHelp ¶
IsGlobalHelp reports whether the arguments ask for the global help: none at all (a bare binary starts nothing), or `help` / `--help` / `-h` alone.
func Optional ¶
func Optional(cc *kli.CommandConfig, key, description string)
func PrintJSONLines ¶
PrintJSONLines writes each item to w as a line of JSON.
func ReadKeySource ¶
ReadKeySource reads a key the operator names as key:<value>, env:<VAR> or file:<path>, the same way everywhere a key is given on the command line. A key given as a value ends up in shell history and process lists, which the docs warn about; a file wider than owner-only draws a warning on warn.
func ReadLine ¶
ReadLine reads one line the user types (a code): from the terminal with a prompt, or the first line of standard input when it isn't one.
func ReadSecret ¶
ReadSecret reads a password from the terminal without echo, twice when confirm is set; otherwise it reads the first line of stdin.
func ReadSecretValue ¶
ReadSecretValue reads the value of a secret: a hidden prompt on a terminal, and all of standard input otherwise, so a file (a service account's JSON, a .p8 key) can be piped in. Only the line ending at the very end is dropped.
func Required ¶
func Required(cc *kli.CommandConfig, key, description string)
Required, Optional and Boolean define a command's flags.
func SinceParam ¶
SinceParam turns --since (an RFC 3339 time, or a duration back from now such as 24h or 7d) into the time the API takes.
func Str ¶
func Str(c *kli.CommandContext, key string) string
Str, Flag and Num read a command's flags.