cmdutil

package
v1.135.0 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package cmdutil provides CLI command utilities including JSON output helpers.

Index

Constants

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

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

func ApplyNoColorFlag(cmd *cobra.Command, ios *iostreams.IOStreams)

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

func EnablePagerForAnnotated(root *cobra.Command, ios *iostreams.IOStreams)

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

func JSONFieldsFromCmd(cmd *cobra.Command) []string

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

func PartialWarn(w io.Writer, n int, listErr error)

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

func RegisterNoColorFlag(root *cobra.Command)

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

func ValidateOutputFlags(c *cobra.Command, ios *iostreams.IOStreams) error

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

func ValidatePositiveLimit(limit int) error

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 BrowserLauncher interface {
	Browse(url string) error
}

type EditorLauncher

type EditorLauncher interface {
	Edit(filename string) error
}

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.

Jump to

Keyboard shortcuts

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