cmdutil

package
v0.9.13 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 13 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 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

View Source
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

func Execute(cfg *kli.Config, prog string, args []string, stderr io.Writer) int

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 Failed

func Failed(err error) error

Failed marks err as a failure (ExitFailure) that Act passes on unchanged.

func Flag

func Flag(c *kli.CommandContext, key string) bool

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

func IsGlobalHelp(args []string) bool

IsGlobalHelp reports whether the arguments ask for the global help: none at all (a bare binary starts nothing), or `help` / `--help` / `-h` alone.

func IsTerminal

func IsTerminal(r any) bool

IsTerminal tells whether r is a terminal.

func Num

func Num(c *kli.CommandContext, key string) int64

func Optional

func Optional(cc *kli.CommandConfig, key, description string)

func PrintJSON

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

PrintJSON writes v to w as one line of JSON.

func PrintJSONLines

func PrintJSONLines[T any](w io.Writer, items []T) error

PrintJSONLines writes each item to w as a line of JSON.

func ReadKeySource

func ReadKeySource(spec string, getenv func(string) string, warn io.Writer) (string, error)

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

func ReadLine(uio IO, prompt string) (string, error)

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

func ReadSecret(uio IO, confirm bool, prompt string) (string, error)

ReadSecret reads a password from the terminal without echo, twice when confirm is set; otherwise it reads the first line of stdin.

func ReadSecretValue

func ReadSecretValue(uio IO, prompt string) (string, error)

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

func SinceParam(v string, now time.Time) (string, error)

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.

func Summaries

func Summaries(cfg *kli.Config, prog string) map[string]string

Summaries reads the one-line description of every command from the tree's own flat help, so the grouped help cannot say something else than `<prog> <command> --help`. The tree doesn't register help itself, so it is added.

func UsageErr

func UsageErr(err error) error

UsageErr marks err as a usage error (ExitUsage): the command was invoked wrongly, not one that ran and failed.

Types

type HelpGroup

type HelpGroup struct {
	Title, Note string
	Commands    []string
}

HelpGroup is a section of a binary's global help: the commands that belong together and a note on what they have in common.

type IO

type IO struct {
	Stdin          io.Reader
	Stdout, Stderr io.Writer
}

IO is the user's side of a command: its standard streams.

func IOOf

func IOOf(c *kli.CommandContext) IO

IOOf is the IO of a running command.

Jump to

Keyboard shortcuts

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