cliutil

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2026 License: MIT Imports: 9 Imported by: 0

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

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

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

View Source
var CoreFormats = []string{"default", "compact", "json", "llm"}

CoreFormats contains the default formats supported by all commands.

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

func ExitCodeForError(err error) int

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

func IsMachineFormat(format string) bool

IsMachineFormat returns true for formats intended for machine consumption.

func IsTerminal

func IsTerminal(fd uintptr) bool

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

func NewExitError(code int, err error) error

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

func QuickStartHelp(content string) string

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

func StripIndent(s string) string

StripIndent removes common leading indentation from a multiline string.

func ValidateFormat

func ValidateFormat(format string, allowed []string) error

ValidateFormat checks if the given format is in the allowed list.

func WriteJSON

func WriteJSON(w io.Writer, v any, verbose bool) error

WriteJSON writes the given value as JSON to the writer. If verbose is true, it pretty-prints with indentation.

func WriteLLM

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

WriteLLM writes the given value as LLM-formatted structure. This uses gzh-cli-core/cli's Output formatter set to "llm".

Types

type ExitError

type ExitError struct {
	Code int
	Err  error
}

ExitError carries a process exit code alongside an error so a command's RunE can signal outcomes beyond the default failure code (1). root.Execute inspects it with errors.As and exits with Code; any error that is not an *ExitError keeps the default exit code 1.

func (*ExitError) Error

func (e *ExitError) Error() string

func (*ExitError) Unwrap

func (e *ExitError) Unwrap() error

Unwrap exposes the underlying error for errors.Is/As chains.

Jump to

Keyboard shortcuts

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