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 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 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.