cli

package
v0.0.0-...-f4b5801 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package cli provides shared CLI utilities such as table formatting helpers.

Index

Constants

This section is empty.

Variables

View Source
var MarkdownTemplateFuncs = template.FuncMap{
	"mdEscape": MarkdownEscape,
}

MarkdownTemplateFuncs are the helper funcs available in every glamour-fed markdown template. Pass to `template.New(...).Funcs(...)`.

Functions

func AttributesToAnyMap

func AttributesToAnyMap(attrs map[string]string) map[string]any

AttributesToAnyMap converts cobra's StringToString flag value into the map[string]any shape the API inputs expect. Returns nil when no entries are present so the field marshals to JSON null and the server treats it as absent rather than rejecting an empty Map.

func IsInteractive

func IsInteractive(f *os.File) bool

IsInteractive reports whether f is attached to a terminal. It is the single gate used to decide between the interactive pager and plain streaming.

func MarkdownEscape

func MarkdownEscape(s string) string

MarkdownEscape escapes markdown punctuation in user-supplied strings before they land in a template that gets fed to glamour. Without this, characters like `_` in `cost_center` open emphasis spans and the renderer mangles the output (e.g. `cost_center` displays as `cost_****center`).

func NewTable

func NewTable(headers ...any) table.Table

NewTable creates a new table with consistent formatting

func Paginate

func Paginate[T any](seq iter.Seq2[T, error], cfg PagerConfig[T]) error

Paginate renders a paginated iterator.

When Out is an interactive terminal it launches a scrollable table that lazily pulls more rows as the cursor approaches the end, so a multi-thousand-row list never fetches more than the user actually scrolls through. The user quits with q / esc / ctrl-c.

When Out is not a terminal (a pipe, a file, CI), it drains the iterator and writes a plain table — keeping scripts and output redirection working. The caller is responsible for routing structured (--output json) requests elsewhere before reaching here.

func StringMapToAnyMap

func StringMapToAnyMap(m map[string]string) map[string]any

StringMapToAnyMap preserves an existing attribute map (read off a Project, Environment, or Component) when an update command needs to round-trip it without modification.

func TruncateString

func TruncateString(s string, maxWidth int) string

TruncateString truncates a string to maxWidth runes, handling emojis correctly

Types

type PagerConfig

type PagerConfig[T any] struct {
	// Columns are the table header titles.
	Columns []string
	// Row maps one item to its cells. The returned slice must align with Columns.
	Row func(T) []string
	// Out is the destination; defaults to os.Stdout when nil. Used both to
	// decide whether output is interactive and as the render target.
	Out *os.File
	// PageSize is how many rows are pulled from the iterator per batch in
	// interactive mode. Zero uses a sensible default.
	PageSize int
}

PagerConfig describes how a paginated result set is rendered by Paginate.

Jump to

Keyboard shortcuts

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