Documentation
¶
Overview ¶
Package cliutil provides CLI utility functions and formatters.
This package contains helpers for command-line output formatting, including colored output, table formatting, and progress indicators.
Features ¶
- Colored output (success, warning, error)
- Table formatting for bulk operations
- Progress spinners and bars
- JSON/YAML output formatting
Index ¶
- Constants
- Variables
- func ColorsEnabled() bool
- func DisableColors()
- func EnableColors()
- func ExitCodeForError(err error) int
- func ExitCodesBulkHelp() string
- func ExitCodesConflictHelp() string
- func IsMachineFormat(format string) bool
- func IsTerminal(fd uintptr) bool
- func NewExitError(code int, err error) error
- func QuickStartHelp(content string) string
- func StripIndent(s string) string
- func ValidateFormat(format string, allowed []string) error
- func WriteJSON(w io.Writer, v any, verbose bool) error
- func WriteLLM(w io.Writer, v any) error
- type ExitError
Constants ¶
const ( ExitOK = 0 ExitToolError = 1 ExitPartialFailed = 2 ExitReclaimIncomplete = 3 )
Exit code contract shared by gz-git commands.
One-shot bulk commands (clone, update, pull, fetch, push, status, commit, switch, stash, tag, clean, diff):
0 all repositories succeeded 1 tool or configuration error (bad flag, scan failure) 2 completed, but one or more repositories failed
Reclaim after a successful integrate:
3 integrate succeeded, reclaim did not finish
Diagnostic commands follow the grep convention instead (e.g. `conflict detect`): 0 = nothing found, 1 = findings, 2 = execution error.
Variables ¶
var ( ColorCyanBold = ansiCyanBold ColorGreenBold = ansiGreenBold ColorYellowBold = ansiYellowBold ColorMagentaBold = ansiMagentaBold ColorCyan = ansiCyan ColorGreen = ansiGreen ColorYellow = ansiYellow ColorMagenta = ansiMagenta ColorRed = ansiRed ColorGray = ansiGray ColorReset = ansiReset )
Exported color codes. They are vars (not consts) so the color gate can blank them when output is not a terminal, which lets every existing `cliutil.Color*` reference become a no-op without touching the call sites. Blanking happens in this package's init(), which runs before the cmd package builds its help strings — so help text assembled from these vars is already colorless in a non-terminal environment.
var CoreFormats = []string{"default", "compact", "json", "llm"}
CoreFormats contains the default formats supported by all commands.
var TabularFormats = []string{"default", "compact", "json", "llm", "table", "csv", "markdown"}
TabularFormats contains formats meant for tabular data output.
Functions ¶
func ColorsEnabled ¶
func ColorsEnabled() bool
ColorsEnabled reports whether ANSI color should be emitted, based on the environment and whether stdout is a terminal.
func DisableColors ¶
func DisableColors()
DisableColors blanks every exported color code. Idempotent.
func EnableColors ¶
func EnableColors()
EnableColors restores every exported color code to its ANSI value. It exists mainly so tests can force colors on regardless of the test environment.
func ExitCodeForError ¶
ExitCodeForError maps a command error to a process exit code: nil → 0, an *ExitError (anywhere in the chain) → its Code, any other error → 1.
func ExitCodesBulkHelp ¶
func ExitCodesBulkHelp() string
ExitCodesBulkHelp returns the standardized "Exit Codes" help section for bulk commands. It is appended to a command's Long description so `--help` documents the exit-code contract.
func ExitCodesConflictHelp ¶
func ExitCodesConflictHelp() string
ExitCodesConflictHelp returns the "Exit Codes" help section for `conflict detect`, which follows the grep-style convention instead.
func IsMachineFormat ¶
IsMachineFormat returns true for formats intended for machine consumption.
func IsTerminal ¶
IsTerminal reports whether the given file descriptor is a terminal. It is the single TTY-detection helper shared by the color gate (stdout) and the destructive-op confirmation prompt (stdin, see bulk_common).
func NewExitError ¶
NewExitError wraps err with an explicit process exit code. It returns nil when err is nil so call sites can `return cliutil.NewExitError(code, mkErr())` without a preceding nil check.
func QuickStartHelp ¶
QuickStartHelp returns a standardized "Quick Start" help string with colors. It wraps the content (which should contain the examples) with the styled header. It automatically handles the newline prefix.
func StripIndent ¶
StripIndent removes common leading indentation from a multiline string.
func ValidateFormat ¶
ValidateFormat checks if the given format is in the allowed list.