Documentation
¶
Overview ¶
Package cmdutil provides shared helpers for all commands.
Index ¶
- func AddListFlags(cmd *cobra.Command, page, limit *int)
- func AsMap(v any) map[string]any
- func ClampRowsToLimit(rows []map[string]any, limit int) []map[string]any
- func Confirm(prompt string, yes bool) bool
- func ExtractList(data any, listKeys ...string) (raw any, rows []map[string]any, total int, page int)
- func FormatHHMM(value any) (string, bool)
- func FormatWeekday(value any) (string, bool)
- func Int64PtrIfPositive(value int) *int64
- func Int64PtrIfSet(value string) (*int64, error)
- func IntStringPtrIfPositive(value int) *string
- func IsInteractive() bool
- func PaginateRows(rows []map[string]any, page, limit int) ([]map[string]any, int, int, error)
- func PromptPick(rows []map[string]any, cols []output.Column, idKey string, prompt string) (map[string]any, error)
- func PromptSelect(label string, choices []string) string
- func PromptText(label string) string
- func RowsAsAny(rows []map[string]any) []any
- func RowsFromAny(data any) []map[string]any
- func ServerFromCmd(cmd *cobra.Command) string
- func ShouldUseInteractive(cmd *cobra.Command, force, disabled bool, optionFlags ...string) (bool, error)
- func StringPtrIfSet(value string) *string
- func WithListRows(data any, key string, rows []map[string]any, total, page int) any
- type ListResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddListFlags ¶ added in v0.2.0
AddListFlags registers the standard --limit/-L and --page/-p flags on a command.
func ClampRowsToLimit ¶ added in v0.2.0
ClampRowsToLimit trims rows when a server ignores --limit. It is harmless when the server already honored the requested limit.
func ExtractList ¶
func ExtractList(data any, listKeys ...string) (raw any, rows []map[string]any, total int, page int)
ExtractList pulls rows and pagination info from a standard API list response. The API may return pagination at the top level or nested under a "pagination" key:
{"data": [...], "pagination": {"total": N, "page": N, "totalPages": N}}
{"todos": [...]}
func FormatHHMM ¶ added in v0.2.0
FormatHHMM converts numeric API times such as 830 to "08:30". Already-formatted "08:30" strings pass through unchanged.
func FormatWeekday ¶ added in v0.2.0
FormatWeekday converts API weekday values (1=Mon, 7=Sun) to short names.
func Int64PtrIfPositive ¶ added in v0.2.0
Int64PtrIfPositive returns a pointer for positive integer flag values.
func Int64PtrIfSet ¶ added in v0.2.0
Int64PtrIfSet parses a non-empty integer flag value.
func IntStringPtrIfPositive ¶ added in v0.2.0
IntStringPtrIfPositive returns a decimal string pointer for positive flag values.
func IsInteractive ¶ added in v0.2.0
func IsInteractive() bool
IsInteractive reports whether stdin and stdout are both terminals.
func PaginateRows ¶ added in v0.2.0
PaginateRows applies client-side pagination for endpoints that do not expose page/limit parameters. A zero page means page 1 when a limit is set.
func PromptPick ¶ added in v0.2.0
func PromptPick(rows []map[string]any, cols []output.Column, idKey string, prompt string) (map[string]any, error)
PromptPick displays a table and lets the user pick an item by number (1-based) or by typing an ID directly. Returns the selected row, or nil if cancelled. The idKey specifies which column holds the unique identifier.
func PromptSelect ¶ added in v0.2.0
PromptSelect lets a user choose by 1-based index or by exact choice text.
func PromptText ¶ added in v0.2.0
PromptText reads a single trimmed line from stdin after printing a label.
func RowsAsAny ¶ added in v0.2.0
RowsAsAny converts []map[string]any to []any for JSON/jq compatibility.
func RowsFromAny ¶ added in v0.2.0
RowsFromAny converts an API array value into table rows, ignoring non-object entries to match the existing detail-rendering behavior.
func ServerFromCmd ¶
ServerFromCmd extracts the --server flag, falling back to the configured default.
func ShouldUseInteractive ¶ added in v0.2.0
func ShouldUseInteractive(cmd *cobra.Command, force, disabled bool, optionFlags ...string) (bool, error)
ShouldUseInteractive decides whether a command should open a TUI.
Default behavior: in a TTY with no explicit list/filter flags set, the TUI opens automatically so users can browse interactively. Any changed flag (--search, --limit, etc.) switches to plain table output for scripting.
Override with --interactive to force the TUI (requires a terminal), or --no-interactive to suppress it (useful in aliases or scripts).
func StringPtrIfSet ¶ added in v0.2.0
StringPtrIfSet returns a pointer for non-empty flag values.
Types ¶
type ListResult ¶ added in v0.2.0
func NewListResult ¶ added in v0.2.0
func NewListResult(data any, key string) ListResult
NewListResult extracts rows and pagination metadata from a list response. If the explicit key yields no rows, it falls back to treating the entire response as an array — matching the previous ExtractList behavior.
func (ListResult) FinalizeClientSide ¶ added in v0.2.0
func (r ListResult) FinalizeClientSide(page, limit int) (ListResult, error)
FinalizeClientSide applies client-side pagination and rewrites Raw so JSON and table output show the same rows.
func (ListResult) FinalizeServerSide ¶ added in v0.2.0
func (r ListResult) FinalizeServerSide(limit int) ListResult
FinalizeServerSide clamps rows when a server ignores limit and rewrites Raw so JSON and table output show the same rows.
func (ListResult) WithRows ¶ added in v0.2.0
func (r ListResult) WithRows(rows []map[string]any, total, page int) ListResult
WithRows returns a result with filtered/sorted rows. The Raw field is not updated — call FinalizeClientSide or FinalizeServerSide afterward to synchronize Raw with the new rows for consistent JSON/jq output.