Documentation
¶
Overview ¶
Package cmdutil provides CLI command utilities including JSON output helpers.
Index ¶
- Constants
- func ApplyNoColorFlag(cmd *cobra.Command, ios *iostreams.IOStreams)
- func EnablePagerForAnnotated(root *cobra.Command, ios *iostreams.IOStreams)
- func FatalIfError(err error)
- func JSONFieldsFromCmd(cmd *cobra.Command) []string
- func PartialWarn(w io.Writer, n int, listErr error)
- func RegisterNoColorFlag(root *cobra.Command)
- func ValidateOutputFlags(c *cobra.Command, ios *iostreams.IOStreams) error
- func ValidatePositiveLimit(limit int) error
- type BrowserLauncher
- type EditorLauncher
- type SystemBrowser
- type SystemEditor
Constants ¶
const NoColorFlag = "no-color"
NoColorFlag is the persistent flag name that disables color output. Defined here so root registration and any consumer (e.g. PreRunE wiring, integration tests) reference the same string.
const PagerAnnotation = "pager"
PagerAnnotation is the cobra annotation key that marks a command as long-output and pager-eligible. Commands set
Annotations: map[string]string{cmdutil.PagerAnnotation: "true"}
instead of calling StartPager/StopPager inline. This keeps pager lifecycle out of command bodies — adding pager support to a new command is one annotation, not a 5-line copy-paste with an easily- missed defer.
Variables ¶
This section is empty.
Functions ¶
func ApplyNoColorFlag ¶ added in v1.17.0
ApplyNoColorFlag reads --no-color off the command's flag set and, if set, flips the IOStreams color decision off. Designed for use inside PersistentPreRunE so a single registration applies across every subcommand.
Cobra walks PersistentFlags up the parent chain when resolving a flag, so passing --no-color on a leaf command (e.g. `bitbottle pr list --no-color`) still surfaces here. We deliberately do NOT call SetColorEnabled(true) when the flag is unset — leaving IOStreams as constructed lets NO_COLOR env and TTY detection keep their say.
func EnablePagerForAnnotated ¶ added in v1.12.0
EnablePagerForAnnotated walks the command tree rooted at root and wraps every annotated command's RunE so the pager is started before the command runs and stopped after. The pager is a no-op outside a TTY, so unmarked commands and piped invocations are unaffected.
Walking happens at registration time (call once after building the tree). The wrap is invisible to the command body — a command can still call f.IOStreams.Out directly, and gets the pager pipe for free.
func FatalIfError ¶
func FatalIfError(err error)
func JSONFieldsFromCmd ¶ added in v1.86.0
JSONFieldsFromCmd returns the parsed JSON field list from the --json flag. Returns nil when --json was not passed or passed without a field list (= all fields). Returns a non-nil slice when --json field1,field2 was used.
func PartialWarn ¶ added in v1.73.0
PartialWarn writes a warning to w when items were collected but pagination ended with an error. Call it after rendering and before returning listErr. It is a no-op when listErr is nil or n == 0.
func RegisterNoColorFlag ¶ added in v1.17.0
RegisterNoColorFlag adds the persistent --no-color flag to root. The default is false so the flag's presence alone is the disable signal.
func ValidateOutputFlags ¶ added in v1.127.3
ValidateOutputFlags enforces the output-format contract shared by every command: exactly one of --json / --yaml / --template may be active, and --jq is only meaningful alongside --json. It also disables color on the IOStreams when any structured mode is selected so consumers get raw values.
This logic lives here (not inline in the root command's PersistentPreRunE) because cobra runs only the single deepest PersistentPreRunE in a command chain. Command groups that install their own PersistentPreRunE (e.g. via factory.EnableRepoOverride for the -R flag) would otherwise shadow the root's hook and silently skip these checks — the FMT-CONTRACT bug where `pr view --jq .x` was accepted without --json while `status --jq .x` correctly errored. Both call sites now invoke this one function.
The flag lookups tolerate missing flags (returning zero values) so callers that construct a subcommand in isolation — without the root's persistent flags — do not panic.
func ValidatePositiveLimit ¶ added in v1.71.1
ValidatePositiveLimit returns an error if limit is less than 1. Use for --limit flags where 0 is not a documented "no-cap" value. For commands that explicitly document 0 as "no limit", skip this check.
Types ¶
type BrowserLauncher ¶
type EditorLauncher ¶
type SystemBrowser ¶
type SystemBrowser struct{}
func (*SystemBrowser) Browse ¶
func (b *SystemBrowser) Browse(url string) error
Browse opens the given URL in the system browser.
type SystemEditor ¶
type SystemEditor struct{}
func (*SystemEditor) Edit ¶
func (e *SystemEditor) Edit(filename string) error
Edit opens the given filename in $EDITOR.